Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
13
votes
5 answers

How does realloc know how much to copy?

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…
Rok Povsic
  • 4,626
  • 5
  • 37
  • 53
13
votes
1 answer

Does realloc of memory allocated by C11 aligned_alloc keep the alignment?

Consider the following (C11) code: void *ptr = aligned_alloc(4096, 4096); ... // do something with 'ptr' ptr = realloc(ptr, 6000); Since the memory that ptr points to has a 4096-byte alignment from aligned_alloc, will it (read: is it guaranteed to)…
Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
12
votes
5 answers

Malloc of arrays and structs within a struct

How does one malloc a struct which is inside another struct? I would also like to malloc an array of items inside a struct and then realloc this array when needed, how is this done correctly? Could you please give an example of declaring a struct…
some_id
  • 29,466
  • 62
  • 182
  • 304
12
votes
6 answers

can realloc move pointer if new size smaller?

I am wondering whether the C or C++ standard guarantees that a pointer is not changed when realloc is called with a smaller (nonzero) size: size_t n=1000; T*ptr=(T*)malloc(n*sizeof(T)); //<--do something useful (that won't touch/reallocate ptr of…
spirov
  • 261
  • 4
  • 6
12
votes
2 answers

Is it true, that modern OS may skip copy when realloc is called

While reading the https://stackoverflow.com/a/3190489/196561 I have a question. What the Qt authors says in the Inside the Qt 4 Containers: ... QVector uses realloc() to grow by increments of 4096 bytes. This makes sense because modern operating…
osgx
  • 90,338
  • 53
  • 357
  • 513
11
votes
5 answers

What are the chances that realloc should fail?

Does it fail when it runs out of free memory similar to malloc or could there be other reasons?
user963241
  • 6,758
  • 19
  • 65
  • 93
11
votes
2 answers

C Programming - How often should realloc be used?

I have a question about dynamic memory allocation. Context: I'm writing a program that reads a text file of words and counts the frequency with which each word occurs (one word per line). This particular function reads the file, counts the lines…
LeAnn
  • 121
  • 1
  • 8
11
votes
8 answers

Replacing realloc (C --> C++)

In an earlier question, I asked about typecasting pointers, but was directed to the better solution of using the C++ allocation system instead of mallocs. (I am converting some C code to C++) However, I still have an issue with a similar function: I…
Biosci3c
  • 772
  • 5
  • 15
  • 35
11
votes
2 answers

Using original pointer after realloc?

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 +=…
verbose
  • 7,827
  • 1
  • 25
  • 40
10
votes
6 answers

Is realloc() safe in embedded system?

While developing a piece of software for embedded system I used realloc() function many times. Now I've been said that I "should not use realloc() in embedded" without any explanation. Is realloc() dangerous for embedded system and why?
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
10
votes
8 answers

How much overhead do realloc calls introduce?

I am using realloc in every iteration of a for loop that iterates more that 10000 times. Is this a good practice? Will realloc cause an error if it was called a lot of times?
saber
  • 101
  • 1
  • 3
10
votes
3 answers

Behavior of realloc when the new size is the same as the old one

I'm trying to make a code more efficient. I have something like this: typedef struct{ ... }MAP; MAP* pPtr=NULL; MAP* pTemp=NULL; int iCount=0; while (!boolean){ pTemp=(MAP*)realloc(pPtr,(iCount+1)*sizeof(MAP)); …
H_squared
  • 1,251
  • 2
  • 15
  • 32
10
votes
6 answers

Using realloc (X, 0) instead of free() and using malloc with length of a string +1

So I don't really know how to put the title this time. First of all I'd like to say that I've seen several comments on this page about warning if the question is related to "homework". Mine is, but it's also completed and I just want to further…
keont
  • 653
  • 1
  • 9
  • 26
9
votes
2 answers

Can alloca() memory be reallocated?

Memory allocated by malloc can be reallocated with realloc. Is there a similar function for alloca? Reallocating stack memory could be useful when you don't want memory to be allocated on the heap, and you need to allocate variable stack memory…
Phridge
  • 148
  • 8
9
votes
6 answers

Should I enforce realloc check if the new block size is smaller than the initial?

Can realloc fail in this case? int *a = NULL; a = calloc(100, sizeof(*a)); printf("1.ptr: %d\n", a); a = realloc(a, 50 * sizeof(*a)); printf("2.ptr: %d\n", a); if(a == NULL){ printf("Is it possible?\n"); } return (0); } The output in my…
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118