memory allocation
csci 2410 | fall 2023
Prof. Sibin Mohan
static
- most definitions
- all local, global, file scope variables
static
- most definitions
- all local, global, file scope variables
|
int i ; |
struct student st ; |
char name[128] ; |
const double* pd ; |
... |
|
static
- most definitions
- all local, global, file scope variables
|
int i ; |
struct student st ; |
char name[128] ; |
const double* pd ; |
... |
|
- memory allocated at compile time
static
- memory allocated at compile time
static
- memory allocated at compile time
- compiler needs to to exactly how much memory
static
- memory allocated at compile time
- compiler needs to to exactly how much memory
- the following is illegal:
static
- memory allocated at compile time
- compiler needs to to exactly how much memory
- the following is illegal:
char array[n] ;
static
- memory allocated at compile time
- compiler needs to to exactly how much memory
- the following is illegal:
char array[n] ;
static
- memory allocated at compile time
- compiler needs to to exactly how much memory
- the following is illegal:
char array[n] ;
- the value of n can change at run time
static
- memory allocated at compile time
- compiler needs to to exactly how much memory
- the following is illegal:
char array[n] ;
- the value of n can change at run time
- e.g., we can do n = 200 ;
- before the array is defined
dynamic
- everything allocated using malloc
dynamic
- everything allocated using malloc*
[*and other calls as we shall see]dynamic
- everything allocated using malloc*
- memory allocated at run time
[*and other calls as we shall see]dynamic
- everything allocated using malloc*
- memory allocated at run time
|
char* pc = (char*)malloc(128*sizeof(char)) ; |
|
[*and other calls as we shall see]dynamic
- everything allocated using malloc*
- memory allocated at run time
|
char* pc = (char*)malloc(128*sizeof(char)) ; |
|
- 128 bytes of memory allocated dynamically
[*and other calls as we shall see]
dynamic
- 128 bytes of memory allocated dynamically
dynamic
- 128 bytes of memory allocated dynamically
- this is completely legal:
dynamic
- 128 bytes of memory allocated dynamically
- this is completely legal:
|
(char*)malloc(n*sizeof(char)) |
|
dynamic
- 128 bytes of memory allocated dynamically
- this is completely legal:
|
(char*)malloc(n*sizeof(char)) |
|
- value of n can change at run time
dynamic
function name |
bytes allocated |
inititalize? |
dynamic
function name |
bytes allocated |
inititalize? |
malloc(size) |
size |
no |
dynamic
function name |
bytes allocated |
inititalize? |
malloc(size) |
size |
no |
calloc (nmemb,size) |
nmemb*size
|
0 |
dynamic
function name |
bytes allocated |
inititalize? |
malloc(size) |
size |
no |
calloc (nmemb,size) |
nmemb*size
|
0 |
realloc (*ptr,size) |
grow/shrink *ptr to size |
orig *ptr |
dynamic
function name |
bytes allocated |
inititalize? |
malloc(size) |
size |
no |
calloc (nmemb,size) |
nmemb*size
|
0 |
realloc (*ptr,size) |
grow/shrink *ptr to size |
orig *ptr |
free(*ptr) |
n/a |
n/a |
|
|
|
dynamic
function name |
bytes allocated |
inititalize? |
malloc(size) |
size |
no |
calloc (nmemb,size) |
nmemb*size
|
0 |
realloc (*ptr,size) |
grow/shrink *ptr to size |
orig *ptr |
free(*ptr) |
n/a |
n/a |
|
|
|
all defined in <stdlib.h>
size_t
- unsigned integer data type
size_t
- unsigned integer data type
- lots of system calls use size_t
size_t
- unsigned integer data type
- lots of system calls use size_t
- not a real data type
size_t
- unsigned integer data type
- lots of system calls use size_t
- not a real data type
- typedef is used → depends on platform!
size_t
- unsigned integer data type
- lots of system calls use size_t
- not a real data type
- typedef is used → depends on platform!
platform |
size_t |
32 bit |
unsigned int |
64 bit |
unsigned long long int |
malloc
void *malloc(size_t size);
malloc
void *malloc(size_t size);
- allocate raw memory of size_t size bytes
malloc
void *malloc(size_t size);
- allocate raw memory of size_t size bytes
- no initialization
- "data" is whatever is in memory
malloc
void *malloc(size_t size);
- allocate raw memory of size_t size bytes
- no initialization
- "data" is whatever is in memory
- use memset() to set memory to 0
malloc
void *malloc(size_t size);
allocate raw memory of size_t size bytes
no initialization
- "data" is whatever is in memory
use memset() to set memory to 0
returns → pointer to newly allocated memory
calloc
void* calloc(size_t nmemb, size_t size);
calloc
void* calloc(size_t nmemb, size_t size);
- allocate raw memory of nmemb*size bytes
calloc
void* calloc(size_t nmemb, size_t size);
- allocate raw memory of nmemb*size bytes
- guaranteed to initialize memory to 0
calloc
void* calloc(size_t nmemb, size_t size);
- allocate raw memory of nmemb*size bytes
- guaranteed to initialize memory to 0
- e.g., calloc(10, sizeof(int)) ;
calloc
void* calloc(size_t nmemb, size_t size);
- allocate raw memory of nmemb*size bytes
- guaranteed to initialize memory to 0
- e.g., calloc(10, sizeof(int)) ;
- creates memory for 10 integers
- each one set to 0
calloc
void* calloc(size_t nmemb, size_t size);
allocate raw memory of nmemb*size bytes
guaranteed to initialize memory to 0
e.g., calloc(10, sizeof(int)) ;
- creates memory for 10 integers
- each one set to 0
returns → pointer to newly allocated memory
realloc
void* realloc(void *ptr, size_t size);
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
- grows/shrink in place if possible
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
- grows/shrink in place if possible
- if not enough space to grow,
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
- grows/shrink in place if possible
- if not enough space to grow,
- allocate new memory of size (ptr2)
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
- grows/shrink in place if possible
- if not enough space to grow,
- allocate new memory of size (ptr2)
- copy as much of old data as possible
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
- grows/shrink in place if possible
- if not enough space to grow,
- allocate new memory of size (ptr2)
- copy as much of old data as possible
- i.e., from ptr → ptr2
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
- grows/shrink in place if possible
- if not enough space to grow,
- allocate new memory of size (ptr2)
- copy as much of old data as possible
- i.e., from ptr → ptr2
- free old pointer, i.e., ptr
realloc
void* realloc(void *ptr, size_t size);
- reallocate the memory pointed to by ptr
- i.e., grow/shrink it to the new size
- grows/shrink in place if possible
- if not enough space to grow,
- allocate new memory of size (ptr2)
- copy as much of old data as possible
- i.e., from ptr → ptr2
- free old pointer, i.e., ptr
- returns → one of,
- *ptr if the new size fits
- *ptr2, i.e., pointer to new allocation
realloc oddities
- if original allocation was using calloc()
- remember it sets the memory to 0
realloc oddities
- if original allocation was using calloc()
- remember it sets the memory to 0
- realloc will not set extended memory to 0
realloc oddities
- if original allocation was using calloc()
- remember it sets the memory to 0
- realloc will not set extended memory to 0
- e.g., if calloc created a 10 byte array, pa
- initialized to 0
realloc oddities
- if original allocation was using calloc()
- remember it sets the memory to 0
- realloc will not set extended memory to 0
- e.g., if calloc created a 10 byte array, pa
- initialized to 0
- realloc(pa, 20)
- last 10 bytes not set to 0
common memory allocation errors
common errors
- allocation error → memory not allocated
common errors
- allocation error → memory not allocated
- maybe not enough memory in system
common errors
- allocation error → memory not allocated
- maybe not enough memory in system
- returns NULL
common errors
- allocation error → memory not allocated
- maybe not enough memory in system
- returns NULL
- check for the return value before use!
common errors
- allocation error → memory not allocated
- maybe not enough memory in system
- returns NULL
- check for the return value before use!
- dangling pointer → accessing free'd pointer
common errors
- allocation error → memory not allocated
- maybe not enough memory in system
- returns NULL
- check for the return value before use!
- dangling pointer → accessing free'd pointer
- mem may have been reallocated
common errors
- allocation error → memory not allocated
- maybe not enough memory in system
- returns NULL
- check for the return value before use!
- dangling pointer → accessing free'd pointer
- mem may have been reallocated
- bad things happen (crashes, etc.)
common errors
- allocation error → memory not allocated
- maybe not enough memory in system
- returns NULL
- check for the return value before use!
- dangling pointer → accessing free'd pointer
- mem may have been reallocated
- bad things happen (crashes, etc.)
- avoid free() until all references are done
common errors [contd.]
- memory leaks → allocate but forget to free()!
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
- over time, less memory available
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
- over time, less memory available
- can slow down/crash entire system!
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
- over time, less memory available
- can slow down/crash entire system!
- always pair a free() with an allocation
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
- over time, less memory available
- can slow down/crash entire system!
- always pair a free() with an allocation
- double free → free memory twice
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
- over time, less memory available
- can slow down/crash entire system!
- always pair a free() with an allocation
- double free → free memory twice
- bad (unpredictable) things happen!
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
- over time, less memory available
- can slow down/crash entire system!
- always pair a free() with an allocation
- double free → free memory twice
- bad (unpredictable) things happen!
- accidentally free memory used elsewhere
common errors [contd.]
- memory leaks → allocate but forget to free()!
- memory never get freed/released
- over time, less memory available
- can slow down/crash entire system!
- always pair a free() with an allocation
- double free → free memory twice
- bad (unpredictable) things happen!
- accidentally free memory used elsewhere
- memory allocation logic can crash!
memory allocation csci 2410 | fall 2023 Prof. Sibin Mohan