CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2012
    Posts
    9

    How to find the architecture of the system using pointers?

    Hi,

    I have a program (below)

    int main() {
    int x = 1;
    char *y = (char *) &x;
    printf("%d\n", *y);
    }

    The output will be either "0" or "1" depending on the architecture of the system.
    When I executed on my system I got output as "1", now what is the architecture of my system is it 32 bit or 64 bit?

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to find the architecture of the system using pointers?

    You cant find it out like that. Regardless of system that will always output 1.

    Here is one way of doing it in Windows http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    In other operating systems there are other methods.
    Last edited by S_M_A; June 5th, 2012 at 10:23 AM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: How to find the architecture of the system using pointers?

    Quote Originally Posted by madhu542 View Post
    The output will be either "0" or "1" depending on the architecture of the system.
    When I executed on my system I got output as "1", now what is the architecture of my system is it 32 bit or 64 bit?
    Strictly speaking it doesn't depend upon the architecture, it depends upon the endianness.

    Quote Originally Posted by S_M_A View Post
    Regardless of system that will always output 1.
    Not so - it depends upon the endianness of the system. For little-endian systems (e.g. x86) it will output 1. For big-endian systems it will output 0.

    Are you thinking that it will always give 1 because of the %d (i.e. int) specifier in the printf call?

    What will happen is that *y (which is a char) will be promoted to an int before the printf call - this is because variadic arguments (such as those after the format specifier in printf) undergo integer promotion. So for little-endian systems the value of *y (char 1) is promoted to int 1, and output as 1. For big-endian systems *y is 0, which is promoted to int 0.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to find the architecture of the system using pointers?

    Quote Originally Posted by Peter_B View Post
    Strictly speaking it doesn't depend upon the architecture, it depends upon the endianness.


    Not so - it depends upon the endianness of the system. For little-endian systems (e.g. x86) it will output 1. For big-endian systems it will output 0.

    Are you thinking that it will always give 1 because of the %d (i.e. int) specifier in the printf call?

    What will happen is that *y (which is a char) will be promoted to an int before the printf call - this is because variadic arguments (such as those after the format specifier in printf) undergo integer promotion. So for little-endian systems the value of *y (char 1) is promoted to int 1, and output as 1. For big-endian systems *y is 0, which is promoted to int 0.
    I think he was going on whether an int was 4 or 8 bytes to determine 32 or 64 bit architecture.

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to find the architecture of the system using pointers?

    Quote Originally Posted by GCDEF View Post
    I think he was going on whether an int was 4 or 8 bytes to determine 32 or 64 bit architecture.
    I think that int could be 4 bytes on both 32-bit and 64-bit architecture.
    This *could* work if you are interested if the code is *built* to run on 32-bit and 64-bit architecture:
    Code:
    int main()
    {
    	return sizeof(void*);
    }
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to find the architecture of the system using pointers?

    Eh I was a bit unclear there yes. Endian affect things for sure but you can't tell if the code executes in a 32 or 64 bit OS.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to find the architecture of the system using pointers?

    Quote Originally Posted by madhu542 View Post
    ...now what is the architecture of my system is it 32 bit or 64 bit?
    Could you please clarify – what are you looking for?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to find the architecture of the system using pointers?

    But Vlad since the code is built for a specific target how can the size of the pointer change in runtime?

    Or do you mean to use two separate builds and use them to test the system?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to find the architecture of the system using pointers?

    Quote Originally Posted by S_M_A View Post
    But Vlad since the code is built for a specific target how can the size of the pointer change in runtime?
    I meant that you can only determine if the code was built for 32-bit or for 64-bit, NOT if it runs on 64-bit.
    Quote Originally Posted by S_M_A View Post
    Or do you mean to use two separate builds and use them to test the system?
    Well, you can use only one (64-bit) build and see if it even runs on your system

    But seriously, I have asked OP – what is he looking for?
    What is my “architecture” if I run 32-bit Windows on 64-bit CPU? Or emulate 64-bit environment on a 32-bit CPU?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured