/ Published in: C
Realloc memory at every X steps. Useful when allocating fixed number of items (in this case chars).
Expand |
Embed | Plain Text
char *variable; int size = 120, tmpsize = 0; variable = (char *)malloc(size*sizeof(char)); while ( ... ) { //Realloc memory tmpsize += 40; if (tmpsize > size) { size += 120; variable = (char *)realloc(variable, size*sizeof(char)); } strcat(variable,"your string to add with a fixed size"); }
You need to login to post a comment.
