Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

free global variables?

Posted on 2003-11-12
7
489 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:Kent Olsen
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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

792 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