Memory Management in C Memory Management in C Personal Software Engineering Personal Software Engineering
Memory Organization Memory Organization The call stack grows from the The call stack grows from the Function Call The top of memory down top of memory down Stack Frames Code is at the bottom of Code is at the bottom of sp memory. memory. Global data follows the code. Global data follows the code. Available The for What's left What's left – the "heap" the "heap" - is is Heap allocation available for allocation. available for allocation. Global Variables Binary Code 0
Allocating Memory Allocating Memory void *malloc( unsigned nbytes ) ; void *malloc( unsigned nbytes ) ; – Allocates 'nbytes' of memory in the heap. Allocates 'nbytes' of memory in the heap. – Guaranteed not to overlap other allocated memory. Guaranteed not to overlap other allocated memory. – Returns pointer to the first byte (or NULL if the heap is full). Returns pointer to the first byte (or NULL if the heap is full). – Similar to constructor in Java Similar to constructor in Java – allocates space. allocates space. – Space allocated uninitialized (random garbage). Space allocated uninitialized (random garbage). void free( void *ptr ) ; void free( void *ptr ) ; – Frees the memory assigned to ptr. Frees the memory assigned to ptr. – The space must have been allocated by malloc. The space must have been allocated by malloc. – No garbage collection in C (or C++). No garbage collection in C (or C++). – Can slowly consume memory if not careful Can slowly consume memory if not careful
How Much Space Is Needed? How Much Space Is Needed? - 1 1 sizeof sizeof ( type type ) ) – – gives the size of a type in bytes. gives the size of a type in bytes. Allocation Examples Allocation Examples ip int int *ip ip ; ????
How Much Space Is Needed? How Much Space Is Needed? - 1 1 sizeof sizeof ( type type ) ) – – gives the size of a type in bytes. gives the size of a type in bytes. Allocation Examples Allocation Examples ip int int *ip ip ; ???? ip ???? ip = ip = (int int *) *) malloc malloc( ( sizeof sizeof(int int) ) ) ) ;
How Much Space Is Needed? How Much Space Is Needed? - 1 1 sizeof sizeof ( type type ) ) – – gives the size of a type in bytes. gives the size of a type in bytes. Allocation Examples Allocation Examples ip int *ip int ip ; ???? ip ???? ip ip = = (int int *) *) malloc malloc( ( sizeof sizeof(int int) ) ) ) ; ip 1234 *ip ip = 1234 ; = 1234 ;
How Much Space Is Needed? How Much Space Is Needed? - 1 1 sizeof sizeof ( type type ) ) – – gives the size of a type in bytes. gives the size of a type in bytes. Allocation Examples Allocation Examples ip int int *ip ip ; ???? ip ???? ip = ip = (int int *) *) malloc malloc( ( sizeof sizeof(int int) ) ) ) ; ip 1234 *ip ip = 1234 ; = 1234 ; ip ip = ( = (int int *) *)malloc malloc( ( sizeof sizeof(int int) ) ; ) ) ; ip 1234 ????
How Much Space Is Needed? How Much Space Is Needed? - 1 1 sizeof sizeof ( type type ) ) – – gives the size of a type in bytes. gives the size of a type in bytes. Allocation Examples Allocation Examples ip int int *ip ip ; ???? ip ???? ip = ip = (int int *) *) malloc malloc( ( sizeof sizeof(int int) ) ) ) ; ip 1234 *ip ip = 1234 ; = 1234 ; orphan storage ip = ( ip = (int int *) *)malloc malloc( ( sizeof sizeof(int int) ) ; ) ) ; ip 1234 ????
How Much Space Is Needed? How Much Space Is Needed? - 1 1 sizeof sizeof ( type type ) ) – – gives the size of a type in bytes. gives the size of a type in bytes. Allocation Examples Allocation Examples ip int *ip int ip ; ???? ip ???? ip ip = = (int int *) *) malloc malloc( ( sizeof sizeof(int int) ) ) ) ; ip 1234 *ip ip = 1234 ; = 1234 ; ip free( free(ip ip) ; ) ; ???? 1234
How Much Space Is Needed? How Much Space Is Needed? - 1 1 sizeof sizeof ( type type ) ) – – gives the size of a type in bytes. gives the size of a type in bytes. Allocation Examples Allocation Examples ip int int *ip ip ; ???? ip ???? ip ip = = (int int *) *) malloc malloc( ( sizeof sizeof(int int) ) ) ) ; ip 1234 *ip ip = 1234 ; = 1234 ; ip free(ip free( ip) ; ) ; ???? 1234 ip ip = ( = (int int *) *)malloc malloc( ( sizeof sizeof(int int) ) ; ) ) ; ip ????
How Much Space Is Needed? How Much Space Is Needed? - 2 2 char * char *make_copy make_copy( char * ( char *orig orig ) { ) { char *copy = (char *) char *copy = (char *) malloc malloc( ( sizeof sizeof(char) * (char) * strlen strlen(orig orig) + 1 ) ; ) + 1 ) ; (void) (void) strcpy strcpy( copy, ( copy, orig orig ) ; ) ; return copy ; return copy ; } char *copy char orig[4] ; 'J' 'o' 'e' '\0' 'J' 'o' 'e' '\0' char **create_string_vector char ** create_string_vector( ( int int nstrings nstrings ) { ) { char ** char **str_vector str_vector ; str_vector str_vector = (char **) = (char **) malloc malloc( ( nstrings nstrings * * sizeof sizeof(char *) ) ; (char *) ) ; return return str_vector str_vector ; } nstrings nstrings str_vector str_vector
Linked Lists Linked Lists Structures with values and link (pointer) to next Structures with values and link (pointer) to next – and and possibly previous possibly previous - structure. structure. Example: List of strings: Example: List of strings: typedef struct node { top string "New Years" next char *string ; struct node *next ; } node ; string "4th of July" next node *top ; string "Labor Day" next
Recommend
More recommend