Solved

meaning of free() in C.

Posted on 2003-11-10
6
280 Views
Last Modified: 2010-04-15
Frndz ,

I have a code like below ..



...........
if(xmlString != NULL)
      {
            free(xmlString);
            xmlString = NULL;
      }

      xmlString = (char *) malloc (maxSize);

      if(xmlString == NULL)
      {
            cout << "Insufficient memory available to allocate to xmlString" << endl;
            exit(-1);
      }

      memset(xmlString,'\0',maxSize );

My reqrmnt was to dynamically allocate memory for xmlString each time I call the function . But while i am debugging using dbx ,I found  that even after the execution of statement free(xmlString) if I print xmlString in dbx it will show me the contents of the string .

My qtn is what exactly free() will do ?
Will it delete the content and release the memory to OS ?
OR Does this mean xmlString is not valid after execution of free(xmlString) ?

If I free(xmlString) and again alloacte memory using malloc will this alocates in same meory space ? or diff memeory space ? will there be any prob by freeing the memory and allocating diff amount of memory again in a prog ?


Thanks for ur precious suggestions .

Thanks
Raghava
0
Comment
Question by:raghava_dg
  • 4
  • 2
6 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 30 total points
ID: 9713301
Hi raghava_dg,
> My qtn is what exactly free() will do ?
Free will change the system data structures to indicate that the memory has been disallocated and you are no longer using that part of memory
> Will it delete the content and release the memory to OS ?
It generally will not delete the content ... memory will just be handed over to the system (put back on free list)
> OR Does this mean xmlString is not valid after execution of free(xmlString) ?
xmlString is a valid variable name for your program but it is not holding a valid memory location (location which you are authorized to access)


Cheers!
Sunny:o)
0
 

Author Comment

by:raghava_dg
ID: 9713430
Thanks sunny for the quick response ,

do u have any suggestions abt one more qtn I asked  ...

If I free(xmlString) and again alloacte memory using malloc will this alocates in same meory space ? or diff memeory space ? will there be any prob by freeing the memory and allocating diff amount of memory again in a prog ?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9713446
>If I free(xmlString) and again alloacte memory using malloc will this alocates in same meory space ? or diff memeory space ?
can be either ... depends on the system ... you cannot predict anything in this regard

>will there be any prob by freeing the memory and allocating diff amount of memory again in a prog ?
absolutely not as long as you free all the memory you allocate ....
to resize allocated memory, use realloc()
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9713482
>>will there be any prob by freeing the memory and allocating diff amount of memory again in a prog ?
>absolutely not as long as you free all the memory you allocate ....
what I meant was...

allocate N bytes
free N bytes

allocate M bytes
allocate K more
Free M bytes

allocate P bytes
free p bytes
free K bytes

is perfectly all right ... you can allocate as much as you want whenever you want (subject to constraints by system resources) but you need to take care that whatever you allocated should ultimately be free()d
0
 

Author Comment

by:raghava_dg
ID: 9713604
OK Thanks . One more quick Qtn .

i declared

char * xmlString ;

An I allocated mem as follows

xmlString = (char *) malloc (maxSize);

and I am deallocating memory as below

free(xmlString)   ...This should be fine rt ? If I am rt  , free(xmlString) should deallocate all the memory . No need for me to supply the ammount of memory allocated during a call to free .
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9713618
yes thats right ... however a safer practice is to initialize pointers to NULL as well
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

708 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

17 Experts available now in Live!

Get 1:1 Help Now