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();
}
7
  • @Mat o no sir, its not a duplicate this is the problem i am facing and searching didnt helped me out Commented Dec 23, 2012 at 8:58
  • It's the same problem. You're returning a pointer to an automatic (stack-based) array. Commented Dec 23, 2012 at 8:59
  • well that guy is not using indices like i am doing mine is updated version, not a duplicate :P Commented Dec 23, 2012 at 9:01
  • @Asadullah Is there a reason why you don't use std::vector instead of arrays? Commented Dec 23, 2012 at 9: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 :) Commented Dec 23, 2012 at 9:05

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

6 Comments

int *theArray = new int[5]; This is not making it local ??
@Asadullah No. It's heap-allocated array which will be alive until delete it explicitly. Which is why I updated to free the memory in main.
you are absolutely right. now about memory leak ?? what should i do ??
i must write delete theArray at the end of main ?? or what ??
thank you everyone for your helps. just one last thing, how memory leak damages my system or what happens if memory is leaking ??
|
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)

1 Comment

Now that I read your last paragraph, I totally get it and I totally agree.
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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.