realloc

Initially mallocate 0 elements to later reallocate and measure size

◇◆丶佛笑我妖孽 提交于 2019-12-01 23:34:29
I have a function that will add a new position to an array by reallocating new memory every time it is called. The problem is that, for each call I need it to add one position to the array, starting from 1 at first call, but I understand that I have to mallocate before reallocating. So my question is, can I initially do something like p = malloc(0) and then reallocate for example using p = (int *)realloc(p,sizeof(int)) inside my function? p is declared as int *p . Maybe with a different syntax? Of course I could make a condition in my function that would mallocate if memory hasn't been

Are multiple realloc more expensive than a huge malloc?

旧街凉风 提交于 2019-12-01 22:34:25
问题 I am using a dynamic array to represent a min-heap. There is a loop that removes minimum, and add random elements to the min-heap until some condition occur. Although I don't know how the length of the heap will change during run-time (there is a lot of randomness), I know the upper bound, which is 10 million. I have two options: 1) Declare a small array using malloc, then call realloc when there number of elements in the heap exceeds the length. 2) Declare a 10 million entry array using

Shrinking with realloc

自古美人都是妖i 提交于 2019-12-01 17:52:16
I encountered this small piece of code in this question , & wanted to know, Can the realloc() function ever move a memory block to another location, when the memory space pointed to is shrinked? int * a = malloc( 10*sizeof(int) ); int * b = realloc( a, 5*sizeof(int) ); If possible, under what conditions, can I expect b to have an address different from that in a ? It's possible for realloc to move memory on any call. True in many implementations a shrink would just result in the change of the reserved size in the heap and wouldn't move memory. However in a heap which optimized for low

Can realloc fail (return NULL) when trimming?

笑着哭i 提交于 2019-12-01 17:05:15
问题 If do the next: int* array = malloc(10 * sizeof(int)); and them I use realloc: array = realloc(array, 5 * sizeof(int)); On the second line (and only it), can it return NULL ? 回答1: Yes, it can. There are no implementation guarantees on realloc() , and it can return a different pointer even when shrinking. For example, if a particular implementation uses different pools for different object sizes, realloc() may actually allocate a new block in the pool for smaller objects and free the block in

Shrinking with realloc

拈花ヽ惹草 提交于 2019-12-01 16:13:06
问题 I encountered this small piece of code in this question, & wanted to know, Can the realloc() function ever move a memory block to another location, when the memory space pointed to is shrinked? int * a = malloc( 10*sizeof(int) ); int * b = realloc( a, 5*sizeof(int) ); If possible, under what conditions, can I expect b to have an address different from that in a ? 回答1: It's possible for realloc to move memory on any call. True in many implementations a shrink would just result in the change of

Using original pointer after realloc?

↘锁芯ラ 提交于 2019-12-01 15:29:57
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 += sizeIncrement); if (newBuffer == NULL) { free (buffer); return NULL; } currentPosition = newBuffer + (currentPosition - buffer); buffer = newBuffer; } I hope the names of the variables are self-explanatory; if context is needed, I will edit to provide the entire chunk of code and not just this excerpt. My question is about the line currentPosition =

How does realloc know how much to copy?

谁说胖子不能爱 提交于 2019-12-01 15:17:43
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 implementation, and realloc doesn't always do free, but when it does, how much does it copy? Edit: Thanks for the answers. But how can I then implement realloc in my code with malloc/free/..? It knows because malloc recorded that information when you called it. After all, the system has to keep track of the sizes of allocated blocks anyway so that it doesn't

Using original pointer after realloc?

旧时模样 提交于 2019-12-01 14:22:32
问题 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 += sizeIncrement); if (newBuffer == NULL) { free (buffer); return NULL; } currentPosition = newBuffer + (currentPosition - buffer); buffer = newBuffer; } I hope the names of the variables are self-explanatory; if context is needed, I will edit to provide the

malloc() of struct array with varying size structs

孤人 提交于 2019-11-30 16:39:10
How does one malloc an array of structs correctly if each struct contains an array of strings which vary in size? So each struct might have a different size and would make it impossible to realloc(numberOfStructs * sizeof(structName)) after malloc(initialSize * sizeof(structName) How does one allocate memory for this and keep track of what is going on? I am making some guesses here, based on the information you have provided. The only reason I can see for wanting to realloc an array of structs is if you want to add more structs to that array. That's cool. There are plenty of reasons to want

What is the correct usage of realloc() when it fails and returns NULL?

自作多情 提交于 2019-11-30 13:46:53
问题 Can anyone summarize what is the correct usage of realloc() ? What do you do when realloc() fails? From what I have seen so far, it seems that if realloc() fails, you have to free() old pointer. Is that true? Here is an example: 1. char *ptr = malloc(sizeof(*ptr) * 50); 2. ... 3. char *new_ptr = realloc(ptr, sizeof(*new_ptr) * 60); 4. if (!new_ptr) { 5. free(ptr); 6. return NULL; 7. } Suppose realloc() fails on line 3 . Am I doing the right thing on line 5 by free() ing ptr ? 回答1: From http:/