Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
realloc.c File Reference
#include <stdlib.h>
#include <string.h>
Include dependency graph for realloc.c:

Go to the source code of this file.

Functions

void * realloc (void *ptr, size_t size)
 Reallocates the given area of memory. More...
 
void * reallocf (void *ptr, size_t size)
 Reallocates the given area of memory. More...
 

Function Documentation

◆ realloc()

void* realloc ( void *  ptr,
size_t  size 
)

Reallocates the given area of memory.

Reallocates the given area of memory. It must be previously allocated by

See also
malloc,
calloc or
realloc and not yet freed with a call to
free or
realloc. Otherwise, the results are undefined.

The reallocation is done by either: 1) expanding or contracting the existing area pointed to by ptr, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. If the area is expanded, the contents of the new part of the array are undefined. 2) allocating a new memory block of size size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block.

If there is not enough memory, the old memory block is not freed and null pointer is returned. If ptr is NULL, the behavior is the same as calling malloc(size).

Parameters
ptrpointer to the memory area to be reallocated
sizenew size of the array
Returns
pointer to new memory allocation. If size is zero (e.g. realloc(ptr,0)) then returns NULL

Definition at line 4 of file realloc.c.

5 {
6  void* new_data = NULL;
7 
8  if(size)
9  {
10  if(!ptr)
11  {
12  return malloc(size);
13  }
14 
15  new_data = malloc(size);
16  if(new_data)
17  {
18  memcpy(new_data, ptr, size); // TODO: unsafe copy...
19  free(ptr); // we always move the data. free.
20  }
21  }
22 
23  return new_data;
24 }
void free(void *ptr)
Deallocates allocated memory space.
#define NULL
Definition: stddef.h:15
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References free(), malloc(), memcpy(), and NULL.

Referenced by reallocf().

Here is the caller graph for this function:

◆ reallocf()

void* reallocf ( void *  ptr,
size_t  size 
)

Reallocates the given area of memory.

Reallocates the given area of memory. It must be previously allocated by

See also
malloc,
calloc or
realloc and not yet freed with a call to
free or
realloc. Otherwise, the results are undefined.

reallocf is a FreeBSD extension to realloc that frees the input pointer if an error occurrs

This library does not handle the BSD case where realloc(ptr,0) frees the ptr

Parameters
ptrpointer to the memory area to be reallocated
sizenew size of the array
Returns
pointer to new memory allocation. If size is zero (e.g. realloc(ptr,0)) then returns NULL

Definition at line 26 of file realloc.c.

27 {
28  void* p = realloc(ptr, size);
29 
30  if((p == NULL) && (ptr != NULL))
31  {
32  free(ptr);
33  }
34 
35  return p;
36 }
void free(void *ptr)
Deallocates allocated memory space.
#define NULL
Definition: stddef.h:15
void * realloc(void *ptr, size_t size)
Reallocates the given area of memory.
Definition: realloc.c:4

References free(), NULL, and realloc().