code_junkie

What causes this integer pointer reassignment to crash?

Nov 11th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. int *a = 10;
  2. *a = 100;
  3.  
  4. int *a = 10;
  5.  
  6. int *a = malloc(sizeof(int));
  7. *a = 100;
  8.  
  9. int cell = 10;
  10. int *a = &cell; // a points to address of cell
  11. *a = 100; // content of cell changed
  12.  
  13. a = malloc(sizeof(int));
  14.  
  15. long *a;
  16.  
  17. a = malloc(sizeof *a);
  18.  
  19. int *a = 10;
  20.  
  21. int *a = 10;
  22.  
  23. int *a = 10;
  24.  
  25. int *a = 10;
  26.  
  27. *a = 100;
  28.  
  29. int *a;
  30. a = malloc(sizeof(int));
  31. *a = 10;
  32. printf("a=%in", *a);
  33. free(a);
  34.  
  35. int *a = (int *) 10;
  36. *a = 100;
  37.  
  38. int* a = 10;
  39. *a = 100;
  40.  
  41. "Pointer-to-int 'a' becomes 10"
  42. "Value-pointed-to-by 'a' becomes 100"
  43.  
  44. "Value-pointed-to-by 10 becomes 100"
  45.  
  46. int* ptr = (int*)10; // You've guessed at a memory address, and probably got it wrong
  47. int* ptr = malloc(sizeof(int)); // OS gives you a memory address at runtime
  48.  
  49. (int*) a = 10;
  50. (*a) = 100;
  51.  
  52. (void*) v = NULL;
  53.  
  54. struct Hello {
  55. int id;
  56. char* name;
  57. };
  58.  
  59. ...
  60.  
  61. struct Hello* hello_ptr = malloc(sizeof Hello);
  62. hello_ptr->id = 5;
  63. hello_ptr->name = "Cheery";
  64.  
  65. void* malloc(size_t size);
  66.  
  67. free(hello_ptr);
  68.  
  69. 0.1.2.3.4.5. 6
  70. C h e e r y
Add Comment
Please, Sign In to add comment