-
June 4th, 2012, 11:49 PM
#1
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?
-
June 4th, 2012, 11:59 PM
#2
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.
-
June 5th, 2012, 09:13 AM
#3
Re: How to find the architecture of the system using pointers?
Originally Posted by madhu542
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.
Originally Posted by S_M_A
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.
-
June 5th, 2012, 09:20 AM
#4
Re: How to find the architecture of the system using pointers?
Originally Posted by Peter_B
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.
-
June 5th, 2012, 10:26 AM
#5
Re: How to find the architecture of the system using pointers?
Originally Posted by GCDEF
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...
-
June 5th, 2012, 10:28 AM
#6
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.
-
June 5th, 2012, 10:31 AM
#7
Re: How to find the architecture of the system using pointers?
Originally Posted by madhu542
...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...
-
June 5th, 2012, 10:33 AM
#8
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?
-
June 6th, 2012, 09:16 AM
#9
Re: How to find the architecture of the system using pointers?
Originally Posted by S_M_A
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.
Originally Posted by S_M_A
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|