metallichondaman

Segmentation Fault!??!

Dec 15th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. //I am trying to figure out why I get a segmentation fault at "*((char *) hacky_nonpointer)"
  2. //
  3. //This works on a 32 bit older gcc compiler on an older machine, but with gcc-4.4.3 on my
  4. //x64 machine, I get a segmentation fault at that point. Is it my compiler??
  5. //Also, GDB shoots an "Error accessing memory" when I do x/xw hacky_nonpointer. It does
  6. //retain it's value though.
  7.  
  8. #include <stdio.h>
  9.  
  10. int main() {
  11. int i;
  12.  
  13. char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
  14. int int_array[5] = {1, 2, 3, 4, 5};
  15.  
  16. unsigned int hacky_nonpointer;
  17.  
  18. hacky_nonpointer = (unsigned int) char_array;
  19.  
  20. for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
  21. printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",
  22. hacky_nonpointer, *((char *) hacky_nonpointer));
  23. hacky_nonpointer = hacky_nonpointer + sizeof(char);
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment