简体   繁体   English

C编程,使用内存

[英]C programming, using memory

I have a main function and it has global array 我有一个main函数,它有全局数组

unsigned int    MainArray [800];

then the program calls function which has local 然后程序调用具有本地的函数

unsigned int    FunctionArray [400];

and I have not enough space in memory. 而且我的记忆空间不足。

However the MainArray is not used while the function is called and inside data is not important, also FunctionArray is not required after function is used. 但是,在MainArray函数时不使用MainArray并且内部数据不重要,使用FunctionArray后也不需要FunctionArray

Is any way how FunctionArray can be declared over same memory allocation that the MainArray uses? 有没有什么方法可以在MainArray使用的相同内存分配中声明FunctionArray When I declare MainArray [800]; 当我宣布MainArray [800]; inside main then later on other functions can't access the data. 在main之后,其他函数无法访问数据。

If you declare a variable inside main() , this doesn't make it a global variable, it's a variable local to main() . 如果在main()声明一个变量,这不会使它成为全局变量,它是main()局部变量。 Unless you pass a pointer to it around, other functions can't access it. 除非您将指针传递给它,否则其他函数无法访问它。

You seem to ask for a global array. 你好像要求全局数组。 Just declare it outside main() and every function can access it. 只需 main() 之外声明它,每个函数都可以访问它。

Global variables, however, should be carefully considered since they vulnerate the rule of information hiding/encapsulation (that may be the reason nobody taught you they exist), nevertheless there are sometimes good reasons to use them (especially on devices where memory is a scarce resource which seems to be the case for you). 但是,应该仔细考虑全局变量,因为它们会破坏信息隐藏/封装的规则(这可能是没有人教你存在的原因),但有时也有充分的理由使用它们(特别是在内存稀缺的设备上)似乎是你的情况的资源)。

If you want the data from the mainArray on the later state of the program, ie, after the execution of the function , then, you can't say the "data of the mainArray is not important while the function is called" because it's important afterwards. 如果你希望mainArray的数据在程序的后期状态,即在执行function ,那么,你不能说“在调用函数时mainArray的数据并不重要”,因为它很重要然后。

If you are thinking of a way for "FunctionArray can be declared over same memory allocation that the MainArray uses", then even if there WOULD HAVE BEEN a way, the data of mainArray would have been destroyed which you will be needing in later program as you said. 如果您正在考虑“可以通过MainArray使用的相同内存分配声明FunctionArray”的方法,那么即使有一种方式, mainArray的数据也会被破坏,您将在以后的程序中将其作为你说。

If you don't need the data of main in later state, you can do one thing, 如果你不需要以后状态的main数据,你可以做一件事,

define the array as a pointer outside the main() , 将数组定义为main()之外的指针,

unsigned int * MainArray;

then allocate it dynamically with required size in main() . 然后在main()以所需大小动态分配它。 When the need of the array is finished, just free the memory. 当数组的需要完成后,只需free内存。 The space will be available for farther allocations. 该空间将可用于进一步分配。

If you need the mainArray after the execution of the function having the functionArray ,and not before; 如果在执行具有functionArray的函数之后需要mainArray ,而不是之前; just allocate the memory for the mainArray after the function has been executed. 只需在执行函数后为mainArray分配内存。 and use the local functionArray as the pointer as well, in the function and free the memory before the control gets back from the function. 并在functionArray使用本地functionArray作为指针,并在控件从函数返回之前free内存。 This way your program doesn't occupy the space for the mainArray while executing that function, and when the function is done executing, functionArray is not occupying any space. 这样,程序在执行该函数时不占用mainArray的空间,并且当函数执行mainArray时, functionArray不占用任何空间。

But if you need mainArray, both before and after the function execution, I think there is no way you could solve the memory problem.. 但是如果你需要mainArray,无论是在函数执行之前还是之后,我认为你无法解决内存问题。

you can use the arrays base pointer to access the array's memory space in other functions. 您可以使用数组基指针访问其他函数中的数组内存空间。
you declare a array 你声明一个数组

unsigned int    MainArray [800];

Then if you pass MainArray as parameter to function and receive it as, 然后,如果您将MainArray作为参数传递给函数并接收它,

returnType somefunction(unsigned int   * FunctionArray, ...)
{

}

Then you can use the same memory in both main function and array. 然后你可以在main函数和数组中使用相同的内存。 but in function make sure that you access FunctionArray only within its size limit of 400. This will enable you to access same memory in both functions as array. 但是在函数中确保只在其大小限制为400的范围内访问FunctionArray 。这将使您能够在两个函数中访问与数组相同的内存。
You can even allocate memory to arrays dynamically, if they are needed in program in sequence and not at same time. 如果在程序中按需顺序而不是同时需要,则甚至可以动态地为内存分配内存。 you can use malloc or alloca . 你可以使用mallocalloca

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM