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

1

If all allocated memory is defined at compile time, then how is this possible:

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

As far as I understand, this could only be possible if you use dynamic memory allocation.

    
asked by anonymous 19.06.2018 / 15:14

1 answer

1

First let's agree that this array is C style and not C ++.

I do not know what you mean by "set." If this means it's in the code, yes, but everything is in the code. If you are saying that memory is allocated during compilation, no, this would be impossible.

There may be a confusion of concepts there.

There is the difference between compile time, which we often call static, and runtime that 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 create one with 10 elements, it can not have anything other than this. 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, however there is an allocation in the load of the executable, the allocation does not occur before, it is only simpler and determined in advance. / p>

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

When you allocate memory in the heap , and only in it (usually through malloc() ) we call dynamic allocation. The word dynamic here can not be interpreted by itself, we are talking about dynamic area allocation . That is, an area that is managed according to need and life time is not determined.

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

There is still an area called an automatic, which some people think is static, and not quite so. This area is represented by the stack (typically). The whole area of the stack is in fact static, it is an area that is reserved, and somehow we can speak in allocation, during the loading of the executable, and in general does not change during all process execution (or thread where the allocation is made to the thread load).

but the data that goes into it is allocated individually on demand of the code according to the execution of the moment. This allocation is not static. Can we call it dynamic? Until we can, but not in the same way as heap , so it's called automatic, after all the life time is determined and you do not need to manage manually.

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

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

You need to understand how the stack works to understand all of the implications of this. In the link above you have information about.

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

    
19.06.2018 / 15:37