内存泄漏怀疑realloc()函数

时间:2016-10-20 17:58:02

标签: c++

我需要你的帮助!

需要解决任务:

用户键入一个浮点数组A.需要将正值复制到数组B,负数 - 复制到数组C.

我为此任务编写了代码:

A[6]={1,2,3, -1,-2,-3} //work correctly
A[7]={1,2,3,0,-1,-2,-3} //work correctly
A[8]={1,2,3,0,0,-1,-2,-3} //work correctly

A[7]={1,2,3,4,-1,-2,-3} //not work
or
A[7]={1,2,3,-4,-1,-2,-3} //not work
A[8]={1,2,3,4,-5,-1,-2,-3} //not work

但问题是,该程序有效,只有当用户键入少于4个正负值时才会起作用:

*** Error in `./main': realloc(): invalid next size: 0x08f84278 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67257)[0xb7432257]
/lib/i386-linux-gnu/libc.so.6(+0x6d577)[0xb7438577]
/lib/i386-linux-gnu/libc.so.6(+0x70410)[0xb743b410]
/lib/i386-linux-gnu/libc.so.6(realloc+0x10e)[0xb743c53e]
./main[0x8048a1b]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb73e3637]
./main[0x80487b1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 fd:01 399768     /home/work/Desktop/New Folder/main
0804a000-0804b000 r--p 00001000 fd:01 399768     /home/work/Desktop/New Folder/main
0804b000-0804c000 rw-p 00002000 fd:01 399768     /home/work/Desktop/New Folder/main
08f7f000-08fa4000 rw-p 00000000 00:00 0          [heap]
b7200000-b7221000 rw-p 00000000 00:00 0 
b7221000-b7300000 ---p 00000000 00:00 0 
b7357000-b7359000 rw-p 00000000 00:00 0 
b7359000-b7375000 r-xp 00000000 fd:01 391774     /lib/i386-linux-gnu/libgcc_s.so.1
b7375000-b7376000 rw-p 0001b000 fd:01 391774     /lib/i386-linux-gnu/libgcc_s.so.1
b7376000-b73c9000 r-xp 00000000 fd:01 391722     /lib/i386-linux-gnu/libm-2.23.so
b73c9000-b73ca000 r--p 00052000 fd:01 391722     /lib/i386-linux-gnu/libm-2.23.so
b73ca000-b73cb000 rw-p 00053000 fd:01 391722     /lib/i386-linux-gnu/libm-2.23.so
b73cb000-b757a000 r-xp 00000000 fd:01 391744     /lib/i386-linux-gnu/libc-2.23.so
b757a000-b757b000 ---p 001af000 fd:01 391744     /lib/i386-linux-gnu/libc-2.23.so
b757b000-b757d000 r--p 001af000 fd:01 391744     /lib/i386-linux-gnu/libc-2.23.so
b757d000-b757e000 rw-p 001b1000 fd:01 391744     /lib/i386-linux-gnu/libc-2.23.so
b757e000-b7581000 rw-p 00000000 00:00 0 
b7581000-b76ee000 r-xp 00000000 fd:01 915714     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b76ee000-b76ef000 ---p 0016d000 fd:01 915714     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b76ef000-b76f4000 r--p 0016d000 fd:01 915714     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b76f4000-b76f5000 rw-p 00172000 fd:01 915714     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b76f5000-b76f8000 rw-p 00000000 00:00 0 
b770c000-b770f000 rw-p 00000000 00:00 0 
b770f000-b7711000 r--p 00000000 00:00 0          [vvar]
b7711000-b7712000 r-xp 00000000 00:00 0          [vdso]
b7712000-b7734000 r-xp 00000000 fd:01 391726     /lib/i386-linux-gnu/ld-2.23.so
b7734000-b7735000 rw-p 00000000 00:00 0 
b7735000-b7736000 r--p 00022000 fd:01 391726     /lib/i386-linux-gnu/ld-2.23.so
b7736000-b7737000 rw-p 00023000 fd:01 391726     /lib/i386-linux-gnu/ld-2.23.so
bfec6000-bfee7000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

错误:

for (int i=0;i<n;i++)
{
if(A[i]!=0)
    {
    if(A[i]>0) 
        {
        B[m-1] = A[i];
        B = (float *) realloc (B, (m++)*sizeof(float));
        }
    else
        {
        C[l-1] = A[i];
        C = (float *) realloc (C, (l++)*sizeof(float));
        }
    }

} 

问题代码块:

<ImageView
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:scaleType="fitXY"
    android:src="@drawable/global_message_b"
    app:srcCompat="@drawable/global_message_b" />

也许我不正确使用realloc?

0 个答案:

没有答案