TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Jasen Betts
from: Bo Simonsen
date: 2003-09-03 18:58:32
subject: Pointers

Hello Jasen!

02 Sep 03 06:34, you wrote to me:


 BS>>         tmp = (char *) malloc(80);
 BS>>         sprintf(tmp, "test");

 JB> could be wise to  do

 JB>  if (NULL != (tmp=(char*) malloc(80)) // check for available memory
 JB>    sprintf(tmp,"%.79S","test");   // protect
against overrun (not
 JB> needed here)

Like Kurt showed it.

 BS>> int main()  {   printf("%s", test()); }

 BS>> How do i free the memory i allocated for tmp?

 JB>  int main()
 JB>  {
 JB>          char *ptr;
 JB>          ptr=test();
 JB>          printf("%s",ptr);
 JB>          free(ptr);
 JB>  return 0;
 JB>  }

I know, I would avoid that (forgot to write in my other post).

 JB> you can do
 JB>   char *ptr=test();
 JB>  or
 JB>   printf("%s",ptr=test());

 JB> to reduce the number of lines needed but you will need to use a
 JB> variable to hold the resuult from test() so you can both use it and
 JB> free it.

Indeed :-) Thanks.

Regards,
Bo


--- Squish/Linux v1.12.002 BETA
* Origin: The Night Express, Roennede Dk (2:236/100)
SEEN-BY: 633/267 270
@PATH: 236/100 237/9 20/11 106/1 2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.