## question on memory allocation for char strings ##

Siping Liu siping at cathedral.cerc.wvu.wvnet.edu
Fri Jun 29 03:49:18 AEST 1990


look at this example:
/**************************/
main()
{
  f1(f2());
}

f1(parm1)
char parm1[];
{
  printf("%s", parm1);
}

char *f2()
{
  char abc[100];

  strcpy(abc, "12345");

  return(abc);
}
/*******************************/

The question is: can I get the correct print out in function "f2"
as I have set in function "f1" ?? After the program returned from "f2",
memory space for "abc" was freed and may be used when calling "f1". So
even I get a correct print out, I still question if it's correct.

I have been told a lot of times that "strcpy" is always safe, but
I replace "f1" above by "strcpy", then how can I guarantee that it is safe?

Thanks in advance.

siping at cerc.wvu.wvnet.edu



More information about the Comp.lang.c mailing list