|
Memory Allocation in C
An Overview |
|
Prof. David Bernstein
|
| Computer Science Department |
| bernstdh@jmu.edu |
(void *) malloc(size_t size):
size bytes(void *) realloc(void* p, size_t size):
p
(which, if it is not null, must have been
returned by a call to malloc()
or realloc()) to size
bytesNULL)
but does not change the parameter (it can't since the
parameter is a void* not a void**)
p is null then
realloc() is equivalent to
malloc()
void free(void *p):
p
(which must have been returned by a previous call
to malloc()
or realloc()) available for re-usep (i.e., p still
points to the now free memory) -- it can't because the
parameter is a void* NOT a void**
p is null then the
call does nothing
(void *) calloc(size_t number, size_t size):
number
entities of size size
NULL)
size * number
is of type size_t (e.g., does not wrap)(void *) memset(void* buffer, int value, size_t number):
unsigned char named value
to the first number of characters pointed
to by buffer
buffer
NULL pointer is returned, or the behavior is as
if the size were nonzero (but the returned pointer should
not be used)realloc()
since it will not free the old memory
alloca():
realloc() with a size of 0:
size_t
int must begin
at an address that is a multiple of 32)
libc)