### Should this array be created in Heap or Stack ?

Dear gurus,

This is about returning an array from a function.
First, in the function, an array is created by allocating space from the heap (line 14 and 18 below).
Then its pointer is returned (line 23).

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include #define LEVEL 10 using namespace std; uint16_t ** get_RGBfreq() { uint16_t ** tempArray; tempArray = new uint16_t * [3]; for (int i=0; i < 3; i++) { tempArray[i] = new uint16_t [ LEVEL + 1]; for (int j=0; j <= LEVEL; j++) tempArray[i][j] = 777; } return tempArray; } int main() { uint16_t ** myRGBfreq = get_RGBfreq(); for (int i=0; i < 3; i++) { for (int j=0; j <= LEVEL; j++) cout << "myRGBfreq[" << i << "][" << j << "] is " << myRGBfreq[i][j] << endl; cout << endl; } return 0; }``````

This program works fine.
Its output is below:

 ```myRGBfreq[0][0] is 777 myRGBfreq[0][1] is 777 myRGBfreq[0][2] is 777 myRGBfreq[0][3] is 777 myRGBfreq[0][4] is 777 myRGBfreq[0][5] is 777 myRGBfreq[0][6] is 777 myRGBfreq[0][7] is 777 myRGBfreq[0][8] is 777 myRGBfreq[0][9] is 777 myRGBfreq[0][10] is 777 myRGBfreq[1][0] is 777 myRGBfreq[1][1] is 777 myRGBfreq[1][2] is 777 myRGBfreq[1][3] is 777 myRGBfreq[1][4] is 777 myRGBfreq[1][5] is 777 myRGBfreq[1][6] is 777 myRGBfreq[1][7] is 777 myRGBfreq[1][8] is 777 myRGBfreq[1][9] is 777 myRGBfreq[1][10] is 777 myRGBfreq[2][0] is 777 myRGBfreq[2][1] is 777 myRGBfreq[2][2] is 777 myRGBfreq[2][3] is 777 myRGBfreq[2][4] is 777 myRGBfreq[2][5] is 777 myRGBfreq[2][6] is 777 myRGBfreq[2][7] is 777 myRGBfreq[2][8] is 777 myRGBfreq[2][9] is 777 myRGBfreq[2][10] is 777 ```

In this case, the function `get_RGBfreq()` returns an array, as expected.

Question
If I want to create the array in the stack (not the heap) instead, by using
`unint16_t tempArray[3][LEVEL]`
(i) Is it possible to return this array from the function?
(ii) If yes, how to write the code?
(iii) Which (using heap or stack) is the better way of doing this, particularly in my case as above?

Note:
I heard we should avoid pointer if there is alternative.
That's why I attempt to use the stack instead of heap.

Thanks in advance.
Last edited on
 (i) Is it possible to return this array from the function?
Where on the stack would this array be?

 (ii) If yes, how to write the code?
If you answered q1, you'd realise the answer is not really.

By declaring the array "on the stack", you mean declaring it locally. You could have a function that populates it, but it would have to accept the array as a parameter.

 (iii) Which (using heap or stack) is the better way of doing this, particularly in my case as above?
If you separate the allocation from the initialisation function, you'll be able to initialise the array the same way irrespective of how it was created.

What is best depends on your design, the scope of the array and your requirement.
Last edited on
Besides what was said sometimes it is a bad idea to declare large arrays in the stack because the memory allocated to the stack is limited.
Thanks, kbw and vlad.
Now it seems obvious that I should use the heap rather than stack for my case.

I imagine, if stack were used, then there must be a copy constructor to copy the array from the stack when the function return the array. Correct..? Then this (copy operation) could be an expensive operation.

Sorry I don't really understand this phrase:
 If you separate the allocation from the initialisation function, you'll be able to initialise the array the same way irrespective of how it was created.

Nevertheless, now I have the idea that I should use the heap in this case.
Thank you very much !
Last edited on
 @activecat Sorry I don't really understand what below means: If you separate the allocation from the initialisation function, you'll be able to initialise the array the same way irrespective of how it was created.

It means the following

 ``12345678910111213141516171819`` ``````void init_RGBfreq( uint16_t myRGBfreq[][LEVEL + 1], size_t n ) { for ( size_t i=0; i < n; i++) { for ( size_t j=0; j < LEVEL + 1; j++) tempArray[i][j] = 777; } } int main() { uint16_t myRGBfreq[3][LEVEL + 1]; init_RGBfreq( myRGBfreq, 3 ); // other suff } ``````

Last edited on
Thanks vlad.

Meanwhile, this phrase hints that it is still possible to do it using stack (although not preferable):
 What is best depends on your design, the scope of the array and your requirement.

In this case, I am trying to make below function works:

 ``123456789101112`` ``````uint16_t XXX get_RGBfreq() { uint16_t tempArray[3][LEVEL]; for (int i=0; i < 3; i++) for (int j=0; j <= LEVEL; j++) tempArray[i][j] = 777; // An arbitrary number for simplicity purpose. return & tempArray[3][LEVEL]; }``````

Of course the above code doesn't compile.
But how to correct it (line 1 and line 11) so that it works accordingly?

Note:
The purpose is to return the array `tempArray[][]` created by the function, where the array is allocated in the stack (not heap).
Last edited on
You may not return a reference to a local array from a function. You was already said that
 If you separate the allocation from the initialisation function, you'll be able to initialise the array the same way irrespective of how it was created.
Oh I see, sorry for misinterpretation.
Thank you very much.
Last edited on
Topic archived. No new replies allowed.