Question

I'm dereferencing a pointer to my array struct in my function and printing it within the function. This works correctly, however, as soon as I return the pointer out of the function it prints incorrectly. I did some research on similar questions, but I just cannot seem to find my exact problem.

correct print: 11 5 1 2 3 4 5 10 20 30 40 incorrect: 11 5 1 2 5024 96 0 0 20 30 40

(Problem areas are in commented in CAPS)

[arraytools.cpp]

#include "arraytools.h"
#include <iostream>

using namespace std;

void DisplayArray (short int* a)
{
    short int size = a[0];
    int i;
    for (i=0; i<size; i++)
    {
        cout<< a[i] << " ";
    }
    cout<<endl;
}

short int* ConcatArray (short int* a1, short int* a2)
{
    short int size = a1[0] + a2[0] + 1;  //size of newarray
    short int *ptr;          //pointer for newarray
    short int newarray[size];  //initializing new array with given new size
    newarray[0] = size;    //making first object in new array the size of it

    int i,j;    
    for (i=0; i<a1[0]; i++)  //loop to store first array objects to newarray
    {
        newarray[i+1] = a1[i];
    }

    int lastpoint = a1[0] + 1;  //marks the point to start adding the second array to newarray
    for (j=0; j<a2[0]; j++)  //loop to store second array objects to newarray
    {
        newarray[lastpoint] = a2[j];
        lastpoint++;

    }

    ptr = &newarray[0];  //assigning new array to pointer
    DisplayArray(ptr);  //PRINTS CORRECTLY HERE
    return ptr;
}

[main.cpp]

#include "arraytools.h"
#include <iostream>
using namespace std;

int main()
{
    char choice = 'y';  //user defined later in program
    while (choice == 'y') // for repeating process
    {
        //declaring two arrays of short int
        short int arr1[] = {5,1,2,3,4};
        short int arr2[] = {5, 10, 20, 30, 40};

        //pointers to refer to declared arrays
        short int* nptr, *ar1, *ar2;

        ar1 =arr1;
        ar2 =arr2;

        DisplayArray(ar1);
        DisplayArray(ar2);

        nptr = ConcatArray(ar1, ar2); //RECIEVES RETURNED POINTER
        DisplayArray(nptr);  //PRINTS INCORRECTLY

        cout<<"Run process again? y/n: "; //loop exit condition
    cin >> choice;
    }
   return 0;
}
Was it helpful?

Solution

This line is root of problem:

short int newarray[size];

It allocates array in stack, and then you return address of that, even though it becomes invalid as soon as you return from that function. Try this:

short *newarray = new short [size];

Now it goes to heap. Of course you should also delete that with delete[] operator when you no longer need it, like maybe in main after printing:

delete[] nptr;

OTHER TIPS

You are allocating newarray "on the stack". It will be reclaimed once it goes out of scope (i.e. function ends). Use malloc if you really want to do this. Or if you really are using C++ consider a better container such as std::vector.

ConcatArray (short int* a1, short int* a2)
{
    short int size = a1[0] + a2[0] + 1;  //size of newarray
    short int *ptr;          //pointer for newarray
    short int newarray[size];  //initializing new array with given new size

When you put the new array as a variable local to the function, it is allocated on the stack (usually). Then you return a pointer to the calling code. But the memory is freed up once the method returns. So the pointer is pointing to an undefined area of memory once the called function returns.

Your function returns a pointer on a local variable wich is stored in stack. You need malloc memory for your data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top