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…
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…
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…
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…
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…
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…
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…
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…
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) ==…
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;
}
…
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(…
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…
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…
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.