I have to dynamically increase a length of double array. I know, how to do it with char array, so I tried this:
int main() {
char * tmp = NULL;
for (int i = 1; i <= 4; i++) {
tmp = realloc(tmp, i * sizeof(char));
tmp[i] = 'i';
}
puts("char OK");
double * tmp1 = NULL;
for (int i = 1; i <= 4; i++) {
tmp1 = realloc(tmp1, i * sizeof(double));
tmp1[i] = 0;
}
return 0;
}
The first array works fine. But the second one crushes with message realloc(): invalid next size
.
These are my 2 questions:
UPD: removed a typo
TL;DR: Both the snippets are wrong, the first one appears to work because of undefined behavior.
To elaborate, the problem is with your indexing logic. C uses a 0-based indexing. So, inside the loop which is staring the iteration from value of i
as 1
, by using
tmp[i] = .......
you're trying to access invalid memory, at this point, only access up to tmp[i-1]
is valid.
You need to use tmp1[i-1] = 0;
, and likewise.
That said,
Never use the form
pointer = realloc (pointer, ......)
because, in case realloc
call fails, you'll end up losing the original pointer, too.
Quoting C11
, chapter §7.22.3.5
The
realloc
function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.
and
[....] If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.
realloc()
,