Advertisement
thenewboston

C Programming Tutorial - 47 - The Heap

Aug 22nd, 2014
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     // before now, the memory your program was allowed to use was limited by the variables it declared
  7.     // what if we need more space that our variables reserved?
  8.     // what if we don't know how many "employees" we are going to need in our array
  9.  
  10.     // this creates an integer pointer, points to first item in heap
  11.     int * points;
  12.  
  13.     // malloc gets memory from the heap, need to tell it how many bytes
  14.     // we don't know how many bytes an int is, but we know we need 10
  15.     // (int *) is an int typecast pointer, we will be using this memory to store ints
  16.     // can now just use point[0], points[1], etc...
  17.     points = (int *) malloc(5 * sizeof(int));
  18.  
  19.     return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement