2

I was playing around with C++ and I stumbled upon this problem. I'm trying to initialize an array pointer on the heap, and it works inside the initialize(), where it outputs 69, but in the main(), it crashes with the error EXC_BAD_ACCESS.

#include <iostream>

void initialize(int* array, int size) {
    array = new int[size];

    // Testing
    array[2] = 69;
    std::cout << array[2] << std::endl; // Works fine
}

int main() {

    int size = 3;
    int* array;

    // Initializing
    initialize(array, size);

    // Testing
    std::cout << array[2] << std::endl; // Crash, EXC_BAD_ACCESS

    // Cleanup
    delete[] array;
    array = nullptr;


    return EXIT_SUCCESS;
}

Please help me understand the problem with this.

Yes, I know I should use std::vector but I want to understand why this doesn't work :)

3
  • For that matter, it crashes at the delete[] function too, with the error text "pointer being freed was not allocated" Commented Oct 20, 2015 at 17:51
  • 1
    You pass your array pointer to your function by value - so when you assign new int[size] to it, array in main stays unchanged. Commented Oct 20, 2015 at 17:52
  • 1
    @MadsMarquart Well, I'm pretty sure we have a duplicate for this question, I see it asked about 3-5 times a month. Though there might be too many duplicates for it, and we don't have a real canonical Q&A. Commented Oct 20, 2015 at 18:07

3 Answers 3

18

When you pass array to the function, a copy of that pointer is made. When you assign new int[size]; to array, you assign it actually to the argument, which is the copy I was talking about. To really modify the array defined in main, use references. Change the definition of the function to

void initialize(int*& array, int size)

or return the pointer like1

int* initialize(int size)

and try it again.


I recommend the second method due to its higher expressiveness: something like

initialize(array, 3);

does not make clear if array is modified or not. OTOH,

int* array = initialize(3);

does.


1 as noted by @Jack in the comments to this answer

Sign up to request clarification or add additional context in comments.

3 Comments

Or directly change the signature to int* initialize(int size) if you don't have any specific purpose to set an existing pointer by reference.
"array to the function, a copy of it it is made."-you may want to clear out that copy of the pointer is made not the whole array itself
@Giorgi Done, although I think there wasn't much to confuse.
0

The reason why the program fails is because you want the memory to be allocated outside the initialize function and the function to operate on that memory.

Simply remove the new statement from your function so that it looks like this...

void initialize(int* array, int size) {
        for (int i = 0; i < size; i++) {
        cout << array[i] << " ";
    }
}

... then, do your allocation in main and just before the function call...

int size = 3;
int* array = new int [size];

initialize(array, size);

Comments

0

Pass the address of the pointer to avoid the error message

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.