answersLogoWhite

0

This involves allocating and de-allocating memory at run-time. Look into the new and delete operators for C++ or malloc and free for C.

User Avatar

Sonny Bernhard

Lvl 10
2y ago

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
More answers

realloc - memory reallocator

it is initialised as follows:

#include
void *realloc(void *ptr, size_t size);

The realloc() function changes the size of the memory object pointed to by ptr to the size specified by size. The contents of the object will remain unchanged up to the lesser of the new and old sizes. If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed. If the new size is larger, the contents of the newly allocated portion of the object are unspecified. If size is 0 and ptr is not a null pointer, the object pointed to is freed. If the space cannot be allocated, the object remains unchanged.
User Avatar

Wiki User

17y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What is Realloc?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What if happen when you allocate memory by using malloc or calloc when the momory is not available contigiously?

malloc/calloc/realloc will return NULL


Which function should be used to free the memory allocated by calloc?

Use the free function to release memory that was previously allocated by malloc, calloc or realloc.


10 When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?

As for pointers, I think you have to readjust them. Pointers are just variables that store a memory address in them. You can have as many pointers that point to a single location in memory as you want.


What is the use of realloc in c language?

Realloc is a function in C. It is used to change the size of a block of memory by expanding it. There are various ways realloc can change the memory size, depending on if there is enough space in the block of memory.


How can you increase the size of a statically allocated array?

/* Allocate space for an array with ten elements of type int. */int *ptr = malloc(10 * sizeof (int));if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */ realloc It is often useful to be able to grow or shrink a block of memory. This can be done using realloc which returns a pointer to a memory region of the specified size, which contains the same data as the old region pointed to by ptr (truncated to the minimum of the old and new sizes). If realloc is unable to resize the memory region in-place, it allocates new storage, copies the required data, and frees the old pointer. If this allocation fails, realloc maintains the original pointer unaltered, and returns the null pointer value. The newly allocated region of memory is uninitialized (its contents are not predictable). The function prototype is void *realloc(void *pointer, size_t size);