c语言malloc定义报错core,C语言,realloc动态内存申请,出现报错double free or corruption (!prev)...

程序大概如下:

#include

#include

#include

#include

#include

#define MALLOC(num,type) (type*)alloc((num)*sizeof(type))

#define REALLOC(ptr,num,type) (type*)re_alloc(ptr,(num)*sizeof(type))

double *x;

int count =0;

void *alloc(size_t size)

{

void *new_mem;

new_mem = malloc (size);

if(new_mem == NULL)

{

printf("Out of memory!\n");

exit(1);

}

return new_mem;

}

void *re_alloc(void *ptr, size_t new_size)

{

void *renew_mem;

renew_mem = realloc(ptr,new_size);

if((renew_mem == NULL)||(renew_mem == ptr))

{

printf("renew is out of memory\n");

exit(1);

}

return renew_mem;

}

void mem_free(void *ptr)

{

if (ptr != NULL)

free(ptr);

}

int main()

{

......

x = MALLOC(N1, double);

for(i=0;i{

x[count] = A; //为x赋值

count ++;

}

N = N1 +N2;

x = REALLOC(x, N, double);

for(i=0;i{

x[count] = A; //为x赋值

count ++;

}

mem_free(x);

}

在linux下运行,报错,但是仍然可以输出正确结果,报错如下:

*** glibc detected *** ./main: double free or corruption (!prev): 0x0000000000cc7910 ***

======= Backtrace: =========

/lib64/libc.so.6[0x395d875366]

/lib64/libc.so.6[0x395d877e93]

/lib64/libc.so.6(fclose+0x14d)[0x395d865add]

./main[0x402a6c]

/lib64/libc.so.6(__libc_start_main+0xfd)[0x395d81ecdd]

./main[0x400a99]

======= Memory map: ========

。。。。。

求帮助,急、、急、、、

加入输出的主程序如下:

int main()

{

x = MALLOC(N1, double); //其它变量类似

for(i=0;i{

x[count] = A;

y[count] = B;

..... //多个类似的指针变量赋值

count ++;

fprintf(fout, "%12.4f%12.4f\n", x[count], y[count]);

}

N = N1 +N2;

x = REALLOC(x, N, double); //其它变量类似

for(i=0;i{

x[count] = A;

y[count] = B;

count ++;

fprintf(fout, "%12.4f%12.4f\n", x[count], y[count]);

}

for(i=0;i{

fprintf(fout2, "%12.4f%12.4f\n", x[i], y[i]);

}

mem_free(x);

mem_free(y);

....

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值