• C

meaning of free() in C.

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
raghava_dgAsked:
Who is Participating?
 
sunnycoderCommented:
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
 
raghava_dgAuthor Commented:
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
 
sunnycoderCommented:
>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
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
sunnycoderCommented:
>>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
 
raghava_dgAuthor Commented:
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
 
sunnycoderCommented:
yes thats right ... however a safer practice is to initialize pointers to NULL as well
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.