answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

The realloc() function in the C run-time library changes the allocation of a previously allocated memory block. If the new allocation is shorter than the old allocation, the end of the allocated memory is deleted. If the new allocation is longer than the old allocation, the old allocation is copied to the new allocation, and the new portion of the allocation is undefined.

char* pchar = (char*) malloc(100); /*allocate 100 bytes */

pchar = (char*) realloc (pchar, 200); /* change allocation to 200 bytes */

However, and this is important to proper program design...

You should always check the result of malloc() to see if it returned NULL, which means that it failed to find enough memory.

The same thing goes for realloc() and, additionally, the example above can cause a memory leak. This is because if realloc() failed, pchar is set to NULL, losing the address of the previously "successful" allocation, because you never called free().

A proper example...

char* pchar;

char* pchartemp;

if ((pchar = (char*) malloc (100)) NULL) {

... report realloc() failure and abort ...

} else {

pchar = pchartemp;

}

... use larger pchar ...

free (pchar);

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The realloc() function modifies a memory location previously allocated using malloc(). The prototype is void * realloc(void *ptr, size_t size);

First argument ptr is a pointer to the previously allocated memory and the second is the new size in bytes.

The function returns ptr if sufficient space is available to expand the memory block. If sufficient space does not exist to expand the current block in its current location, a new block of the size is allocated, and existing data is copied from the old to the new block. The old block is freed, and a pointer to the new block is returned.

If argument ptr is NULL, the function acts like malloc(), allocating a block of size bytes and returning a pointer to it.

If argument size is 0, the memory that ptr points to is freed, and the function returns NULL.

If memory is insufficient for reallocation, the function returns NULL and the original block remains unchanged.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the use of realloc in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the use of loader in c language?

There is no part called 'loader' in C language.


What programming language does the PSP use?

c++


What is use of l in c language?

Bitwise OR.


What is thread in c language?

thread is not specific to C-language. Use wikipedia to find out what threads are, and use Posix Threads (or pthreadsfor short) to create multi-threaded programs in C


Easy to use loop in c language?

Yes.

Related questions

What is the use of realloc in c program?

Increase or decrease the size of the allocated memory. (Note: functionality of malloc and free can be achieved with realloc, too.)


What is realloc memory allocation?

Use help/manual.


Why you are use to c language?

I do use am a programmer, because C-language.


Memory allocation in c plus plus?

Although C++ inherits malloc/calloc, realloc and free from C, programmers are encouraged to use the object-oriented operators, new and delete instead. Not only are they much easier to use, they can also be used with primitive data types.


What is the use of loader in c language?

There is no part called 'loader' in C language.


What are the different types of memory allocations in c?

1.malloc 2.calloc 3.realloc 4.free


How do you write a programme in c language using arrays to copy diagonal elements of a 2-d array into a 1-d array?

TO use a c language first step is to know about the c language and the steps to use the c progrmming language with the help of any elders or with the teachers. TO use the arrays you have to get th eknowledge of "c" language


Which compiler you must use to run C language?

To run C language?! Your question makes no sense.


Explain use of graphics in C language?

C has nothing to do with graphics.


Can you use C as OOPS?

No, it is a programming language.


What is the use of c-language?

Programming, mainly.


Where we use the c language?

In computer-programming.