1

In function fun, I am allocating array A. The size of array is known at run time, I am wandering from which area A is getting memory. My wild guess is stack but I couldn't think of any reason for it.

#include<iostream>
using namespace std;


void fun(int n)
{
   int A[n];

    //do something with array

   for(int i=0;i<n; i++)
     cout<<A[i]<<" ";
   cout<<"\n";

}

int main()
{
  int n;
  cin>>n; 
  fun(n);
  return 0;

}
rgaut
  • 3,159
  • 5
  • 25
  • 29

1 Answers1

4

"Where does following array getting memory from?"

From the function local stack of fun().

Note:
Allocation of stack local arrays using a variable is a compiler (standards version) specific extension. The common term for the extension is VLA (variable length array) and is supported by several compilers (e.g. GCC g++), and also demanded by some c standard definitions.

See here for more clarifications please: Does C++ support Variable Length Arrays?

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190