Solved

free global variables?

Posted on 2003-11-12
7
488 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 79
Problem to ASCII 1 183
Unable to start eclipse ? 17 142
Please explain C language error codes ? 5 11
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

803 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