Dynamic Memory Allocation
Dynamic Memory Allocation
since files can contain many lines, and lines can be long, we have to ask the compiler to reserve a significant amount of memory:
#define MAXNUMLINE 100000 #define MAXLINELENGTH 1024 char text[MAXNUMLINE][MAXLINELENGTH]; ... strcpy(text[0], Maxima rules!\0);
the above example would allocate a 100 Megabytes of memory, even though the text might just contain one line (albeit essential).
an int
this allocates (reserves) size bytes of memory. the memory is not initialized.
char * copy_string(char * str) { /* allocate just enough memory */ char * copy_of_str = (char *) malloc((strlen(str)+1)*sizeof(char)); if(copy_of_str == 0) return 0; /* return 0 case there is no memory */ strcpy(copy_of_str, str); /* copy the string into the new memory */ return copy_of_str; } the memory is not initialized. here we copy the string in it.
example: create_vector_array(10)
10 * 4 = 40 bytes
v v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
example: create_matrix(5, 4)
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 mtx[col][3] mtx[col][2] mtx[col][1] mtx[col][0]
return mtx; }
0.0
you can free objects in any order. make sure that you are not using the pointer to the memory after it was freed and never call free() twice on the same pointer. to be sure that this can never happen, I always set the pointer to 0 after a free:
name = 0; my_vector = 0;
even worse, after the above free() the pointers to 1000 pieces of memory are lost forever. instead, we need to write a function to free all of them:
void delete_matrix(double ** matrix, int n_columns) { int col; for(col = 0; col < n_columns; col++) { free(mtx[col]); /* free each column */ } free(matrix); /* free matrix at the very end */ }
we need to know the number of columns, but not the number of rows.
observation 2:
the memory manager needs 3 bytes for administration.
observation 3:
the memory manager indeed recycles memory that was freed.
-> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> ->
used used used used used used used used used used used used used used used used used used
= = = = = = = = = = = = = = = = = =
16 16 16 16 16 16 16 16 16 16 16 16 20 20 20 20 24 24
the good news: fast more memory efficient no memory leakage the bad news: length/size fixed must determine compiletime
the bad news: slower, fragmentation issue some memory wasted error-prone, memory leakage the good news: length/size flexible size set run-time, adapt to requirements
this will allocate count times size bytes of memory; the difference with malloc() is that calloc() sets the memory to 0.
behind the scenes this will allocate a new block of new_size bytes, and copy the values of the existing block in the new memory; finally, the existing memory block will be freed, so dont use block afterwards!
char * name = copy_string(Mabel); /* allocates 6 bytes */ name = realloc(name,25); /* enlarge the block to 25 bytes */ strcat(name, and Klaas); /* stick and Klaas behind it */ printf(%s, name); Mabel and Klaas