Change the vector size during the course of the program?

Asked

Viewed 505 times

-1

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

https://www.urionlinejudge.com.br/judge/pt/problems/view/1030

If I have 5 people with a 2 jump it will be as follows:

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

I can’t manipulate the vector that way, it’s very complex...

  • This is dynamic memory allocation, in doing this can not define vetor[6] has to die as *vetor

  • 2
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

2 answers

1

Thus:

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

I put in the Github for future reference.

I know, you’re going to say you didn’t change the size, but you did create a new one, but that’s what you want to do this way, including because you’re using an initializer.

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

Everything that is stored in the stack (see at the end) cannot have its size changed because the stack is a continuous data sequence.

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

Even nothing prevents writing beyond the area reserved for the vector, which would corrupt the memory, but can. It doesn’t mean it’s getting bigger, but you can write over it, like it’s bigger. Of course, don’t do that, it’s a serious mistake, but the compiler lets it, it doesn’t control the size most of the time.

The case

But you want to do more, you want to eliminate elements in the middle. Then you will have to go through the whole vector and whenever one is deleted you will have to copy the next element to the current position, and do this until you reach the end. In the end, they will have elements that no longer need to be copied or accessed. You will need two variables, one to control the position where you are writing on the vector, and the other to control where you are reading. The write will increase by 1 in each step, the other can increase more often when the element being read should not be copied. Make an algorithm and if you have doubt post new specific question.

Heap

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

If you make dynamic allocation you can reduce the size, but you can leave unused space unused, which makes it virtually unnecessary to do so. In the 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 may have to change the object of place 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 change in size of the allocation occurs with realloc(), and he controls whether he needs to move or not.

A typical example: How to store any string in C?. Note that it is more common to reserve a size and keep it this way in general is simpler and usually comes out cheaper in most cases. There’s no place for precious.

A more complete reply was given on This prevents an array from being initialized with a variable size in C?.

An example of code using dynamic allocation: How to create a vector of variable size?.

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

-2

If you are not limited to C, you can use the C++ vector library. It works dynamically as well as list, which is another option for solving this problem.

  • 2

    It seems that this fits more like a comment in the question than as an answer itself.

Browser other questions tagged

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