16

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++?

elyashiv
  • 3,623
  • 2
  • 29
  • 52
  • 6
    It's **undefined behaviour**. The only way to print `ptr` is with `%p`, or by converting it to `intptr_t` and using the according print formatting macro. – Kerrek SB Aug 16 '12 at 10:41

6 Answers6

22

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++

rashok
  • 12,790
  • 16
  • 88
  • 100
11

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++

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
8

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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Please clarify my small doubt. As per my knowledge mixed endian system will choose either little or big endian for each task. Please tell me whats the use of else case in your porgram. Whether my understanding about mixed endian is wrong? – rashok Aug 17 '12 at 10:46
  • "Mixed endian" is a catchall term used for architectures where the byte ordering is not the canonical big-endian or little-endian (check wikipedia for some examples); my code simply checks if the memory representation of that 32-bit integer matches the expected one for big-endian or little-endian systems, if it doesn't match it doesn't investigate further and just says that it's mixed endian. – Matteo Italia Aug 17 '12 at 17:18
6

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.

sellibitze
  • 27,611
  • 3
  • 75
  • 95
  • `ptr stores the address of the starting byte of the integer object.` You mean it will always give us lowerest address of multi-byte data (like int)? Is there any platform in which memory grows downwards? – abhiarora Jul 25 '19 at 10:25
3

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

Community
  • 1
  • 1
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
1

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.

Kirt Undercoffer
  • 591
  • 4
  • 11