I've had problems with variables overwriting each other in memory, so I decided I'd try to allocate one of my arrays dynamically.
In the simplified code below, I'm attempting to create an array of integers using dynamic allocation, then have a function edit the values within that array of integers. Once the function has finished executing, I'd like to have a nicely processed array for use in other functions.
From what I know, an array cannot be passed to a function, so I'm simply passing a pointer to the array to the function.
#include <iostream>
using namespace std;
void func(int *[]);
int main(){
//dynamically allocate an array
int *anArray[100];
anArray[100] = new int [100];
func(anArray);
int i;
for (i=0; i < 99; i++)
cout << "element " << i << " is: " << anArray[i] << endl;
delete [] anArray;
}
void func(int *array[]){
//fill with 0-99
int i;
for (i=0; i < 99; i++){
(*array)[i] = i;
cout << "element " << i << " is: " << array[i] << endl;
}
}
When I attempt to compile the code above, g++ gives me the following warning:
dynamicArray.cc: In function ‘int main()’:
dynamicArray.cc:21:12: warning: deleting array ‘int* anArray [100]’ [enabled by default]
When I run the compiled a.out executable anyway, it outputs nothing, leaving me with nothing but the message
Segmentation fault (core dumped)
in terminal.
What am I doing wrong? My code not attempting to access or write to anything outside of the array I created. In fact, I'm not even attempting to read or writing to the last element of the array!
Something REALLY weird happens when I comment out the part that actually modifies the array, like so
//(*array)[i] = i;
G++ compiles with the same warning, but when I execute a.out I get this instead:
element 0 is: 0x600df0
element 1 is: 0x400a3d
element 2 is: 0x7f5b00000001
element 3 is: 0x10000ffff
element 4 is: 0x7fffa591e320
element 5 is: 0x400a52
element 6 is: 0x1
element 7 is: 0x400abd
element 8 is: 0x7fffa591e448
element 0 is: 0x600df0
element 1 is: 0x400a3d
element 2 is: 0x7f5b00000001
element 3 is: 0x10000ffff
element 4 is: 0x7fffa591e320
element 5 is: 0x400a52
element 6 is: 0x1
element 7 is: 0x400abd
element 8 is: 0x7fffa591e448
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x00007fffa591e2f0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f5b92ff4b96]
./a.out[0x400976]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f5b92f9776d]
./a.out[0x400829]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:13 4070334 /home/solderblob/Documents/2013 Spring Semester/CSC 1254s2 C++ II/Assignment 1/a.out
00600000-00601000 r--p 00000000 00:13 4070334 /home/solderblob/Documents/2013 Spring Semester/CSC 1254s2 C++ II/Assignment 1/a.out
00601000-00602000 rw-p 00001000 00:13 4070334 /home/solderblob/Documents/2013 Spring Semester/CSC 1254s2 C++ II/Assignment 1/a.out
01eb5000-01ed6000 rw-p 00000000 00:00 0 [heap]
7f5b92a64000-7f5b92a79000 r-xp 00000000 08:16 11276088 /lib/x86_64- linux-gnu/libgcc_s.so.1
7f5b92a79000-7f5b92c78000 ---p 00015000 08:16 11276088 /lib/x86_64- linux-gnu/libgcc_s.so.1
7f5b92c78000-7f5b92c79000 r--p 00014000 08:16 11276088 /lib/x86_64- linux-gnu/libgcc_s.so.1
7f5b92c79000-7f5b92c7a000 rw-p 00015000 08:16 11276088 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5b92c7a000-7f5b92d75000 r-xp 00000000 08:16 11276283 /lib/x86_64-linux-gnu/libm-2.15.so
7f5b92d75000-7f5b92f74000 ---p 000fb000 08:16 11276283 /lib/x86_64-linux-gnu/libm-2.15.so
7f5b92f74000-7f5b92f75000 r--p 000fa000 08:16 11276283 /lib/x86_64-linux-gnu/libm-2.15.so
7f5b92f75000-7f5b92f76000 rw-p 000fb000 08:16 11276283 /lib/x86_64-linux-gnu/libm-2.15.so
7f5b92f76000-7f5b9312b000 r-xp 00000000 08:16 11276275 /lib/x86_64-linux-gnu/libc-2.15.so
7f5b9312b000-7f5b9332a000 ---p 001b5000 08:16 11276275 /lib/x86_64-linux-gnu/libc-2.15.so
7f5b9332a000-7f5b9332e000 r--p 001b4000 08:16 11276275 /lib/x86_64-linux-gnu/libc-2.15.so
7f5b9332e000-7f5b93330000 rw-p 001b8000 08:16 11276275 /lib/x86_64-linux-gnu/libc-2.15.so
7f5b93330000-7f5b93335000 rw-p 00000000 00:00 0
7f5b93335000-7f5b93417000 r-xp 00000000 08:16 31987823 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f5b93417000-7f5b93616000 ---p 000e2000 08:16 31987823 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f5b93616000-7f5b9361e000 r--p 000e1000 08:16 31987823 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f5b9361e000-7f5b93620000 rw-p 000e9000 08:16 31987823 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f5b93620000-7f5b93635000 rw-p 00000000 00:00 0
7f5b93635000-7f5b93657000 r-xp 00000000 08:16 11276289 /lib/x86_64-linux-gnu/ld-2.15.so
7f5b93834000-7f5b93839000 rw-p 00000000 00:00 0
7f5b93853000-7f5b93857000 rw-p 00000000 00:00 0
7f5b93857000-7f5b93858000 r--p 00022000 08:16 11276289 /lib/x86_64-linux-gnu/ld-2.15.so
7f5b93858000-7f5b9385a000 rw-p 00023000 08:16 11276289 /lib/x86_64-linux-gnu/ld-2.15.so
7fffa5900000-7fffa5921000 rw-p 00000000 00:00 0 [stack]
7fffa59ff000-7fffa5a00000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
for(i=0; i!=size; ++i)(plus of course the type fori).