8

I would like to know how a variable length array is managed (what extra variables or data structures are kept on the stack in order to have variable length arrays).

Thanks a lot.

3 Answers 3

4

It's just a dynamically sized array (implementation-dependent, but most commonly on the stack). It's pretty much like alloca in the old days, with the exception that sizeof will return the actual size of the array, which implies that the size of the array must also be stored somewhere (implementation-dependent as well, but probably on the stack too).

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

2 Comments

That's not entirely true, because there was no way to interrogate the size of an alloca() array after it had been created, but sizeof works on VLAs (so if you do call sizeof on the array, that size must be kept somewhere).
@caf: good point - I guess the size also needs to be stored somewhere (implementation-dependent) then - I'll update my answer.
1

The size of variable length arrays is determined on run-time, instead of compilation time.
The way it's managed depends on the compiler.
GCC, for instance, allocates memory on the stack.
But there is no special structure. It's just a normal array, whose size is known at run-time.

Comments

-2

alternatively you could use some containers, e.g. ArrayList in java or vector in c/c++

1 Comment

-1 not anything to do with the question (how is a feature of C99 might be implemented) .

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.