#include<stdio.h>
#include<malloc.h>
#include<string.h>
void foo( char ** ptr)
{
*ptr = malloc(0); // allocate some memory**
strcpy( *ptr, "Hello World");
}
int main()
{
char *ptr = 0;
// call function with a pointer to pointer
foo( &ptr );
printf("%s\n", ptr);
// free up the memory
free(ptr);
return 0;
}而且它还在运行
#include<stdio.h>
#include<malloc.h>
#include<string.h>
void foo( char ** ptr)
{
*ptr = malloc(11); // allocate some memory
strcpy( *ptr, "Hello World");
}
int main()
{
char *ptr = 0;
// call function with a pointer to pointer
foo( &ptr );
printf("%s\n", ptr);
// free up the memory
free(ptr);
return 0;
}通过任何number...it更改malloc始终是running.how,这是可能的?Hello World有12个字符,所以可以在0,12,8,任何数字中运行。
发布于 2011-03-03 14:03:26
很明显,代码中有bug。
有两种可能性:
https://stackoverflow.com/questions/5177152
复制相似问题