Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

meaning of free() in C.

Posted on 2003-11-10
6
Medium Priority
?
287 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
[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
6 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 90 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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

721 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