2
How to read a row of integers and store in a dynamically sized array ?
Currently I read the line as string(although the input is only integers) using gets, only way it worked.
The code is like this:
type def struct{char entradas[50];} Processo
int main(){[...] Processo processos; gets(processo[i].entradas); }
And it works, only I need it to be variable this size of inputs[]
Without ever releasing the previous buffer? Beware there! I would recommend using the
realloc
in that case.– Guilherme Bernal
I forgot to use the
free()
thanks for the warning, already corrected. I prefer to use themalloc()
even, I don’t know why else I’ve always had to do more validations with realloc. (from my previous experiences it seemed to me to be less robust than themalloc()
+memcpy()
.– Mansueli
The advantage of
realloc
is that it can find free memory that is right after buffer and increase allocation without creating a new one or copying the data. It will domalloc
+memcpy
+free
only if there is no space. So it will be more efficient.– Guilherme Bernal