Why do I get a segmentation fault when I dereference a typecast?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Charlie Starks
    New Member
    • Dec 2011
    • 2

    Why do I get a segmentation fault when I dereference a typecast?

    Please review my pastebin. I commented exactly where it occurs, and how GDB will not access my variable.

    I made a program to prove that you can store an address into an unsigned int and use it as a pointer. It works on old 32bit machine, but not on my 64 bit. I can only assume it is because of my compiler??

    Code:
    //I am trying to figure out why I get a segmentation fault at "*((char *) hacky_nonpointer)"
    //
    //This works on a 32 bit older gcc compiler on an older machine, but with gcc-4.4.3 on my
    //x64 machine, I get a segmentation fault at that point. Is it my compiler??
    //Also, GDB shoots an "Error accessing memory" when I do x/xw hacky_nonpointer. It does
    //retain it's value though.
    
    #include <stdio.h>
    
    int main() {
       int i;
    
       char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
       int int_array[5] = {1, 2, 3, 4, 5};
    
       unsigned int hacky_nonpointer;
    
       hacky_nonpointer = (unsigned int) char_array;
    
       for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
          printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",
                hacky_nonpointer, *((char *) hacky_nonpointer));
          hacky_nonpointer = hacky_nonpointer + sizeof(char);
       }
    }
    Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
    Last edited by Niheel; Dec 16 '11, 05:38 AM. Reason: full code is better for future reference, pastebin link is still there
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    No it is not your compiler it is your code.

    A 64 bit machine (and 64 bit compiler) has a 64bit address space and 64 bit pointers but you are assigning the pointer to a unsigned int which is 32 bits and then casting that back to a pointer, 64 bits again. You are loosing the most significant 32 bits of the pointer.

    If you are going to assign a pointer value to an integer (and it isn't very good practice) then you should use an integer of the right size.

    C99 contains a header, stdint.h which defines uintptr_t which is defined as being an integer of the right size to contain a pointer on the platform.
    Last edited by Banfa; Dec 19 '11, 01:20 PM.

    Comment

    • Charlie Starks
      New Member
      • Dec 2011
      • 2

      #3
      Awesome.
      Assigning a pointer value to an int most certainly isn't good practice. The program was designed to show the fundamentals of pointers and what really is going on under the cover, even though I am just refreshing my own memory.

      I appreciate the insight and it definitely works. Kudos to you, Banfa, for now I know where my studies are going to take me next. Thanks a lot!

      Comment

      Working...