I have created a function (eg: fun()) and called it from the main function.I have also created an array in the function and I am returning the base address of the array to an integer pointer in the main function.If the array is declared as static in the function fun(),the array elements are properly displayed in the main function.If not,some garbage values are being printed.But the vice versa of this is working fine.That is,decalring an array in the main function and printing it in an another function by passing the base address.So what is the use of declaring the array as static?What is the correct explanation for it?
4 Answers
If an object is declared static in a function, it is essentially a global and permanent, and is not re-initialized every time the function is called like other local variables.
If you have a non-static object which is local to a function, and you return its address, by the time the caller does something with the address the object has gone out of scope because you are no longer in the body of the called function, and the region of memory your pointer refers to has undefined data in it, regardless of what you put there before. If the object was static then it does not fall out of scope.
If you have an object local to main, and you call a function from main and pass a pointer to this object, that object is still alive as far as the program is concerned, because you are still "in" the main function, and will return to it when you leave the body of the called function, so there is no problem referring to that region of memory. This has nothing to do with main: try the same thing from a different function.
Incidentally, none of this has anything to do with arrays, and the whole discussion applies to any kind of object.
2 Comments
static variable exists it is not visible outside the function where it is declared.static variable is not visible outside of the function where it is declared, the memory in which the variable is contained persists and is legally accessible outside of said function.If array declared as static then the array remains alive even after the execution of the function call and hence you can access the elements of the array.
Actually, static arrays are allocated in a different section of memory and is not allocated in the function stack frame. So it does not matter even if the function call is over, the array is still there.
Comments
If you do not declare the array static, then the array "disappears" when you leave the function. In fact, all of the variables that you declare in a function that are not static only exist as long as the function is executing. After return from the function, the variables are de-allocated from the heap. This is called the "scope" of a variable, the area in the code where the variable exists.
In your case, you are returning a pointer to an area where your array once was, when the function was executing, but by the time that the main uses the pointer, the area where the array once was has been overwritten.
Comments
Objects can have one of three storage durations: auto, static, or dynamic.
Objects with auto storage duration have storage allocated for them immediately following the point they are defined, and that storage is only held for the duration of the current scope; once that scope is exited, the object ceases to exist. For example:
void foo( int x ) // storage for x is set aside here
{
int bar; // storage for bar is set aside here
if ( x > 0 )
{
int i; // storage for i is set aside here
...
} // storage for i is released here
} // storage for x and bar is released here
The variable i only exists within the scope of the if statement; bar and x only exist within the scope of the function. Once the function exits, none of those variables exist anymore, and any storage allocated for them is now available for someone else to use1.
Objects with static storage duration have storage allocated for them when the program starts, and that storage is held until the program exits. Anything declared at file scope (outside of any function) or with the static keyword has static storage duration:
void foo( int x ) // storage for x is set aside here
{
static int bar; // storage for bar was set aside at program start
...
} // storage for x is released here; storage for bar will
// be released when the program exits
This is the reason you see the behavior you do; when the array is not declared static, the storage for it is released after the function exits, and someone else is free to overwrite it. When you declare it static, that storage is held until the program exits, so noone else can use it.
Objects with dynamic storage duration are allocated manually using malloc, calloc, or realloc, and held until explicitly released with free.
1. In practice, the storage for
x, bar, and i is usually set aside at function entry and held until the function exits, but logically speaking you can't expect there to be any storage for i outside the scope of the if statement, meaning any pointers to i would not be valid outside of the if statement.
1 Comment
register variables (a 4th one) storage duration when they are first and last used? (not necessarily declaration to end of block?)