So, to start off I've already looked at a few questions including this one and none of them seem to help.
I'm simply trying to write a function that extends the size of an array using realloc()
.
My code currently looks like this:
unsigned char *xtnd = malloc(4);
xtndc(&xtnd, 4);
// sizeof(*xtnd) should now be 8
void xtndc ( unsigned char ** bytesRef , uint8_t count ) {
*bytesRef = realloc(*bytesRef, (sizeof(**bytesRef)) + count);
}
But no matter what I do it seems that the size of xtnd
is always 4. After running xtndc()
on it it should now be 8 bytes long.
Any suggestions?
The type of
**bytesRef
isunsigned char
, sosizeof(**bytesRef)
is1
.sizeof
doesn't keep track of dynamic allocations, it's a compile time tool that gives you the size of a type, in this caseunsigned char
.You have to keep track of the array size manually to calculate the new required size.