Malloc

The malloc function performs dynamic memory allocation in C and is part of the standard library

Realloc

C library function for reallocating a dynamically allocated memory region.



Pointer call knows

Example

"This is evidently what happened in your example since your realloc call asks for less memory than was allocated by calloc and we can see that it didn t do that here since the address of ptr when free is called is the same as the block allocated by calloc;if the existing allocation is much bigger than the request realloc may choose to copy the existing memory block into a smaller allocation possibly acquired with malloc"

from question  

Intel pin: how to detect realloc size

"The runtime is complaining about an invalid pointer which indicates that the pointer you are passing to realloc is not a pointer that was created with a call to malloc or to calloc;it knows because whenever you do a malloc there is a memory management header that is part of the data area and the pointer you are given is a pointer to allocated memory after the header"

from question  

Realloc (again)

Faster memcpy free

Example

"A realloc can occur significantly faster than a malloc memcpy and free"

from question  

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

"That s why realloc can temporarily require more memory than a malloc free pair"

from question  

Why is realloc eating tons of memory?

Others

Example

I mean i think realloc is way smarter so why do we even need malloc

from question  

Why am I getting a segmentation fault when using realloc()?

Malloc often gives you more memory that you ask and stores the actual value in a special location that realloc can access at a later time

from question  

Realloc simply not diong anything, not erroring

For that you have realloc;with malloc you would end up with a fixed size for your table just like if you had only declared it has static but with later initialization well i m a bit agains this sentence because malloc is much more than that but for this purpose this purpose is safe to say this purpose

from question  

Using 2D array of char pointer in C

In the case of overflow a free malloc pair costs less than realloc because of its internal hidden memcpy

from question  

Substitute or workaround for asprintf on AIX

But if it works with large datasets the users will notice that using the malloc -only program slows down other programs much more than the realloc -using program with the same data

from question  

Why should we use `realloc` if we need a `tmp buffer`

If you work with data that doesn t need construction destruction and requires reallocations a large array of ints then i believe malloc free is a good choice as malloc gives you realloc which is way faster than new-memcpy-delete it is on my linux box but i guess this may be platform dependent

from question  

In what cases do I use malloc and/or new?

As a result if one wants code to be compatible with aggressive compilers one must refrain from having any pointers point within an object which is going to be realloc ed unless one can guarantee that such pointers will never even be examined after a successful realloc takes place;as far as the c standard is concerned the behavior of realloc in all cases where it succeeds is equivalent to copying the memory block to some arbitrary location calling free upon it calling malloc to create a new block of the requested size and returning a pointer to the new block in cases where the malloc wouldn t succeed realloc is defined to return null without disturbing the original block

from question  

After using realloc the next pointer in array is lost

If no storage is allocated or deallocated between the malloc and realloc the size of the realloc is known when the malloc is performed and the realloc size is larger than the malloc size then it may make sense to consolidate the malloc and realloc operations into a single larger allocation

from question  

Are compilers allowed to optimize out realloc?

Realloc is worse than malloc in that you will need to have the old and new pointers valid during the realloc

from question  

Is realloc() safe in embedded system?

The justification of realloc is that it s faster than 2nd malloc manual copy free

from question  

C++ realloc performance vs malloc

Also i should note that in same cases you don t have to do a copy even when realloc increases the size for example if the next block in the heap is free;malloc does not initialize memory to zero

from question  

Implementation of Realloc in C

At the moment it s possible that there is some space after stringarray into which you are storing the wordlength-arraylength extra pointers when you calloc them and realloc doesn t move stringarray;it s quite probable that 0xb49010 is one of the pointers you calloc d and you re overwritten the memory where malloc keeps its block size

from question  

Reallocate a 2d char array

However functions like realloc do not have this property as they can return a pointer to storage containing pointers;as you pointed out yourself nonnull and malloc are just macros

from question  

Unfamiliar function declaration in c

Allocate the block using malloc not new and then use realloc;realloc knows how much free space is available after the block for expansion

from question  

Better method to increase the size of an array in C++ dynamically

Of course the real realloc is a lot more complex because the real realloc checks the current block to see if the current block can be expanded before the current block allocates new data and probably doesn t call regular malloc but the functionality is roughly this

from question  

Double free or corruption error happens when call free in c

So your call to realloc may not have resulted in a physical page being returned to the system so it s still mapped to you program and can be used;however a following call to malloc could use that space or it could be reclaimed by the system at any time

from question  

What happens to array elements after the original array is reallocated?

Back to Home
Data comes from Stack Exchange with CC-BY-SA-4.0