|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
贴上代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void main()
{
char *buf = NULL;
const char *str = " i am student, you are teacher ";
buf = (char *)malloc(sizeof(str) * 2);
memset(buf, 0, sizeof(buf));
strcpy(buf, str);
printf("buf:%s \n", buf);
free(buf);
buf = NULL;
}
编译器使用的是微软visual studio 2012版本.
问题:printf打印的时候总是触发断点,进入到malloc.c文件的_heap_alloc(size_t size)函数中.个人感觉buf指针作为堆上内存空间的指针,在strcpy函数之前buf指针并没有改动,问题可能出现在strcpy方面,求大神帮忙解答!!!!!
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
char *buf = NULL;
const char str[] = " I am student, you are teacher ";
buf = (char *)malloc(sizeof(str));
memset(buf, 0, sizeof(buf));
strcpy(buf, str);
printf("buf: %s\n", buf);
free(buf);
// buf = NULL; // 已经 free 了,怎么还用??
return 0;
}
|
|