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?
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.
Re: How to find the architecture of the system using pointers?
Quote:
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.
Quote:
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.
Re: How to find the architecture of the system using pointers?
Quote:
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.
Re: How to find the architecture of the system using pointers?
Quote:
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*);
}
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.
Re: How to find the architecture of the system using pointers?
Quote:
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?
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?
Re: How to find the architecture of the system using pointers?
Quote:
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.
Quote:
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?