1

I want to know why while compiling my code in ubuntu this message appears

    $*** glibc detected *** ./a.out: malloc(): memory corruption: 0x00000000024dd190 ***        

but the same code works without any error in code::blocks and gives me the right results ?? I need your help to fix this error .

I've tried to update the compiler but that doesn't work?!

Update:

the error appears when executing this statement:

    $first_array = (unsigned long*)malloc(sizeof(unsigned long*));

I don't see that there's any error in it, Yes??

4

1 回答 1

0

您在内存中定义的大小等于指针的大小,而不是无符号长整数,您应该使用:

first_array = (unsigned long*)malloc(sizeof(unsigned long));

如果您在 32 位系统上,您的第一个代码将在内存中分配 4 个字节,然后您使用指向 8 字节数据结构的指针指向它。您需要分配变量的大小,而不是指针的大小。

于 2012-11-19T17:57:30.257 回答