In C, you can manually manage memory to have greater control.
malloc.
Local variables are automatically allocated on the stack when they come into scope. They are deallocated (memory is freed) when they exit their scope.
The stack grows from top to bottom.
Manually allocated memory is put on the heap.
The heap grows from bottom to top.
<stdlib.h>
void* malloc(size_t size)
- Request allocation of size bytes
- Returns a void* to the allocated memoru
- Returns NULL if allocation failed
free(void* p)
- Frees block of memory starting at p
- Must free in same scope of call to malloc
void* memset(void s[n], int c, size_t n)
- Sets first n bytes of memory pointed to by s with byte c.
- Returns s (pointer to void)
- Can clear memory area given by call to malloc (or use calloc)
void* calloc(size_t n, size_t size)
- Request allocation of n elements of size bytes each
- Clears allocated memory with 0
- Returns a void* to the allocated memoru
void* realloc(void* p, size_t size)
- Resize memory to size bytes
- Doesn't initialize new memory, see memset
- If p is NULL → same as malloc(size)
char *p = malloc(sizeof(char * 64)); // char *p = malloc(sizeof(*p * 12)); <<-- *p is a char, same effect as above // always error check malloc if (p == NULL) { fprintf(stderr, "Call to malloc failed! Aborting...\n"); exit(1); } // use p strcpy(p, "malloced string"); puts(p); // free memory, else memory leak free(p);