Why is there no function in standard C library like realloc() without data copying?
Asked Answered
R

3

8

For example, I want such a function:

char *dst = (char*)malloc(512);
char *src = (char*)malloc(1024);
...
dst = (char*)realloc(dst, 1024);
memcpy(dst, src, 1024);

As you see, I just want the function realloc() to extend the size of buffer, but the realloc() in C library may copy data from old address. So is there a function in any library like what I want?

Religieux answered 14/7, 2011 at 15:43 Comment(6)
Why don't you just free/malloc the buffer if you don't care about the old contents?Beeman
Don't cast the return value of malloc(). It is redundant at best, and may hide an error the compiler would have caught in its absence.Thundersquall
Sadly, you have to cast it when writing C-esk code in C++. I would bet thats what's happening.Asymmetric
I get why the memory cannot be reliably EXTENDED. But what about reliably SHRINKING the allocation? For example, I overallocate a pool of memory, and when the program reaches homeostasis, I re-alloc the pool to a smaller size.Achlorhydria
@Mat: Reducing memory fragmentation would be one reason.Haemato
What a bummer, I need this. It hugely helps efficiency when making an insert function for a. dynamic array, avoiding double copyingHeadman
E
2

realloc attempts do extend the buffer without copying, but can only do that if the extra space is free.

In your case, you just allocated space for src and that memory block just might have used the space realloc would have needed. In that case it can only allocate a larger block somewhere else and copy the data to that block.

Extender answered 14/7, 2011 at 15:57 Comment(1)
Thank you. Yes, you are right. Actually what I mean is like...use HeapReAlloc() with flag=HEAP_REALLOC_IN_PLACE_ONLY to try to extend the buffer size, if failed, just alloc a new piece without copying data. You know HeapReAlloc() is OS API and I don't want to deal with heap. so I think maybe there should be a function like what I want in C library.Religieux
D
3

Why not just:

free(dst);
dst = malloc(1024);

Also note that realloc may move the block as well as resizing it, so holding an old pointer returned by a previous call to malloc, calloc or realloc may no longer refer to the same chunk.

Davin answered 14/7, 2011 at 15:47 Comment(1)
@tristopia: realloc does not lose the old pointer if reallocation fails. Rather, broken calling code that doesn't check for failure before overwriting the old pointer is what can cause it to be lost.Harvest
E
2

realloc attempts do extend the buffer without copying, but can only do that if the extra space is free.

In your case, you just allocated space for src and that memory block just might have used the space realloc would have needed. In that case it can only allocate a larger block somewhere else and copy the data to that block.

Extender answered 14/7, 2011 at 15:57 Comment(1)
Thank you. Yes, you are right. Actually what I mean is like...use HeapReAlloc() with flag=HEAP_REALLOC_IN_PLACE_ONLY to try to extend the buffer size, if failed, just alloc a new piece without copying data. You know HeapReAlloc() is OS API and I don't want to deal with heap. so I think maybe there should be a function like what I want in C library.Religieux
T
-1

So is there a function in any library like what I want?

No there is not. How is the system supposed to extend the piece of memory if there isn't room ?

e.g. imagine you do this:

char *a = malloc(2);
char *b = malloc(2);

The memory allocated might now look like:

1   2   3   4   5   6   7
-------------------------
|   |   |   |   |   |   |
-------------------------
\      /\       /\      /        
 \    /  \     /  \    /
   v        v        v
 memory   unused/   memory
 for "a"  internal   for "b"
         memory piece

Now you do realloc(a,10); . The system can't simply extend the memory piece for "a". It'll hit the memory used by "b", so instead it has to find another place in memory that has 10 contiguous free bytes , copy over the the 2 bytes from the old memory piece and return you a pointer to these new 10 bytes.

Testate answered 14/7, 2011 at 16:5 Comment(3)
Yes, you are right, so the "move" operation is often carried out. But what I want the system to do is "if you could not extend the piece a, just allocate a new piece, but don't copy the data, I don't need it".Religieux
@Religieux Such a thing does not exist in standard C. If you don't care about the data anyway, can't you just use malloc() ?Testate
@Testate You would need first a free, then a malloc. While with this hypothetical operation you would first try to extend if possible the memory allocated without freeing it, which could be more efficient.Dungaree

© 2022 - 2024 — McMap. All rights reserved.