• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 ?
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()
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

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
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 .
sunnycoderCommented:
yes thats right ... however a safer practice is to initialize pointers to NULL as well
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.