Solved

free global variables?

Posted on 2003-11-12
7
492 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

726 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