3

I know that stack allocation takes constant time. From what I understand, this happens because the allocation size can be determined in compile time. In this case the program knows how much memory is needed to run (for example) a function and the entire chunk of memory that is needed can be allocated at once.

What happens in cases where the allocation size is only known at run time?

Consider this code snippet,

void func(){
  int n;
  std::cin >> n;

  // this is a static allocation and its size is only known at run time
  int arr[n]; 
}

Edit: I'm using g++ 5.4 on linux and this code compiles and runs.

5
  • 1
    That's not supported by standard C++. You may want to consider std::vector. Commented Jul 22, 2019 at 15:24
  • I tried something similar with g++ and it ran fine. Am I missing something? Commented Jul 22, 2019 at 15:26
  • What happens in cases where the allocation size is only known at run time? - in what question ? Commented Jul 22, 2019 at 15:26
  • 3
    Variable length arrays are not allowed in standard C++. Some compilers (unfortunately) support it as an extension, but I would avoid it, it's not portable. For GCC, enable the -Wvla warning. And use -std=c++17 rather than the gnu++ variant that is used by default and enables language extensions. Commented Jul 22, 2019 at 15:28
  • What you want is std::vector. Commented Jul 22, 2019 at 15:34

4 Answers 4

5

What happens in cases where the allocation size is only known at run time?

Then the program is ill-formed, and therefore compilers are not required to compile the program.

If a compiler does compile it, then it is up to the compiler to decide what happens (other than issuing a diagnostic message, as required by the standard). This is usually called a "language extension". What probably happens is: amount of memory is allocated for the array, determined by the runtime argument. More details may be available in the documentation of the compiler.

Sign up to request clarification or add additional context in comments.

5 Comments

More important, a conforming compiler must issue a diagnostic. If the compiler accepts the code, that diagnostic would typically be a warning.
@PeteBecker In my opinion, the fact that the program is not guaranteed to compile / work is more important to the programmer than the fact that a conforming compiler is required to tell them about it. For a compiler implementer though, it sure would be very important to know this.
For a user who writes code like this, it's important to be told that it's only being accepted because the compiler implements an extension. That would eliminate a bunch of questions here on SO.
@PeteBecker Indeed. That could be achieved by making all compilers conform to the standard. That's why it is important for the compiler implementers to know. What is important for the user of the compiler is what that diagnostic should say: The program is ill-formed, and won't compile without language extension.
Honestly, all of this is important to know no matter who you are. If you're working with C-style arrays in C++, it's important to understand how they work.
5

It is impossible (using standard C++ language) to allocate space on the stack without knowing how much space to allocate.

The line int arr[n]; is not a valid C++ code. It only compiles because the compiler you are using decided to let you do that, so for more information you should refer to your compiler documentation.

For GCC, you might take a look at this page: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

6 Comments

this is possible of course, way depend from compiler. say _alloca for CL
Yes, I should have specified that it is impossible in a way defined by C++ standard. AFAIK, _alloca is a C99 thing.
@RinatVeliakhmedov There is no alloca in standard C. Variable length arrays are in C99 though.
@RinatVeliakhmedov But regardles, I think RbMm's point is that it is possible to allocate space on stack even when the size is known only at runtime. Indeed, it is not possible in standard C++. In fact, it is not possible to express any stack memory allocation in C++; After all, C++ is agnostic to the concept of stack memory entirely.
A conforming compiler must issue a diagnostic for this ill-formed code.
|
2

I'm using g++ 5.4 on linux and this code compiles and runs.

Yes, and this invalid code compiles under MSVC 2010:

int& u = 0;

The standard sais that this code is ill formed. Yet MSVC compiles it! This is because of a compiler extension.

GCC accepts it because it implements it as an extension.

When compiling with -pedantic-errors, GCC will reject the code correctly.

Likewise, MSVC has the /permissive- compiler argument to disable some of it's extensions.

Comments

1

The memory allocation procedure varies when the size to be allocated is determined at runtime. Instead of allocation on stack, memory is reserved on the heap when the size is not known at compile time. Now allocation of memory is possible on the heap until the main memory of the computer is completely used up. Also, in some languages like C,C++ the allocation is permanent and the user is required to deallocate the memory after use.

In the example given above, memory of size n*sizeof(int) is reserved on the heap and is garbage collected (in java or python) or manually deallocated if the memory is assigned a pointer. (in c/c++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.