简体   繁体   中英

Does a pointer point to the LSB or MSB?

if I have the following code:

int i = 5;
void * ptr = &i;
printf("%p", ptr);

Will I get the LSB address of i, or the MSB?
Will it act differently between platforms?
Is there a difference here between C and C++?

Consider the size of int is 4 bytes. Always &i will gives you the first address of those 4 bytes.

If the architecture is little endian, then the lower address will have the LSB like below.

+------+------+------+------+
Address | 1000 | 1001 | 1002 | 1003 |
        +------+------+------+------+
Value   |   5  |    0 |    0 |    0 |
        +------+------+------+------+

If the architecture is big endian, then the lower address will have the MSB like below.

+------+------+------+------+
Address | 1000 | 1001 | 1002 | 1003 |
        +------+------+------+------+
Value   |   0  |    0 |    0 |    5 |
        +------+------+------+------+

So &i will give LSB address of i if little endian or it will give MSB address of i if big endian

In mixed endian mode also, either little or big endian will be chosen for each task dynamically.

Below logic will tells you the endianess

int i = 5; 
void * ptr = &i; 
char * ch = (char *) ptr;

printf("%p", ptr); 
if (5 == (*ch))
    printf("\nlittle endian\n");
else
    printf("\nbig endian\n");

This behaviour will be same for both c and c++

Will I get the LSB address of i, or the MSB?

This is platform dependent: it will be the lowest addressed byte , which may be MSB or LSB depending on your platform's endianness .

Although this is not written in the standard directly, this is what's implied by section 6.3.2.3.7:

When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object.


Will it act differently between platforms?

Yes


Is there a difference here between c and c++?

No: it is platform-dependent in both C and C++

It depends on the endianness of the platform; if it's a little-endian platform, you'll get a pointer to the LSB, if it's a big-endian platform it will point the MSB. There are even some mixed-endian plaforms, in that case may God have mercy of your soul check the specific documentation of your compiler/CPU.

Still, you can perform a quick check at runtime:

uint32_t i=0x01020304;
char le[4]={4, 3, 2, 1};
char be[4]={1, 2, 3, 4};
if(memcmp(&i, le, 4)==0)
    puts("Little endian");
else if(memcmp(&i, be, 4)==0)
    puts("Big endian");
else
    puts("Mixed endian");

By the way, to print pointers you must use the %p placeholder, not %d .

ptr stores the address of the starting byte of the integer object. Whether this is where the most or the least significant byte is stored depends on your platform. Some weird platforms even use mixed endianness in which case it'll be neither the MSB nor the LSB.

There is no difference between C and C++ in that respect.

What it points is MSB for my VC++ 2010 and Digital Mars. But it is related to endianness.

This question's answers give some infor for you: Detecting endianness programmatically in a C++ program .

Here, user "none" says:

#define BIG_ENDIAN      0
#define LITTLE_ENDIAN   1
 int TestByteOrder()
{
   short int word = 0x0001;
   char *byte = (char *) &word;
   return(byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
}

This gives some endianness info

well I get the LSB address of i, or the MSB?

It depends on the machine and the OS. On big endian machines and OS's you will get the MSB and on little endian machines and OS's you will get the LSB.

Windows is always little endian. All (most ?) flavors of Linux/Unix on x86 is little endian. Linux/Unix on Motorola machines is big endian. Mac OS on x86 machines is little endian. On PowerPC machines it's big endian.

well it act differently between platforms? Yes it will.

is there a difference here between c and c++? Probably not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM