-4

if we take size of array as user input as it is now allowed in new versions of C++ and C, then when will the memory allocated of this array compile time OR run time

#include <stdio.h>
int main(void)
{
  int size, i;
  printf("Enter the size of the arrays:\n");
  scanf("%d", &size);
  int arr1[size];
}
πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
  • 4
    C or C++ choose one. This is not a valid C++. – 273K Jun 04 '22 at 18:40
  • 1
    *the memory allocated of this array compile time* - how do you imagine it is possible? – 273K Jun 04 '22 at 18:42
  • 2
    "As it is now allowed in new versions of C++" This is [not true](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). It is allowed as a GCC _extension_ to C++ that is enabled by default, it is not allowed by C++ per se. – Nathan Pierson Jun 04 '22 at 18:44
  • 1
    In contrast to ISO C, ISO C++ does not support [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Even in ISO C, they are only an optional feature since C11. A compliant compiler is not required to support them. – Andreas Wenzel Jun 04 '22 at 18:44
  • Compile the code and inspect the resulting binary. – Cheatah Jun 04 '22 at 18:55
  • How can the memory be allocated at compile time if the length is user-input? – Roi Jun 04 '22 at 19:17

1 Answers1

1

When the size is unknown at compile time, it is impossible to allocate memory for the unknown size at compile time.

If you allocate memory for a size determined during program execution, whether that is by some method specified by the language standard or by some other method, the memory is allocated during program execution.

Eric Postpischil
  • 168,892
  • 12
  • 149
  • 276