• C

free global variables?

hi!
  i'm writing an error handling routine, which should cleanup memory before calling exit(). i know, i should free() malloced variables, which are stored in the heap. should i also free() global variables, which, if i'm not wrong, are also stored in the heap?
Vikram_BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Hi Vikram_B,

You do not have to free the global variables ... compiler takes care of that ... infact compiler takes care of any memory that was allocated at compile time and that includes global variables

Cheers!
Sunny:o)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunnycoderCommented:
just take care that if you have a global pointer variable and it points to some malloced memory, then you need to free that memory... but space taken up by the pointer variable itself would not be your concern

e.g. suppose you have

int * c;
int d[100];

as a global declaration... and in your program you did
c = (int *) malloc ( 100 * sizeof(int) );

then you need to issue a free(c); ....  space taken up by d will be freed by compiler

As you would realize, while c is a global variable, the memory pointed by it is not in the same space...

you have to free only the malloced/realloced memory.
0
Kent OlsenData Warehouse Architect / DBACommented:

(Late to the party, again....)

Hi Vikram_B,

Sunny's right about not freeing global variables.  Your global variables are generated as part of your program's object module and have an address that is fixed at link time.

The only variables that you CAN free() are heap variables.  Most of the time these are variables that you explicity assign using malloc(), but on occasion a variable will be created in one of the library functions and a pointer to it returned.  strdup() behaves this way.  If you're going to free() all of the heap variables, don't forget about these.

But if your error handler is just going to call exit(), you needn't bother freeing all of the heap variables.  The system will do that for you by discarding the entire heap.  A bunch of free() statements right before calling exit() complicates your source, makes a larger executable, and performs unnecessary (and useless) memory management.


Kent
0
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

sunnycoderCommented:
>(Late to the party, again....)
;o)
are you busy or is it your mail filter again
0
Vikram_BAuthor Commented:
hi  Kdo & sunny,
 

>The only variables that you CAN free() are heap variables

  are global variables also stored in heap? if yes, CAN i free them too?
0
sunnycoderCommented:
>are global variables also stored in heap?
no, they are in a different data segment
>if yes, CAN i free them too?
no again :o)

you can free the memory which you allocated dynamically .. i.e. at run time
0
Vikram_BAuthor Commented:
thanx! i guess that ends the party :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.