Author | Message |
---|---|
jpascua
Posts: 197
|
Posted 15:28 Jan 14, 2015 |
So Prof. Keenan said in class that a function cannot return a static array. I noted down that it's because "at some point memory will be deleted by other parts of a program." But can someone explain what this quoted statement meant? I know it had to do with the stack. But how does that apply to static arrays? Thanks! |
jwarren6
Posts: 56
|
Posted 17:58 Jan 14, 2015 |
I understood it to mean this: if you are in a function (that is, you called the function AND have not exited) and let's say you create an array while in said function, then the moment you exit that function the array is gone because it is treated as if it had been a local variable. There is no way for you to pass that array to the main() function. This is because when you do a function call, the program allocates some memory to that function so that it can do its local variables and such BUT when you exit that function, the program now reallocates that memory as free and any other part of the program can now use it (and write all over it). |
jpascua
Posts: 197
|
Posted 18:28 Jan 14, 2015 |
This is only when the static array is declared inside the function but not necessarily returned, right? When something is returned, where is it located on the stack? |
jwarren6
Posts: 56
|
Posted 18:47 Jan 14, 2015 |
This is when the static array is created inside the function. There is no way to successfully pass this array to main() because of the memory issue. When you pass an array to a function, what you are really passing is a reference to a memory location in main program memory where the array starts. Inside the function, you can do whatever you want to legally do with that array without any problems because you are really working with an array that is not located in your local function memory. The stack part has to do with keeping track of function calls and such so that the program knows where to return to. You can have functions calling on functions and so on. |
jpascua
Posts: 197
|
Posted 19:11 Jan 14, 2015 |
So I read your explanation many times trying to picture what you mean. From what I understood, you're saying that a function that creates a static array can pass this to whatever function calls it (in your example, main()). When this happens, that function is passing a reference to the memory location of that array to main() and main() can do whatever it wants to the array it receives since it is not located locally? But this means that you can pass static arrays no...? Maybe I'm misreading something? Last edited by jpascua at
19:12 Jan 14, 2015.
|
jwarren6
Posts: 56
|
Posted 19:19 Jan 14, 2015 |
Sorry for the long explanations. If you create an array inside a function, it cannot be used by main(). This is because the array is in local memory which is destroyed when you exit the function. If you create an array in main() it can be used by a function AND any changes made to it by that function are permanent. This is because the array is always in main() memory. When you pass an array to a function, you really only pass a reference (automatically). |
jpascua
Posts: 197
|
Posted 19:39 Jan 14, 2015 |
Don't apologize, I really appreciate that you're helping me try to understand this. :) So basically, the main() function is the only one that can pass a static array to another function (since it is automatically passing a reference). That means that other functions can modify that array to their own discretion and whatever that array is modified to, other functions will see that same modification. For example: int main() { int array[] = { 1 }; a(array); return 0; } void a(int array[]) { array[0] = 2; cout << array[0] << endl; b(array); } void b(int array[]) { cout << array[0] << endl; } So both function a and b would be outputting a 2? Last edited by jpascua at
19:41 Jan 14, 2015.
|
jwarren6
Posts: 56
|
Posted 19:59 Jan 14, 2015 |
Yes |
kknaur
Posts: 540
|
Posted 11:11 Jan 15, 2015 |
Let me clear up a few things. It is generally safe to create a static array in main and pass it to any function since main and its stack memory will always exist as long as your program is executing. You can change the contents of an array inside of another function and it will also make changes to that array outside of the function (since you are passing memory locations). Where things get tricky is when you have a static array created inside of a function other than main. For example: int main() { } void myFunction() { int arr[5]; //creates a static array in myFunction of size 5 arr[0] = 10; //initializes the first element of the array to be 10 } Now as mentioned in class, the only way to return an array from a function is to actually return a pointer to that array. We will discuss pointers at a later time, but a pointer is an item that just holds a memory address (specifically the first memory address of the array). arr in this example was a static array created in the stack memory assigned for myFunction(). As soon as myFunction returns (the function ends) the memory allocated on the stack is marked as "free" and other parts of your program are now allowed to use this "free" memory which means anything that was there before will eventually be overwritten by new data. If you return a pointer / reference to an array that is now located in this "free" memory area, eventually this memory will be changed by other entities in your program corrupting whatever original data was in your static array. Just wanted to make it clear that passing an array to a function FROM main is ok because the array was created in main and the only time the stack area for main is freed up is when your program is finished. Returning a newly created static array from a function is bad. Generally speaking c++ allows you to return a reference to local data of any type (not just arrays). By local data I mean data allocated on the stack. You should never return a reference to anything allocated on the stack because the results will be unpredictable. If you need further clarification feel free to stop by my office hours or ask again in class and I can review this topic with everyone. |
jpascua
Posts: 197
|
Posted 11:55 Jan 17, 2015 |
I feel that I'm having issues picturing what exactly is happening on the stack. We're talking about 1 stack here, not multiple, right? Since the stack is first-in-last-out and the main() function is the first to execute, it will be at the very bottom of the stack. If a static array is created in main(), does that array sit on the memory address above main() referencing another stack (which is what array uses)? |
kknaur
Posts: 540
|
Posted 14:47 Jan 17, 2015 |
Here are two websites where you can read about Stack vs Heap. I like the explanation of both and maybe this will help. http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap If you have further questions about this topic please come to my office hours and I will go over it with you. Sometimes an online explanation may not be good enough. |