-1

I want to return an array created in a local function through pointers to the main function. My code is below. The array is returned and I am able to access each element by element once only. The next time it is giving garbage values. What is Wrong?

void DoWork(int** ppOut , int& nSize)
{
    int m[5];
    for(int i = 0 ; i < 5 ; i++)
        {
            m[i] = i;
        }

    nSize = 5;
    cout << m[0] << endl;
    cout << m[1] << endl;
    cout << m[2] << endl;
    cout << m[3] << endl;
    cout << m[4] << endl;

    *ppOut = &m[0];

    //delete [] m;
}

void main()
{
    int nSize = -1;
    int i;
    int* f = NULL; 

    DoWork(&f , nSize);

    cout << f[3] << endl;
    cout << f[0] << endl;
    cout << f[2] << endl;
    cout << f[3] << endl;
    cout << f[4] << endl;

    _getch();
}

Output is:-- 0 1 2 3 4 from local function. But in main 3 and rest are grabage values

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 3
    Plenty of undefined behavior, illegal and/or non-standard C++. Your primary problem is that you are storing a pointer to a temporary. `main()` **always** returns `int`. `_getch()` is certainly non-standard (use `std::cin.ignore()` instead). – Dietmar Kühl Dec 29 '13 at 16:36
  • 1
    @OP: why not google this... (no, please **don't tell me** that it's hard to search for. I'm not interested in that nonsense.) –  Dec 29 '13 at 16:43
  • @H2CO3: Because to search on google one needs to understand what to search. – Alok Save Dec 29 '13 at 16:44

2 Answers2

5

The Problem:
The array m is a local array which does not exist beyond the lifetime of the function DoWork(). When you do so what you end up with is Undefined Behavior, which basically means that you can see any observable behavior because the program ceases to be a C++ standard approved program and so it can show(literally) any behavior.

The Solution:
You will need to extend the lifetime of m so that it is still valid even after the function returns. There are number of ways to do this:

  • Create an array before the function & pass a pointer to it to the function.
  • Create a static array inside the function.
  • Use dynamic memory for the array in function (do remember to release it after usage)
  • Use global array which can be poulapted inside the function.

Each has own pros and cons and its more of horses for courses.

On a side note, void main() is not the standard specified prototype for main() it should return an int:

int main()
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • But in main function , i am able to get values from m if it is accessed one at a time...i.e f[0] = 0 , f[4]= 4 if used only one if i use it twice it gives garbage value – user3144247 Dec 29 '13 at 16:38
  • 2
    @user3144247: It's Undefined to use it _at all_. You're just seeing ghost data that way. Read [this](http://stackoverflow.com/a/6445794/560648). – Lightness Races in Orbit Dec 29 '13 at 16:38
3
  1. The main function in a C++ program should be int main
  2. Returning a pointer to a local variable exhibits undefined behavior.
leemes
  • 44,967
  • 21
  • 135
  • 183
Ed Heal
  • 59,252
  • 17
  • 87
  • 127