0

Here is my simple code arrayfunc() should store some numbers in an array, and return the pointer of this array to main function where the content of the array would be printed What is the problem with my code? It only returns the pointer to the first element of the array Any help will be appreciated. Thanks in advance.


   #include <iostream>

   using namespace std;

   //The definition of the function should remain the same
   int* arrayfunc()
   {
    int *array[10];
    array[0] =new int;
    array[1] =new int;
    array[2] =new int;
    array[3] =new int;

    *array[0]=10;
    *array[1]=11;
    *array[2]=12;
    *array[3]=13;

    return  *array;

   }

   int main()
   {


    for(int i=0;i<4;i++)
    cout<<*(arrayfunc()+i)<<endl; 

     return 0;
   }
codeflow
  • 13
  • 1
  • 5
  • 1
    What you have here is an array of `int` pointers, and not a pointer to an array of integers. http://stackoverflow.com/questions/859634/c-pointer-to-array-array-of-pointers-disambiguation – Igor Dec 25 '11 at 13:10

5 Answers5

3

(1) You should allocate your array with new if you want to return it: int* array = new int[10]; [assuming here you want array of ints and not array of int*'s]
(2) to return the pointer to the first element in the array, use return array and not return *array
(3) your array is array of pointers, and not array of ints.

amit
  • 175,853
  • 27
  • 231
  • 333
  • it is already allocated with new! return array does not work here – codeflow Dec 25 '11 at 13:12
  • @codeflow: no, each array's *item* is allocated with `new`, but not the _whole_ array. – Vlad Dec 25 '11 at 13:16
  • 1
    @codeflow: elements are allocated with new, you should allocate the **array** itself with new: `int* array = new int[10];` I'll add it to my answer. – amit Dec 25 '11 at 13:16
  • if the array is exceeded 10 elements we should deallocate and allocate a new array – codeflow Dec 25 '11 at 13:23
  • @codeflow: That is a different question, you should first check if your current problem is solved, try it out, and if you are having more problems - ask a new question with the new code and new problem. – amit Dec 25 '11 at 13:24
1

Your array is allocated on stack, so as soon as the function returns, it's freed. So you want to return a pointer to a dead memory.

But you are not doing that, you are just returning the valid (copy of) value of the 0th array item.

So, what you have to do:

  1. The best idea would be to switch to stl containers. You should be using std::vector or something like that.
  2. If you stick to the idea of manual memory management, you have to allocate the array on heap, return it from the function, and perhaps deallocate it in the caller.

Edit:
basically you want the following:

using namespace std;

vector<int> arrayfunc()
{
    vector<int> v;
    v.push_back(10);
    ...
    return v;
}

...

vector<int> result = arrayfunc();
cout << result[0] << ...

This would be the right C++ way.

(Nitpicking:) You don't need to care about copying the vector, because of the RVO used by all modern C++ compilers.


Allocating an array on heap should be simple, too:

int* array = new int[4];
array[0] = 10;
...
return array;

...
int* array = arrayfunc();
...
delete[] array;

But I would strongly advise to take the former approach (with vector).

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • No, actually he is returning a pointer to the first int, which was allocated dynamically, since his array is an array of pointers. – amit Dec 25 '11 at 13:04
  • But I allocated it with new, how it could be on stack? – codeflow Dec 25 '11 at 13:05
  • I want to access all the elements in the array not only the first one – codeflow Dec 25 '11 at 13:06
  • @amit: that is basically what I wrote: 0th array item is the pointer, since the array is array of pointers. – Vlad Dec 25 '11 at 13:07
  • @Vlad: My comment was referring to your answer before you editted it. it included only `Your array is allocated on stack, so as soon as the function returns, it's freed. So you want to return a pointer to a dead memory.`, which is wrong, because he is returning a pretty valid memory, but not what he intended to. – amit Dec 25 '11 at 13:07
  • @amit: yes, that was incorrect, that's why I changed it. Well, "you want to" is still correct, because that was the OP's intention. – Vlad Dec 25 '11 at 13:08
  • I am actually tried to allocate the array on heap in this function but it is seems like I failed – codeflow Dec 25 '11 at 13:15
  • Thanks but I cant use STL here :) – codeflow Dec 25 '11 at 13:19
0

This codes seems wrong to me in several levels.

  1. Never return an internal variable of a function. The variable array is only defined in the function, so it should never be returned outside.

  2. Why do you allocate each int by itself with new? I would allocate the entire array at once. If you know the array length and it's constant, consider having it defined statically.

talkol
  • 12,564
  • 11
  • 54
  • 64
0

http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx

apocalypse
  • 5,764
  • 9
  • 47
  • 95
0

Just try return array; instead of return *array;

Abed Hawa
  • 1,372
  • 7
  • 18
  • I'm sure it will fail not because of this :), he has to do some initializations – Abed Hawa Dec 25 '11 at 13:11
  • @mrabooode - look at the type of `array`. It is **not** an array of `int` – D.Shawley Dec 25 '11 at 13:15
  • I'm sorry for the misunderstanding, but I was thinking if it was like `return array` it will return the address of the first pointer in the array, and outside the function he may iterate by incrementing that address, he just need to initialize the array. – Abed Hawa Dec 25 '11 at 13:21
  • 1
    Problem is, arrays allocated inside a function are on the stack. When the function return, the array goes invalid and filled with garbage potentially, BOOM a segfault. –  Jul 28 '12 at 04:55