0

Here I have a basic doubt. Here it says that I need not to return global variables.
Now, I am wondering, if I return a global variable (let it be char or int or some other data type), what horrid does it cause?
I know that, when I return a variable from a function, the variable is destroyed immediately.
Does it mean, the memory allocated to that variable is de-allocated/freed? Can someone please shed some light?
Consider the following code:

#include<stdio.h>  

int var;  //a global int

int MyFuction(void)
{
    int temp_var = 0;
    temp_var++;
    return temp_var; //it will get destroyed after returning
}

int main(void)  
{
    MyFunction();
    var++; 
    return var; //Will it get destroyed here, (var being a global variable)? 
}
Community
  • 1
  • 1
  • 1
    There's no immediate danger in returning a global, but it's a "code smell." Always returning the *same* global is a little silly if the user has access to it anyway. Globals are frowned upon in general because they limit extensibility and the potential for multithreading. – Potatoswatter Jul 21 '13 at 00:49

3 Answers3

0
int main(void)  
{
    MyFunction();
    var++; 
    return var; //Will it get destroyed here, (var being a global variable)? 
}

No, it won't. Objects with static storage duration (like var here) are destroyed at program termination.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • +1. Thank you sir. One more thing, now temp_var will get destroyed after returning. Is there anyway I can check whether the memory for temp_var (4 Bytes in this case) is reduced from the total memory allocated to that very process? –  Jul 20 '13 at 19:14
  • 1
    @WedaPashi It is highly dependent on OS (if any) and compiler. You can at least check the stack pointer moved back upwards (if the stack grows downwards) with an offset greater than the size of you local automatic variables. – ouah Jul 20 '13 at 19:23
0

var is gloabl variable it doesn't destroyed after return statement, its life is till program executes and scope is every where in code.

Only variable local to functions goes out-of-scope and destroyed after return statement.

Note: There is two things life and scope. Life of local variable is till function returns. And scope is within function its memory comes from stack. Life of global varialbs is till program terminates and scope is every where in C.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 1
    +1. Thank you sir. One more thing, now `temp_var` will get destroyed after `return`ing. Is there anyway I can check whether the memory for `temp_var` (4 Bytes in this case) is reduced from the total memory allocated to that very process? –  Jul 20 '13 at 19:13
  • 1
    @WedaPashi yes because memory comes from stack-segments its memory should still valid. but you can't rely on contents (memory value ) ah **no** returning not reduce size of your process if local variable. – Grijesh Chauhan Jul 20 '13 at 19:16
  • 1
    @WedaPashi note stack-segment size depends on OS/ compilation models but its not change throught out process executes (stack - grow/shreks from one side as its name suggest)...its heap segment that shrinks and grows, and you can check [Memory Layout of C Programs](http://www.geeksforgeeks.org/memory-layout-of-c-program/) – Grijesh Chauhan Jul 20 '13 at 19:21
  • 2
    @WedaPashi Also note that returning `temp_var` is safe, because its contents will be "copied" to the return value. The local variable goes out of scope, but its value is transferred through the function call. What you should not do is returning arrays with automatic storage duration (so `int arr[10]; return arr;` is **wrong**), because array contents are not copied, instead the array decays into a pointer to its first element, and of course that doesn't exist when the function returned. –  Jul 20 '13 at 20:10
  • @WedaPashi Yes and similarly [as you know return values copy into register](http://stackoverflow.com/questions/17721778/what-is-the-meaning-of-ax-1000-in-the-following-c-program/17721841#17721841) So returning value `temp_var` is not a problem, it coped into register and dies, but if you return `&temp_var`address of `temp_var` then that is wrong because in calling function you will access address of a variable that is die (life-end). --hence you will get a warning from gcc returning local-object address. – Grijesh Chauhan Jul 20 '13 at 20:15
0

the global variable will not get destroyed as they have a lifetime(the time till they are accessible) is that of the whole program.

Keval Doshi
  • 738
  • 6
  • 25