Mail Archives: djgpp/1998/08/05/18:30:31
From: | sparhawk AT eunet DOT at (Gerhard Gruber)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: How do I free the allocated memory?
|
Date: | Wed, 05 Aug 1998 22:19:24 GMT
|
Organization: | EUnet Austria
|
Lines: | 43
|
Message-ID: | <35d0d96f.2693733@news.Austria.EU.net>
|
References: | <35c78e73 DOT 7896408 AT news DOT jet DOT es>
|
NNTP-Posting-Host: | e038.dynamic.vienna.at.eu.net
|
Mime-Version: | 1.0
|
NNTP-Posting-Date: | 5 Aug 1998 22:20:17 GMT
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Destination: gah AT jet DOT es (Grzegorz Adam Hankiewicz)
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Wed, 05 Aug 1998 11:37:27 GMT:
>Hello.
>
>I wanted to create a function which accepts a char string, manipulates
>it at returns a new one. I thought about this:
>
>char *modify_string ( char *old_str)
>{
> char *new_str;
>
> new_str = malloc (512); // That's enough for me.
> if (new_str == NULL) return NULL;
>
> sprintf( new_str, "Blah,blah %s", old_str);
>
> return new_str;
>}
>
>Ok, so I learned that after mallocing some memory I always have to
>free it. But know, since I am returning the pointer, I cannot free the
>memory before returning the pointer, and I cannot free the memory
>after (the compiler will never reach that line).
>
>So, how do I free that allocated mem?
You have to free it, that much is true. But you don't have to free it in the
same function where you allocate it. You can free any pointer anywhere. You
only have to make sure that it is a pointer you aquired with malloc.
--
Bye,
Gerhard
email: sparhawk AT eunet DOT at
g DOT gruber AT sis DOT co DOT at
Spelling corrections are appreciated.
- Raw text -