Solved

free global variables?

Posted on 2003-11-12
7
485 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:Vikram_B
  • 4
  • 2
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 45 total points
ID: 9730077
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9730145
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9741306

(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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9745261
>(Late to the party, again....)
;o)
are you busy or is it your mail filter again
0
 

Author Comment

by:Vikram_B
ID: 9746274
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9746308
>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
 

Author Comment

by:Vikram_B
ID: 9746354
thanx! i guess that ends the party :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
In desperate need of help 8 129
Eclipse Luna does not see MinGW 5 234
libcurl and C++ - Post JSON Data 8 1,072
valid enum? 6 68
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now