delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/12/10/12:14:42

Date: Sat, 10 Dec 1994 17:14:11 UTC+0200
From: Francesc Guasch-Ortiz <frankie AT si DOT upc DOT es>
Subject: Re: Strange crashes
To: endre AT alpha1 DOT obuda DOT kando DOT hu, djgpp AT sun DOT soe DOT clarkson DOT edu

Hirling Endre wrote :
> 2. What's wrong with the following function?
 
> char* string(int n,char c)
> {
>         char* ts;
>         ts=(char*)malloc(n+1);
>         memset(ts,c,n);
>         ts[n]=0;
>         return(ts);
> }

I think the wrong stuff here is the way the variable is declared.
If you want to return an allocated variable you should make it static.
Variables declared in functions are stored in the stack. So when the
function returns the space allocated in the stack is released.

Hope this helps.

	frankie


- Raw text -


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