-
re-calloc???
Help! I need to reallocating memory then set the entire/partial buffer into specific value.
calloc only works for allocating memory, not reallocating memory
and memset only works for char data type... T_T...
Should I do:
Code:
buffer <- malloc(5)
for i <- 0 to 4
buffer[i] <- 2
end for
; do something with buffer[i]
buffer <- realloc(10)
for i <- 5 to 9
buffer[i] <- 2
end for
Or any std function out there?
Btw, we should prefer to use std function instead of our function because they are faster sometime.
-
memset will work with any pointer.
-
Yeah. You can use realloc() to do your job and set the buffer to whatever value
-
Some addition,...
This will works only for 0 value.
Code:
long long *int64;
//malloc and do something like above (previous post)
int64 = realloc(int64, 10 * sizeof(long long));
memset(int64, 0, 10 * sizeof(long long));
-
Yes, memset only works on arrays of bytes, that's true.
If you want to assign specific pointer, float or struct values, then you need a loop.
-
from what i hear, realloc cannot be used to downsize the array.
although i have seen it happen quite often.
-
It can, there's just no reason to think the memory usage will actually become less. As it's probably more efficient for the OS just to leave you with a bit more than you want.