how does realloc know the size of original data?
void *realloc(void *ptr, size_t size);
So, if the implementation is like this:
temp = malloc(size);
memcpy(.. // How much to copy?
free(ptr);
return temp;
I realize this is not the original…
Consider the following (C11) code:
void *ptr = aligned_alloc(4096, 4096);
... // do something with 'ptr'
ptr = realloc(ptr, 6000);
Since the memory that ptr points to has a 4096-byte alignment from aligned_alloc, will it (read: is it guaranteed to)…
How does one malloc a struct which is inside another struct?
I would also like to malloc an array of items inside a struct and then realloc this array when needed, how is this done correctly?
Could you please give an example of declaring a struct…
I am wondering whether the C or C++ standard guarantees that a pointer is not changed when realloc is called with a smaller (nonzero) size:
size_t n=1000;
T*ptr=(T*)malloc(n*sizeof(T));
//<--do something useful (that won't touch/reallocate ptr of…
While reading the https://stackoverflow.com/a/3190489/196561 I have a question. What the Qt authors says in the Inside the Qt 4 Containers:
... QVector uses realloc() to grow by increments of 4096 bytes. This makes sense because modern operating…
I have a question about dynamic memory allocation.
Context: I'm writing a program that reads a text file of words and counts the frequency with which each word occurs (one word per line).
This particular function reads the file, counts the lines…
In an earlier question, I asked about typecasting pointers, but was directed to the better solution of using the C++ allocation system instead of mallocs. (I am converting some C code to C++)
However, I still have an issue with a similar function:
I…
I was reading Richard Reese's new (May 2013) O'Reilly book "Understanding and Using C Pointers", and I have a question about some code therein, on page 87.
if (++length > maximumLength) {
char *newBuffer = realloc (buffer, maximumLength +=…
While developing a piece of software for embedded system I used realloc() function many times. Now I've been said that I "should not use realloc() in embedded" without any explanation.
Is realloc() dangerous for embedded system and why?
I am using realloc in every iteration of a for loop that iterates more that 10000 times.
Is this a good practice? Will realloc cause an error if it was called a lot of times?
I'm trying to make a code more efficient. I have something like this:
typedef struct{
...
}MAP;
MAP* pPtr=NULL;
MAP* pTemp=NULL;
int iCount=0;
while (!boolean){
pTemp=(MAP*)realloc(pPtr,(iCount+1)*sizeof(MAP));
…
So I don't really know how to put the title this time. First of all I'd like to say that I've seen several comments on this page about warning if the question is related to "homework".
Mine is, but it's also completed and I just want to further…
Memory allocated by malloc can be reallocated with realloc. Is there a similar function for alloca? Reallocating stack memory could be useful when you don't want memory to be allocated on the heap, and you need to allocate variable stack memory…
Can realloc fail in this case?
int *a = NULL;
a = calloc(100, sizeof(*a));
printf("1.ptr: %d\n", a);
a = realloc(a, 50 * sizeof(*a));
printf("2.ptr: %d\n", a);
if(a == NULL){
printf("Is it possible?\n");
}
return (0);
}
The output in my…