小编arp*_*ita的帖子

调用free()抛出分段错误

我有下面的代码,它引发了一个分段错误.请建议可以做些什么.

#include <stdio.h>

int main() {
    char *p ,*q ;

    p =(char *)malloc(20) ;
    *p = 30 ;
    p++ ;
    p=q ;

    free(q) ;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c unix malloc free

2
推荐指数
1
解决办法
842
查看次数

免费提供错误

考虑以下两个程序:

/***************correct: no error for this code **************/
#include <stdio.h>
#include <string.h>
int main()
{

char  *p ,*q ;
p =(char *)malloc(10) ;
 strcpy( p , "AB") ;
*p = '\0' ;
p++ ;
q = p ;
//*q = 32 ;
free(q) ;
return 0;
}
/*************code2 which gives error ********************/
#include <stdio.h>
#include <string.h>
int main()
{

int  *p ,*q ;
p =(int  *)malloc(10) ;
*p = 30 ;
p++ ;
q = p ;
*q = 32 …
Run Code Online (Sandbox Code Playgroud)

c unix malloc

0
推荐指数
1
解决办法
372
查看次数

标签 统计

c ×2

malloc ×2

unix ×2

free ×1