1

Could anybody please explain what's wrong with this C++ code? I can see that you cannot return an array in C++ (like in other languages) so I'm returning a pointer. I learned that there's no point in setting the pointer to the address of "myArray" - because "myArray" is already an address (first item)

the output I expected was 1,2,3,4

on different (online) compilers I'm getting different weird results here, including:

  • 1, 4, -993994160, 32767
  • 1, -1077229596, -1077229588, 1075514957
  • 1,2,3,3 (so close)

so here's my dodgy code:

#include <iostream>

using namespace std;

int* getArray(){
    int myArray[] = {1,2,3,4};
    int* pointerToArray = myArray;
    return pointerToArray;
}

void printArray(int inputArr[], int length) {
    for (int i = 0; i < length; i++) {
        cout << inputArr[i] << ", ";
    }
}

int main()
{
   printArray(getArray(),4);
   return 0;
}

any help you could provide is VERY much appreciated!

TheDavil
  • 706
  • 2
  • 7
  • 22
  • Use `std::array` or `std::vector` instead of C-array in that case. – Jarod42 Apr 20 '17 at 21:05
  • And you cannot return reference of local variable. – Jarod42 Apr 20 '17 at 21:06
  • Don't return pointers to local variables. Also, learn to use `std::vector` or `std::array`. You are getting weird results because you're returning a pointer to a variable inside a function that has returned, thus no longer exists. What you now wind up with is -- whatever the compiler feels like giving you. – PaulMcKenzie Apr 20 '17 at 21:06
  • Scope issue. Array gets deallocated as soon as function ends, so pointer is pointing to memory with undefined contents. Looks like it's being used for something after being deallocated. – Mad Physicist Apr 20 '17 at 21:07
  • Use new[] to allocate on the heap instead – Mad Physicist Apr 20 '17 at 21:08

3 Answers3

3

You're returning a pointer to a function's local variable, which ceases to exist when the function returns.

int* getArray(){
    int myArray[] = {1,2,3,4};
    int* pointerToArray = myArray;
    return pointerToArray;
}

myArray effectively disappears when getArray() returns.

This will work as it gives the variable static duration, which means it exists all the time (but then there's only one instance of the variable instead of an instance for each invocation of the function):

int* getArray(){
    static int myArray[] = {1,2,3,4};
    int* pointerToArray = myArray;
    return pointerToArray;
}

There are a multitude of other solutions, including creating an array dynamically using new.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
2

with std::array, you may do:

#include <iostream>
#include <array>


std::array<int, 4> getArray(){
    return {{1,2,3,4}};
}

template <std::size_t N>    
void printArray(const std::array<int, N>& a) {
    for (int e : a) {
        std::cout << e << ", ";
    }
}

int main()
{
   printArray(getArray());
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

You've got a scoping problem, for one. myArray is declared statically in getArray, so as soon as getArray returns, its memory is free for others to use/overwrite.

If you want to use a C array:

#include <alloc.h>

...

int *getArray()
{
    int *myArray = (int *)malloc(4 * sizeof(int));
    myArray[0] = 1;
    ...
    myArray[3] = 4;
    return myArray;
}

int printArray(int *array, uint length) {
    ...
    free(array);
}

Filling in the ellipses appropriately, of course.

jdunlop
  • 181
  • 1
  • 9