0

The following code is printing garbage values. I am passing an array to a function which adds 5 to every element, but when it returns that array's pointer, the main is showing garbage.

I have tried both indexing and pointers there in main but still same results. How can I fix this?

# include <conio.h>
# include <iostream>
using namespace std;

int * add5ToEveryElement(int arr[], int size)
{
    int theArray[5];
    for(int i=0; i<size; i++)
    {
        theArray[i] = arr[i] + 5;
        cout<<theArray[i]<<endl;
    }
    return theArray;
}

void main()
{
    const int size = 5;
    int noArr[size];
    for(int i=0; i<size; i++)
    {
        noArr[i] = i;
    }
    int *arr = add5ToEveryElement(noArr, size);
    cout<<endl;cout<<endl;
    for(int i=0; i<size; i++)
    {
        cout<<arr[i]<<endl;
    }
    cout<<endl;cout<<endl;cout<<endl;cout<<endl;
    for(int i=0; i<size; i++)
    {
        cout<<*arr<<endl;
        *arr++;
    }
    getch();
}
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
  • @Mat o no sir, its not a duplicate this is the problem i am facing and searching didnt helped me out – Bugs Happen Dec 23 '12 at 08:58
  • It's the same problem. You're returning a pointer to an automatic (stack-based) array. – Mat Dec 23 '12 at 08:59
  • well that guy is not using indices like i am doing mine is updated version, not a duplicate :P – Bugs Happen Dec 23 '12 at 09:01
  • @Asadullah Is there a reason why you don't use `std::vector` instead of arrays? – jogojapan Dec 23 '12 at 09:02
  • @jogojapan no sir there is no reason but i am teaching someone how arrays work and how they are passed to function and returned from function :) – Bugs Happen Dec 23 '12 at 09:05
  • These questions are also strongly related: http://stackoverflow.com/questions/8745260/c-return-array-from-function, http://stackoverflow.com/questions/10808825/c-function-to-return-array – jogojapan Dec 23 '12 at 09:10
  • sir those questions are not even answered. And my question is totally different. This is not a duplicate. I repeat NOT A DUPLICATE – Bugs Happen Dec 23 '12 at 09:15

3 Answers3

2

theArray is a local array in the function add5ToEveryElement() which you are returning to main(). This is undefined behaviour.

Minimally you can change this line:

int theArray[5];

to:

int *theArray = new int[5];

It'll work fine. Don't forget to delete it later in main(). SInce you modify the original pointer, save it:

int *arr = add5ToEveryElement(noArr, size);
int *org = arr;
// Rest of the code

//Finally

 delete[] org;
P.P
  • 117,907
  • 20
  • 175
  • 238
0

Returning an array from a function is generally considered bad.

Unless you MUST have a "new" array, I would suggest the typical case in C and C++ is to modify the input array. If the CALLING function wants to have two separate arrays, then it can do so by making it's own copy. Alternatively, you could write your code to have two arrays passed into your function, e.g.

void add5ToEveryElement(int arr[], int arr2[], int size)
{
    for(int i=0; i<size; i++)
    {
        arr2[i] = arr[i] + 5;
        cout<<theArray[i]<<endl;
    }
}

then your main would call with two array arguments, and if you wish to use the same as input and output it will do that too.

Sure, this isn't exactly the answer to your question, but it gives a "better" solution to your problem.

I generally dislike allocation in functions - especially "hidden" allocation (this function says it's adding 5 to every element, not "allocate array with added 5 to each element". Code should never do surprising things, and allocating memory is a little bit of a surprise if you only asked for adding 5 to each element)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

this is the perfect code

# include <conio.h>
# include <iostream>
using namespace std;

int * add5ToEveryElement(int arr[], int size)
{
    int *theArray = new int[5];
    for(int i=0; i<size; i++)
    {
        theArray[i] = arr[i] + 5;
        cout<<theArray[i]<<endl;
    }
    return theArray;
}

void main()
{
    const int size = 5;
    int noArr[size];
    for(int i=0; i<size; i++)
    {
        noArr[i] = i;
    }
    int *arr = add5ToEveryElement(noArr, size);
    int *p = arr;
    cout<<endl;cout<<endl;
    for(int i=0; i<size; i++)
    {
        cout<<arr[i]<<endl;
    }
    cout<<endl;cout<<endl;cout<<endl;cout<<endl;
    for(int i=0; i<size; i++)
    {
        cout<<*arr<<endl;
        *arr++;
    }
    getch();
    delete[] p;
}
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59