smeacham

Example of malloc and free

Nov 6th, 2012
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. //
  2. //  main.m
  3. //  Memory
  4. //
  5. //  Created by Steve on 11/6/12.
  6. //  Copyright (c) 2012 Personal. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int square(int n) {
  12.     int retval = n * n;
  13.     return retval;
  14. }
  15.  
  16. int * memget() {
  17.     int * pInt = malloc(10 * sizeof(int));
  18.    
  19.     if (!pInt) {
  20.         // handle the error
  21.         return;
  22.     }
  23.    
  24.     *pInt = 5;
  25.    
  26.     // When done with the memory...
  27.     free(pInt);
  28. }
  29.  
  30. int main(int argc, const char * argv[])
  31. {
  32.     int i = 5;
  33.    
  34.     printf("i squared is %d\n", square(i));
  35.    
  36.     return 0;
  37. }
Add Comment
Please, Sign In to add comment