简体   繁体   English

使用c malloc函数进行内存分配的意外结果

[英]unexpected results for memory allocation with c malloc function

I have to allocate memory for 4 pointers to pointers on float (2D) over many iterations (6), but at the second iteration, malloc gives me the same address for two allocations. 我必须在多次迭代(6)上为浮点(2D)上的指针分配4个指针的内存,但在第二次迭代时,malloc为两个分配提供了相同的地址。 Code : 代码:

int i=0, a=0;
for(i=0;i<6;i++)
{
    float** P_i=(float**) malloc(4*sizeof(float*));
    for(a=0;a<4;a++)    P_i[a]=(float*) calloc(4,sizeof(float));

    for(a=0;a<4;a++)    free(P_i[a]);
    free(P_i);
}

Debugging with gdb : 使用gdb进行调试:

(gdb) print i
$42 = 1
(gdb) set $pos=0
(gdb) print P_i[$pos++]
$51 = (float *) 0x804d500
(gdb) print P_i[$pos++]
$52 = (float *) 0x804d148
(gdb) print P_i[$pos++]
$53 = (float *) 0x804d4e8
(gdb) print P_i[$pos++]
$54 = (float *) 0x804d500

P_i[0] and P_i[3] point to the same address 0x804d500 and I can't find why :/ P_i [0]和P_i [3]指向相同的地址0x804d500,我找不到原因:/

between the first for(a=0;a<4;a++) and the 2nd (before freeing) 在第一个(a = 0; a <4; a ++)和第二个(在释放之前)之间

My guess is that gdb breaks on last iteration of the loop, before the last calloc() call. 我的猜测是,在最后一次calloc()调用之前,gdb在循环的最后一次迭代中断。 If it's the case P_i[3] have the address of the previous iteration. 如果是这种情况,P_i [3]具有前一次迭代的地址。

Btw, it's hard to use gdb when there's more than one statement per line. 顺便说一句,当每行有多个语句时,很难使用gdb。

With information available this can't be answered, but let me try. 有了可用的信息,这是无法回答的,但让我试一试。

The code seems ok. 代码似乎没问题。 I can't reproduce your problem either. 我也无法重现你的问题。

You can't really put a breakpoint on a blank line. 你不能真的把断点放在空行上。 I guess that would put it on a line with free. 我想这会让它与免费一致。

My guess is, your code was compiled with optimization enabled, which probably reordered things making sure you are not really sure where execution has stopped. 我的猜测是,你的代码是在启用优化的情况下编译的,这可能会重新排序,确保你不确定执行何时停止。 Disable optimization and re-build (on GCC that would be -O0). 禁用优化并重新构建(在GCC上将是-O0)。 Or show us the disassembly (including current PC where you print). 或者向我们展示反汇编(包括您打印的当前PC)。

My run on Ubuntu gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4 built with -O0 -g, stopped on a line with free (before it was executed): 我使用-O0 -g构建的Ubuntu gcc(Ubuntu 4.8.4-2ubuntu1~14.04.1)4.8.4上运行,在一行中停止(在执行之前):

(gdb) print i (gdb)打印我
$1 = 0 $ 1 = 0
(gdb) set $pos=0 (gdb)设置$ pos = 0
(gdb) print P_i[$pos++] (gdb)print P_i [$ pos ++]
$2 = (float *) 0x602040 $ 2 =(浮动*)0x602040
(gdb) print P_i[$pos++] (gdb)print P_i [$ pos ++]
$3 = (float *) 0x602060 $ 3 =(浮动*)0x602060
(gdb) print P_i[$pos++] (gdb)print P_i [$ pos ++]
$4 = (float *) 0x602080 $ 4 =(浮动*)0x602080
(gdb) print P_i[$pos++] (gdb)print P_i [$ pos ++]
$5 = (float *) 0x6020a0 $ 5 =(浮动*)0x6020a0
(gdb) bt (gdb)bt
#0 main () at malloc.c:12 malloc.c中的#0 main():12
(gdb) list (gdb)列表
7 for(i=0;i<6;i++) 7 for(i = 0; i <6; i ++)
8 { 8 {
9 float** P_i=(float**) malloc(4*sizeof(float*)); 9 float ** P_i =(float **)malloc(4 * sizeof(float *));
10 for(a=0;a<4;a++) P_i[a]=(float*) calloc(4,sizeof(float)); 10为(a = 0; a <4; a ++)P_i [a] =(float *)calloc(4,sizeof(float));
11 11
12 for(a=0;a<4;a++) free(P_i[a]); 12为(a = 0; a <4; a ++)free(P_i [a]);

Does your source code exhibit a problem even if you build it separately (not a part of larger program)? 即使您单独构建源代码(不是较大程序的一部分),您的源代码是否也会出现问题? Do you have custom calloc / malloc implemented? 你有自定义的calloc / malloc吗? What does "nm your-executable|grep calloc" show? “nm your-executable | grep calloc”显示了什么? It should be something like this: 它应该是这样的:

U calloc@@GLIBC_2.2.5 U calloc @@ GLIBC_2.2.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM