0

I am new to C++ programing, I learnt that all the static variables are allocated at compile time on stack memory, so their size should be known before compiling by the compiler.

But the dynamic variables are allocated over heap memory which is very large and compiler doesn't need to know about the size before hand

so I tried out static and dynamic declaration of arrays, and giving them size at runtime but the program is running fine.

So, what's the difference between them apart from fact one is on heap? like both are arrays are of size n only

#include<bits/stdc++.h>
using namespace std;

main(){
  int n; 
  cin>>n;
  
  int arr1[n];
  int *arr2 = new int[n];
  
  for(int i=0; i<n; ++i){ 
    arr1[i] = i;
    arr2[i] = i;
  }
  
  for(int i=0; i<n; ++i){
    cout<<arr1[i]<<" "<<arr2[i]<<endl;
  }
  
}

input: 1000

output:

0 0
1 1
2 2
3 3
.
.
.
.
(so on till)
999 999
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Kaneki
  • 65
  • 5
  • 2
    `int arr1[n];` doesn't actually compile. Add `-pedantic-errors` to you compiler options to stop GCC from compiling this non-standard code. Unlike C, C++ does not have VLA's – NathanOliver Jun 07 '21 at 18:24
  • 1
    and `int arr1[n];` isn't a static array in C anyway. – Weather Vane Jun 07 '21 at 18:27
  • This seems very similar to [this question](https://stackoverflow.com/questions/67875717/what-happens-if-user-defined-sized-arrays-are-not-declared-dynamically-in-c) from earlier today, not to mention the duplicates linked to it. – Nathan Pierson Jun 07 '21 at 18:28
  • You've got stack and heap backwards. The heap is generally used for static storage and the stack for dynamic – Tim Randall Jun 07 '21 at 18:34
  • @NathanOliver I'm pretty sure a stack frame is used for local variables, which are temporary (dynamic), and that `malloc` gives you heap memory – Tim Randall Jun 07 '21 at 18:39
  • @TimRandall The stack is used for all variables with automatic storage duration (on systems which do so), so globals, statics, and local live in the stack. Variable with dynamic storage duration (using `new`/`new[]`) will live in the heap. – NathanOliver Jun 07 '21 at 18:41
  • 1
    Only variables of automatic storage duration would live in the "stack". Variable of static storage duration (including those declared at namespace level and member variables explicitly qualified as static) live in static storage. Those of thread local storage duration live in thread-local storage. Those of dynamic storage duration live on the "heap". – Stephen M. Webb Jun 07 '21 at 18:49
  • Or in hamster wheels and fairy dust if that's what the implementer happened to use for dynamic memory. @Tim [Some good reading on sorting out automatic and dynamic](https://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and) – user4581301 Jun 07 '21 at 19:07
  • I accept that i've got dynamic and automatic confused. I'm still pretty sure that this is wrong. `static variables are allocated at compile time on stack memory` – Tim Randall Jun 07 '21 at 19:13
  • Yeah. `static` variables are [allocated when the program starts](https://en.cppreference.com/w/cpp/language/storage_duration) and not from Automatic storage. – user4581301 Jun 07 '21 at 20:19

0 Answers0