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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
legendlee
  • 568
  • 4
  • 12
  • 5
    Why don't you just `free`/`malloc` the buffer if you don't care about the old contents? – Mat Jul 14 '11 at 15:45
  • 5
    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. – pmg Jul 14 '11 at 15:48
  • 1
    Sadly, you have to cast it when writing C-esk code in C++. I would bet thats what's happening. – user606723 Jul 14 '11 at 15:54
  • 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. – KANJICODER Jan 13 '20 at 22:31
  • 1
    @Mat: Reducing memory fragmentation would be one reason. – Tara Feb 04 '21 at 05:14
  • What a bummer, I need this. It hugely helps efficiency when making an `insert` function for a. dynamic array, avoiding double copying – user426 Nov 21 '21 at 05:37

3 Answers3

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.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • 5
    @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. – R.. GitHub STOP HELPING ICE Jul 14 '11 at 16:51
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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 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. – legendlee Jul 14 '11 at 16:45
-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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
nos
  • 223,662
  • 58
  • 417
  • 506
  • 7
    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". – legendlee Jul 14 '11 at 16:35
  • @legendlee Such a thing does not exist in standard C. If you don't care about the data anyway, can't you just use malloc() ? – nos Jul 14 '11 at 18:12
  • 2
    @nos 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. – Antonio Oct 05 '16 at 08:52