Memory leak?

Hello guys.
I need Your help!

Need to solve a task:

User type an float array A. Need to copy positive values to array B, negative - to array C.

I wrote code for this task:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include "stdlib.h"
#include <cmath>


using namespace std;


int main ()
{
int n;            //number of array A elements
int m=1, l=1;     //number of Array B and C correspondly

cout<<"Please enter n: ";
cin>>n;                                       //number input

float *A = (float *) malloc (n*sizeof(float));//dynamic array A definition

for (int i=0;i<n;i++)                         //array A filling
{
cout<<"Please enter A["<<i<<"]= ";
cin>>A[i];
}

float *B = (float *) malloc (m*sizeof(float));//Array B definition with 1 element for beginning
float *C = (float *) malloc (l*sizeof(float));//Array C definition with 1 element for beginning

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));
		}
	}

}
if(m>2) B = (float *) realloc (B, (m--)*sizeof(float)); //because arrays created with 1 extra elements
if(l>2) C = (float *) realloc (C, (l--)*sizeof(float)); //need delete last element that doesn`t have a number

for (int i=0;i<n;i++)
{
cout<<"A["<<i<<"]= "<<A[i]<<endl;
}

free(A);

cout<<endl;

for (int i=0;i<m;i++)
{
cout<<"B["<<i<<"]= "<<B[i]<<endl;
}

free(B);

cout<<endl;

for (int i=0;i<l;i++)
{
cout<<"C["<<i<<"]= "<<C[i]<<endl;
}

cout<<endl;

free(C);


return 0;
}


But problem, that program works, only when, user type less that 4 positive and negative values:

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

Error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
*** 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) 


Problem code block:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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));
		}
	}

}


Maybe i don`t correct use realloc?
You should be using new and delete in C++ instead of malloc and free.

Why do so much work trying to reallocate all the time anyway? If I were you, I'd keep track of how many negative and positive numbers I had as they were being entered.

19
20
21
22
23
24
25
26
27
28
int numNeg = 0;
int numPos = 0;
for (int i=0;i<n;i++)                         //array A filling
{
     cout<<"Please enter A["<<i<<"]= ";
     cin>>A[i];

     if(A[i] < 0) { numNeg++; }
     else { numPos++; }
}

Then, use those numbers to allocate how much space you'll need for your positive array and your negative array.
1
2
float *B = new float[numPos];
float *C = new float[numNeg];

Last edited on
I wanted to use
1
2
int numNeg = 0;
int numPos = 0;


But this solution, how i think, more interest (only my opinion), when arrays B and C reallocated, according to positive or negative values in array A.

This is wrong way, realloc have limitation of use?
Last edited on
Topic archived. No new replies allowed.