Error -- memory violation : Exception ACCESS_VIOLATION received
by:授客 QQ:1033553122
测试代码:
Action()
{
char *a = NULL;
strcpy(a, "shouke");
return 0;
}
运行报错:
Action.c(4): Error: C interpreter run time error: Action.c (4): Error -- memory violation : Exception ACCESS_VIOLATION received.
Action.c(4): Notify: CCI trace: Action.c(4): strcpy(0x00000000, 0x032b0111 "shouke")
.
Action.c(4): Notify: CCI trace: Compiled_code(0): Action()
原因分析:
char *a = NULL; a为指向栈内存的指针,暂时值为NULL,指向内存为空。所以,不能用于存放内容。
改正方法:
法一,用数组开辟栈内存
Action()
{
#char *a = NULL;
char a[20]={0};
strcpy(a, "shouke");
return 0;
}
法二,开辟堆内存:
Action()
{
char *a = NULL;
a = (char *)malloc(sizeof(char)*20);
strcpy(a, "shouke");
free(a);
a = NULL;
return 0;
}