How to start an array with the value the user type is possible?

Asked

Viewed 272 times

1

If all allocated memory is defined at the time of compilation, then how is this possible:

int num{0};
cin >> num;
int array[num];

From what I understand, this could only be possible if using dynamic memory allocation.

  • Set the size of an array? With user input? Yes it is possible normally, only it is not possible to allocate from that position without allocating the previous ones to it

1 answer

1


First, let’s agree that this array is C style, not C++.

I don’t know what you mean by "defined". If that means it’s in the code, yes, but it’s all in the code. If you’re saying that memory is allocated during compilation, no, that would be impossible.

Maybe there’s a confusion of concepts there.

There is a difference between compile time, which we often call static, and run time, which we call dynamic. Static is set at compile time and does not change. I think that’s what you’re talking about. So if in code you create one with 10 elements, it cannot have anything other than that. The code already knows the value. But its allocation only occurs during execution, nor could it be different, so the allocation is dynamic.

Well, it is possible to have the allocation in advance if it is in static area, which is not the case, yet there is an allocation in the executable load, the allocation does not occur before, it is only simpler and determined in advance.

Now let’s use the same terms in another context, although very close.

When you allocate memory in heap, and only in it (usually through the malloc()) we call dynamic allocation. The dynamic word here cannot be interpreted alone, we are talking about allocation in area dynamic. That is, area that is managed as needed and life time is not determined.

So what’s not in the heap is static? Not exactly. If it is a given that already comes in the executable, the allocation is made in area static, it is already there ready since the compilation, loaded the executable, can access. Generally can not write there.

There is still a area automatic call, which some think is static, and not quite so. This area is represented by stack (typically). The whole area of the stack is in fact static, it is an area that is reserved, and in a way we can talk about allocation, during the executable load, and in general does not change during the whole process execution (or the thread where allocation is made on the thread).

But the data going into it is allocated individually on demand from the code according to the execution of the moment. This allocation is not static. Can we call it dynamic? We can, but not in the same way heap, why it is called automatic, after all life time is determined and you do not need to manage manually.

Both the dynamic area and the automatic can have data allocated as needed, in this sense we call dynamics. Maybe I should have another name, I don’t know, flexible, on demand, or something.

So there’s nothing special about it. You’re allocating the area of the vector to be allocated within the stack at the time of execution.

You need to understand how the battery works to understand all the implications of this. In the link above has information about.

Declaring the variable only indicates that you will use it and that you will need a space for data. This space can be determined in the moment before its use, unless it is already embedded in the executable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.