double myArray[5] = { 3, 6, 9, 12, 15 }; LabelEntry staticEntries[5]; staticEntries[2].label = "Label1";
malloc
: standard library function for memory
allocation (#include <stdlib.h>
);malloc
returns the address of the memory it just
set aside.new
Fish(color, loc);
)int i; double * memoryBlock = malloc(5*sizeof(double)); double * ptr = memoryBlock; for ( i = 3; i <= 15; i += 3, ptr++ ) *ptr = i; LabelEntry * staticEntries = malloc(5*sizeof(LabelEntry)); staticEntries[2].label = "Label1"; /* We can use array indexing with a pointer. We could also use: (staticEntries + 2)->label = "Label1"; or *(staticEntries + 2).label = "Label1"; */
free(memoryBlock);The pointer to the space might be static, though, in which case you may lose the pointer without freeing the memory (memory is still allocated but unreachable). This is called a memory leak, because over time you will have a shrinking pool of available memory.