How to use realloc () dynamically in struct allocation?

4

I have a struct TMedidorEletrico *medidor; structure, and it is necessary to reallocate the memory for this structure dynamically until the user closes the loop . Memory must be relocated one step at a time , but when invoking the function realloc() the second time causes the following error:

asked by anonymous 16.09.2015 / 01:52

1 answer

2

The program is relocating but maintaining the same size:

sizeof(struct TMedidorEletrico) * 1

Realloc () does not "add" the size that is passed as the second parameter. This will be the total new size. To achieve the expected result, which is to make room for more elements, realloc should use

sizeof(struct TMedidorEletrico) * (qtdLeitura + 1)
    
16.09.2015 / 03:36