Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
28
votes
6 answers

Proper usage of realloc()

From man realloc:The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. So in this code snippet: ptr = (int *)…
user3163420
  • 295
  • 1
  • 3
  • 6
24
votes
8 answers

How to handle realloc when it fails due to memory?

Question says it all but here is an example: typedef struct mutable_t{ int count, max; void **data; } mutable_t; void pushMutable(mutable_t *m, void *object) { if(m->count == m->max){ m->max *= 2; m->data =…
Nick Van Brunt
  • 15,244
  • 11
  • 66
  • 92
21
votes
7 answers

Aligned memory management?

I have a few related questions about managing aligned memory blocks. Cross-platform answers would be ideal. However, as I'm pretty sure a cross-platform solution does not exist, I'm mainly interested in Windows and Linux and to a (much) lesser…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
20
votes
1 answer

Is there really no version of realloc() supporting alignment?

There exist several aligned versions of the venerable malloc(), e.g.: #include int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include void…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
20
votes
5 answers

How to update other pointers when realloc moves the memory block?

The realloc reference says: The function may move the memory block to a new location, in which case the new location is returned. Does it mean that if I do this: void foo() { void* ptr = malloc( 1024 ); unsigned char* cptr =…
zajcev
  • 303
  • 2
  • 7
17
votes
3 answers

realloc(): invalid next size when reallocating to make space for strcat on char *

I am getting invalid memory error on following code: printf(" %s\n","FINE 5"); printf("%s LENGTH IS: %d\n","FINE 6",strlen(": ")); buffer = (char *)realloc(buffer, strlen(buffer)* sizeof(char) + (strlen(": ")+1)* sizeof(char)); printf(" %s\n","FINE…
PUG
  • 4,301
  • 13
  • 73
  • 115
17
votes
3 answers

Using realloc in c++

std::realloc is dangerous in c++ if the malloc'd memory contains non-pod types. It seems the only problem is that std::realloc wont call the type destructors if it cannot grow the memory in situ. A trivial work around would be a try_realloc…
deft_code
  • 57,255
  • 29
  • 141
  • 224
17
votes
4 answers

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

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 =…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
17
votes
2 answers

Why is there no "recalloc" in the C standard?

Everyone knows that: realloc resizes an existing block of memory or copies it to a larger block. calloc ensures the memory is zeroed out and guards against arithmetic overflows and is generally geared toward large arrays. Why doesn't the C…
Matt
  • 21,026
  • 18
  • 63
  • 115
16
votes
4 answers

Is it safe to use realloc?

Some time ago a friend of mine told me not to use realloc because it's unsafe, but he couldn't tell me why, so I made some research on the subject and the nearest references to my doubt were: First Second I want to know if I can continue to use…
k3oy
  • 531
  • 2
  • 6
  • 19
16
votes
7 answers

Freeing allocated memory: realloc() vs. free()

so I have a piece of memory allocated with malloc() and changed later with realloc(). At some point in my code I want to empty it, by this I mean essentially give it memory of 0. Something which would intuitively be done with realloc(pointer,0). I…
user3021085
  • 407
  • 2
  • 5
  • 16
15
votes
4 answers

git out of memory on checkout

I have cloned a large repo and got an error (after several attempts) Clone succeeded, but checkout failed When trying to fix this with git checkout -f HEAD an error comes back Fatal: Out of memory, realloc failed2 I've already set some memory…
PeterSG
  • 151
  • 1
  • 5
14
votes
7 answers

simple c malloc

While there are lots of different sophisticated implementations of malloc / free for C/C++, I'm looking for a really simple and (especially) small one that works on a fixed-size buffer and supports realloc. Thread-safety etc. are not needed and my…
Thomas
  • 1,001
  • 1
  • 10
  • 20
14
votes
7 answers

Can realloc fail (return NULL) when trimming?

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?
Novak
  • 2,760
  • 9
  • 42
  • 63
13
votes
5 answers

What would realloc do if there is no sequential space of memory?

realloc is used to reallocate the memory dynamically. Suppose I have allocated 7 bytes using the malloc function and now I want to extend it to 30 bytes. What will happen in the background if there is no sequential (continously in a single row)…
Sambhav jain
  • 882
  • 2
  • 12
  • 16