排错-Error--memory violation Exception ACCESS_VIOLATION received解决方

简介: 排错-Error--memory violation Exception ACCESS_VIOLATION received解决方

Error -- memory violation : Exception ACCESS_VIOLATION received

 


测试代码:

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;

}

目录
相关文章
|
3月前
|
JSON 数据格式
【ERROR】Error: transaction invalidated with status (ENDORSEMENT_POLICY_FAILURE)
【ERROR】Error: transaction invalidated with status (ENDORSEMENT_POLICY_FAILURE)
33 0
|
4月前
|
应用服务中间件 nginx Windows
[emerg] 15060#200: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket ......
[emerg] 15060#200: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket ......
158 0
|
11月前
|
SQL Java 数据库连接
sql injection violation, syntax error: syntax error, error in :‘**‘expect IDENTIFIER, actual IDENTIF
sql injection violation, syntax error: syntax error, error in :‘**‘expect IDENTIFIER, actual IDENTIF
177 0
SignTool Error: An error occurred while attempting/Error information: “SignerTimeStamp() failed.“
SignTool Error: An error occurred while attempting/Error information: “SignerTimeStamp() failed.“
136 0
abort: error: Temporary failure in name resolution
abort: error: Temporary failure in name resolution
175 0
|
资源调度 JavaScript
The futex facility returned an unexpected error code
The futex facility returned an unexpected error code
748 0
|
Go iOS开发
The operation couldn’t be completed. Unable to log in with account 'myappleid'. An unexpected failure occurred while logging in (Underlying error code 1100).解决方法
The operation couldn’t be completed. Unable to log in with account 'myappleid'. An unexpected failure occurred while logging in (Underlying error code 1100).解决方法
415 0
|
SQL 数据库
DBCC CHECKDB 遭遇Operating system error 112(failed to retrieve text for this error. Reason: 15105) encountered
我们一个SQL Server服务器在执行YourSQLDBa的作业YourSQLDba_FullBackups_And_Maintenance时遇到了错误:   Exec YourSQLDba.Maint.
1156 0