Wednesday, March 3, 2010

What are ways to allocate memory in C?

// allocate memory of size
void *malloc(size_t size);

//allocates a region of memory, initialized to 0, of size nelements × elementSize
void *calloc(size_t nelements, size_t elementSize);

// resizes allocated memory
void *realloc(void *pointer, size_t size);

// unallocates memory
void free(void *pointer);
Passing non-allocated memory (i.e. not allocated by malloc, calloc, or realloc) to free will result in undefined behavior

No comments: