Adding onto Dynamic Memory

I'm playing with the idea of having the user enter ints until a -1 is entered. Every int that is entered is stored in an dynamic array.

My thought process on this was to keep deleting and recreating new memory of +1 size larger, and then have another block of the same size to keep the data, and then finally copy them back and forth to one another.

I'm sure there is an error in my logic or implementation ( or both! ) but any help would be appreciated!

My code seems to work for 4 iterations, and then all hell breaks loose. I'll post the error at the end since it's really long.

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
// Program Name : ints.cpp
// Compiler     : g++
// Written By   : Steven Riedel <steven.riedel@gmail.com>
// Playing With : Moving data from one chuck of dynamic memory and 
//                then back again, increasing in size. 

#include <iostream>
using namespace std;

int main( void ) {


   int input;
    // the new number being entered. 

   int size = 0;
    // my counter.

   int* ptr = new int;
   int* temp = new int;
    // pointers to memory I'm going to be working with. 

    do { 

      cout << "Enter a number : ";
      cin >> input;

      *( ptr + size ) = input;
      // put the data into the memory

      temp = new int[ size ];
      // askes for some more memory so I can copy ptr into it. 

         for ( int i = 0; i <= size; i++ ) {
            *( temp + i ) = *( ptr + i );
            // copies what is in ptr into temp, element by element
         }
         delete ptr;
         
      size++;

      ptr = new int[ size ];
      // asks for more memory so I can enter a new number into it. 
      
         for ( int i = 0; i <= size; i++ ) {
            *( ptr + i ) = *( temp + i );
            // puts the number from temp back into ptr 
         }
         delete temp;

   } while ( input != -1 );
   
   delete ptr;

   return 0;
}


Here is the error ( long ):
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
$ ./a.out
Enter a number : 1
Enter a number : 2
Enter a number : 3
Enter a number : 4
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x09f55008 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xddfff1]
/lib/tls/i686/cmov/libc.so.6[0xde16f2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xde47cd]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x1e56f1]
./a.out[0x80487f5]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xd8bb56]
./a.out[0x80486a1]
======= Memory map: ========
0012d000-00213000 r-xp 00000000 08:01 1288       /usr/lib/libstdc++.so.6.0.13
00213000-00217000 r--p 000e6000 08:01 1288       /usr/lib/libstdc++.so.6.0.13
00217000-00218000 rw-p 000ea000 08:01 1288       /usr/lib/libstdc++.so.6.0.13
00218000-0021f000 rw-p 00000000 00:00 0 
0023c000-00258000 r-xp 00000000 08:01 2341       /lib/libgcc_s.so.1
00258000-00259000 r--p 0001b000 08:01 2341       /lib/libgcc_s.so.1
00259000-0025a000 rw-p 0001c000 08:01 2341       /lib/libgcc_s.so.1
00491000-004b5000 r-xp 00000000 08:01 394090     /lib/tls/i686/cmov/libm-2.10.1.so
004b5000-004b6000 r--p 00023000 08:01 394090     /lib/tls/i686/cmov/libm-2.10.1.so
004b6000-004b7000 rw-p 00024000 08:01 394090     /lib/tls/i686/cmov/libm-2.10.1.so
00b9b000-00bb6000 r-xp 00000000 08:01 64         /lib/ld-2.10.1.so
00bb6000-00bb7000 r--p 0001a000 08:01 64         /lib/ld-2.10.1.so
00bb7000-00bb8000 rw-p 0001b000 08:01 64         /lib/ld-2.10.1.so
00d75000-00eb3000 r-xp 00000000 08:01 394086     /lib/tls/i686/cmov/libc-2.10.1.so
00eb3000-00eb4000
00eb4000-00eb6000 r--p 0013e000 08:01 394086     /lib/tls/i686/cmov/libc-2.10.1.so
00eb6000-00eb7000 rw-p 00140000 08:01 394086     /lib/tls/i686/cmov/libc-2.10.1.so
00eb7000-00eba000 rw-p 00000000 00:00 0 
00f32000-00f33000 r-xp 00000000 00:00 0          [vdso]
08048000-08049000 r-xp 00000000 08:01 787073     /home/lake/cpp.programs/a.out
08049000-0804a000 r--p 00000000 08:01 787073     /home/lake/cpp.programs/a.out
0804a000-0804b000 rw-p 00001000 08:01 787073     /home/lake/cpp.programs/a.out
09f55000-09f76000 rw-p 00000000 00:00 0          [heap]
b7600000-b7621000 rw-p 00000000 00:00 0 
b7621000-b7700000 ---p 00000000 00:00 0 
b772e000-b7730000 rw-p 00000000 00:00 0 
b773b000-b773f000 rw-p 00000000 00:00 0 
bfbc1000-bfbd6000 rw-p 00000000 00:00 0          [stack]
Aborted

you have to use "delete []" for all pointer you created using "new []". (You can safely call "new int[1]" if you need only one element).

Ciao, Imi.
For a start, this code leaks the int sized block stored in temp.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   int size = 0;
    // my counter.

   int* ptr = new int;
   int* temp = new int;
    // pointers to memory I'm going to be working with. 

    do { 

      cout << "Enter a number : ";
      cin >> input;

      *( ptr + size ) = input;
      // put the data into the memory

      temp = new int[ size ];


Then the next few lines overwrite the buffer allocated to temp.
1
2
3
4
5
6
      // askes for some more memory so I can copy ptr into it. 

         for ( int i = 0; i <= size; i++ ) {
            *( temp + i ) = *( ptr + i );
            // copies what is in ptr into temp, element by element
         }


Plus as imi said, you've use new int and new int[] on temp, and you don't know which delete to call.
Last edited on
Topic archived. No new replies allowed.