Change the size of the vector during the program?

-1

I want to resize the vector to solve this problem "The Legend of Flavious Josephus":

link

If I have 5 people with a jump of 2 it will look like this:

12345 - > 2 and 4 died and in the next cycle, the 1 and 5 die. 135 - > I want to change or make another vector, with the survivors. 3 - > And finally the last.

I can not manipulate the vector in this way, it's very complex ...

    
asked by anonymous 23.08.2018 / 15:42

1 answer

1

So:

int vetor2[4] = {1, 2, 4, 5};

I know, you're going to say that you have not resized, but created a new one, but that's what you want to do this way, even though you're using an initializer.

Once initialized you can no longer use this syntax, it can only be used in the declaration.

Anything that is stored in the stack (see the end) can not have its size changed because the stack is a continuous data stream.

In theory it is even possible to reduce the size and leave a hole, but also treat the vector as if it had fewer elements, after all, especially in C only accesses an area of memory, is not even accessing in a controlled way the vector itself.

Even nothing prevents you from writing beyond the area reserved for the vector, which would corrupt memory, but it can. It does not mean that it is increasing, but it can write in addition, as if it were greater. Of course, do not do this, it's a serious bug, but the compiler does, it does not control the size most of the time.

The case

But you want to do more, or delete elements in between. Then you have to go through all the vector and whenever one is eliminated you will have to copy the next element to the current position, and do this until you reach the end. At the end they will have elements that no longer need to be copied and no longer need to be accessed. You will need two variables, one to control where you are writing to the vector, and another to control where you are reading. The one of writing will increase by 1 in each step, the other one can increment more times when the element being read should not be copied. Make an algorithm and if you have question post new specific question.

Heap

If you really want to resize it is more appropriate to store in the heap . Not that it is so different and allows you to increase the size freely, or that it decreases without any problem, but it is possible to do it right.

If you do dynamic allocation you can reduce size, but you can leave unused space unused, which makes it virtually unnecessary to do so. In heap you also access memory freely, so you can only access fewer elements. It's more a concept of your code than something controlled by language.

If you want to increase in size you will probably have to change the place object to find a place where the new size fits. And there may be a data copy (in some cases there may be optimization), or you can keep it there if you have enough space.

Dynamic allocation occurs with malloc() . And the size change of the allocation occurs with realloc() , and it controls whether or not to relocate.

A typical example: How to store any string in C? . Note that it is more common to reserve a size and keep it so in general it is simpler and usually works out cheaper in most cases. There is no place for preciousness.

A more complete response was given in What prevents an array from being initialized with a variable length in C? .

A code sample using dynamic allocation: How to create a variable-length vector? .

Understand What are and where are the stack and heap? .

    
23.08.2018 / 16:09