user979388
user979388

Reputation: 53

Passing a pointer to dynamically allocated array to a function in C++

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)

Upvotes: 1

Views: 4161

Answers (4)

vivek
vivek

Reputation: 5219

Array indexing starts from 0.
Array of len 100 has indexes from 0 to 99.

So anArray[100] gives you Segmentation Fault.

May be you want to do this:
anArray[99] = new int[100];

OR if you just want to dynamically allocate an array of pointer to ints, do that following:
int **anArray = new int*[100];

Upvotes: 1

sgarizvi
sgarizvi

Reputation: 16796

int *anArray[100];
anArray[100] = new int [100];

First you are allocating an array of 100 pointers. Then you are performing out of range access. The last element of anArray is anArray[99], but you are allocating memory to anArray[100] which does not exist. This will cause a segmentation fault.

In the end, you are deleting a static array of type int*. anArray is allocated at compile time and contains 100 pointers of type int. Remove the delete[] statement.

Upvotes: 0

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17415

//dynamically allocate an array
int *anArray[100];
anArray[100] = new int [100];

anArray is an array of 100 pointers-to-int. To the 101th element (buffer overflow!) you assign a pointer that points to the first element of a dynamically allocated array of 100 ints. You want to fix that and merge the two lines as int* anArray = new int[100];.

Upvotes: 0

The-Q
The-Q

Reputation: 233

When writing:

int *anArray[100];
anArray[100] = new int [100];

In the first line, you are allocating an array of 100 pointers to int. In the second line, you dynamically allocating an array of ints and assigning the address of that array to the 100 cell of the array of pointers. Proper syntax would be:

int *anArray;
anArray = new int [100];

Upvotes: 5

Related Questions