delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/05/07:45:48

From: gah AT jet DOT es (Grzegorz Adam Hankiewicz)
Newsgroups: comp.os.msdos.djgpp
Subject: How do I free the allocated memory?
Date: Wed, 05 Aug 1998 11:37:27 GMT
Organization: Gogosoftware
Lines: 26
Message-ID: <35c78e73.7896408@news.jet.es>
NNTP-Posting-Host: infon132.jet.es
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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?

- Grzegorz Adam Hankiewicz - gah AT jet DOT es - http://web.jet.es/gregorio/
- Gogosoftware - http://welcome.to/gogosoftware/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019