Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1483 questions
89
votes
5 answers

How do you 'realloc' in C++?

How can I realloc in C++? It seems to be missing from the language - there is new and delete but not resize! I need it because as my program reads more data, I need to reallocate the buffer to hold it. I don't think deleteing the old pointer and…
bodacydo
  • 63,809
  • 83
  • 206
  • 303
53
votes
3 answers

Does std::vector *have* to move objects when growing capacity? Or, can allocators "reallocate"?

A different question inspired the following thought: Does std::vector have to move all the elements when it increases its capacity? As far as I understand, the standard behaviour is for the underlying allocator to request an entire chunk of the…
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
53
votes
9 answers

What does malloc(0) return?

What does malloc(0) return? Would the answer be same for realloc(malloc(0),0)? #include #include int main() { printf("%p\n", malloc(0)); printf("%p\n", realloc(malloc(0), 0)); return 0; } Output from Linux…
manav m-n
  • 10,236
  • 21
  • 66
  • 95
40
votes
4 answers

Does realloc overwrite old contents?

When we reallocate memory via realloc(), are the previous contents over-written? I am trying to make a program which reallocates memory each time we enter the data into it. Please tell me about memory allocation via realloc, is it compiler…
user379888
40
votes
3 answers

Realloc on NULL-valued (or undefined) pointer

I was reading about realloc and got confused about a point mentioned there. Consider the code below: #include #include int main () { int* ptr = NULL; ptr = realloc(ptr, 10*sizeof(int)); return 0; } Is there any…
user1607425
39
votes
2 answers

Do we lose data in a buffer after realloc'ing?

I'm having troubles understanding how realloc works. If I malloc'ed a buffer and copied data to that buffer, let's say "AB": +------------+ | A | B | \0 | +------------+ then I realloc'ed the buffer, will there be any lost in the data (even a…
user1129665
37
votes
9 answers

Is it safe to realloc memory allocated with new?

From what is written here, new allocates in free store while malloc uses heap and the two terms often mean the same thing. From what is written here, realloc may move the memory block to a new location. If free store and heap are two different…
Jan Turoň
  • 26,696
  • 21
  • 102
  • 153
36
votes
5 answers

Why is there no reallocation functionality in C++ allocators?

In C the standard memory handling functions are malloc(), realloc() and free(). However, C++ stdlib allocators only parallel two of them: there is no reallocation function. Of course, it would not be possible to do exactly the same as realloc(),…
doublep
  • 25,014
  • 8
  • 67
  • 82
36
votes
2 answers

What header should I include for memcpy and realloc?

I am porting a project to the iPhone and it uses realloc and memcpy which are not found. What is the header to include? It's a project mixing Objective C and C++ and I am starting to be lost. Thanks in advance for your help!
AP.
  • 4,945
  • 7
  • 43
  • 94
34
votes
5 answers

Can I assume that calling realloc with a smaller size will free the remainder?

Let’s consider this very short snippet of code: #include int main() { char* a = malloc(20000); char* b = realloc(a, 5); free(b); return 0; } After reading the man page for realloc, I was not entirely sure that the…
qdii
  • 11,387
  • 7
  • 54
  • 107
33
votes
5 answers

Using realloc to shrink the allocated memory

Simple question about the realloc function in C: If I use realloc to shrink the memory block that a pointer is pointing to, does the "extra" memory get freed? Or does it need to be freed manually somehow? For example, if I do int *myPointer =…
Moshe Rosenschein
  • 333
  • 1
  • 3
  • 5
31
votes
3 answers

Dynamic array in C — Is my understanding of malloc and realloc correct?

I am learning how to create dynamic 1D arrays in C. The code below tries to do the following: Using malloc, create a dynamic array of length 10, that holds values of type double. Set each entry of the array to j/100 for j = 0, 1,..., 9. Then print…
Legendre
  • 3,008
  • 7
  • 28
  • 44
29
votes
4 answers

Does realloc free the former buffer if it fails?

If realloc fails and returns NULL is the former buffer free'd or it is kept intact? I didn't found that particular piece of information in the man page and I'm quite unsure what to do. If memory is freed then double-free could be risky. If not then…
ja.ro
  • 416
  • 4
  • 5
27
votes
8 answers

How do realloc and memcpy work?

I have two questions. Do realloc() and memcpy() copy the entries in an array to another in a way faster than just iterating on each element O(N) ? If the answer is yes then what do you think is its complexity ? If the size allocated is smaller than…
Ahmed Mounir
  • 1,292
  • 2
  • 12
  • 19
22
votes
3 answers

How `realloc` work actually in the background?

How realloc work actually in the background? If there is not enough memory available at old place does this one allocating two/many memory blocks and one pointer pointing to that and other are internally linked with each other or the old region…
Ravindra Gupta
  • 478
  • 2
  • 6
  • 18
1
2 3
98 99