1 . Thanks everyone who read the post.
2 . the programe is that :
#include <stdio.h>
#include <string.h>
void do_it(char *p)
{
p = (char *) malloc(100);
\(void \)strcpy\(p,"1234"\);
}
int main(void)
{
char *p;
do_it\(p\);
\(void \)printf\("p = %s \\n",p\);
return 0;
}
cc -g a.c -o a.out
./a.out
it will show that "memory fault(coredump)"
3 . Why ? I think that malloc() get memory from heap , and the heap can be seen at everywhere in the programe .In other words , it is "global" .
4 Then I write another programe. it works correctly .
#include <stdio.h>
#include <string.h>
char *do_it(void)
{
return ((char *) malloc(100));
}
int main(void)
{
char *p;
p = do_it\(\);
strcpy\(p, "asdfa"\);
\(void\) printf\("p = %s \\n",p\);
}
5 . Then , the point 4 declare that point 3 is correct , but it is not we want .
6 . Then I think the OS has changed the value p when do_it() function return