Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
7
votes
2 answers

How is realloc implemented in the C standard library?

I can't find any source code for the realloc function and it seems to break the fundamental rule of C: it doesn't take the length of the memory to reallocate. How is memory reallocated without knowing the length of the original memory? How could I…
7
votes
3 answers

Google Sparsehash uses realloc() on type which is not trivially copyable

Consider this simple program: #include #include int main() { google::dense_hash_map map; map["foo"] = 0; } Compiling with GCC 8.2 and -Wclass-memaccess (or -Wall) produces a…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
7
votes
3 answers

Do I need to initiallize(set to 0) memory after calling realloc?

I need to implement a simple dynamic array of pointers to a typedef'ed pointer. Using realloc each time it's requested by the user, the array size will grow by sizeof(pointer). So what I have is this: #include #include…
Chris
  • 3,619
  • 8
  • 44
  • 64
7
votes
1 answer

Can a pthread_mutex_t be moved in memory?

I would like to build a dynamic malloced array of pthread_mutex that will grow over time (adding more mutexes). My question is whether they will still work if the array gets moved with realloc(). My concern is that pthread_mutex_init() might somehow…
pidloop
  • 103
  • 8
6
votes
2 answers

Realloc() does not correctly free memory in Windows

I am attempting to use realloc() in a Windows application. I am allocating a large block of memory, then using realloc() to shrink it down later once I know the correct size. I am finding that although realloc() appears to work correctly (memory in…
asheffie
  • 61
  • 3
6
votes
4 answers

Using Realloc in C

Its really a post for some advice in terms of the use of realloc, more specifically, if I could make use of it to simplify my existing code. Essentially, what the below does, it dynamically allocate some memory, if i goes over 256, then the array…
PnP
  • 3,133
  • 17
  • 62
  • 95
6
votes
3 answers

Freeing a null pointer returned from realloc?

When I have a pointer which should repeatedly be used as argument to realloc and to save it's return value, I understand that realloc won't touch the old object if no allocation could take place, returning NULL. Should I still be worried about the…
Student
  • 708
  • 4
  • 11
6
votes
1 answer

How does realloc() reallocate the memory?

How does realloc() reallocate the memory which was first allocated by malloc()? I know that you need to use malloc() before you´re able to reallocate the memory, but I don´t understand how that really should work. What if a dynamic-memory object…
6
votes
5 answers

Is the poor performance of std::vector due to not calling realloc a logarithmic number of times?

EDIT: I added two more benchmarks, to compare the use of realloc with the C array and of reserve() with the std::vector. From the last analysis it seems that realloc influences a lot, even if called only 30 times. Checking the documentation I guess…
Nisba
  • 3,210
  • 2
  • 27
  • 46
6
votes
4 answers

Correct use of Realloc

This is the way I've been taught to use realloc(): int *a = malloc(10); a = realloc(a, 100); // Why do we do "a = .... ?" if(a == NULL) //Deal with problem..... Isn't that redundant? Can't i just do something like this? : if(realloc(a, 100) ==…
Sir_Playsalot
  • 61
  • 1
  • 3
6
votes
3 answers

What if NULL and size 0 are passed to realloc()?

Is the behavior implementation defined? If NULL and size == 0 are passed to realloc(): int main(void) { int *ptr = NULL; ptr = realloc(ptr, 0); if(ptr == NULL) { printf("realloc fails.\n"); goto Exit; } …
user7375520
  • 273
  • 2
  • 15
6
votes
1 answer

Shrinking with realloc

I encountered this small piece of code in this question, & wanted to know, Can the realloc() function ever move a memory block to another location, when the memory space pointed to is shrinked? int * a = malloc( 10*sizeof(int) ); int * b = realloc(…
user191776
6
votes
8 answers

Passing a dynamic array in to functions in C

I'm trying to create a function which takes an array as an argument, adds values to it (increasing its size if necessary) and returns the count of items. So far I have: int main(int argc, char** argv) { int mSize = 10; ent a[mSize]; int…
Tom
  • 5,835
  • 4
  • 25
  • 30
6
votes
11 answers

How can I make sure that a caller passes a malloc'd pointer?

I have a function which reallocs a pointer given as an argument to a new size. Now, the problem is that - according to the man page - realloc needs a pointer which has been returned by malloc or calloc before. How can I make sure that the caller…
RWS
  • 625
  • 3
  • 12
6
votes
3 answers

How to read unlimited characters in C

How to read unlimited characters into a char* variable without specifying the size? For example, say I want to read the address of an employee that may also take multiple lines.
PrithviRaj
  • 571
  • 1
  • 7
  • 15