Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:04:42+00:00 2026-06-12T19:04:42+00:00

Possible Duplicate: When does Endianness become a factor? reading this tuto on endianess, i

  • 0

Possible Duplicate:
When does Endianness become a factor?

reading this tuto on endianess, i fall on this example where endianess does matter. It is about writting a char* filled with 1 and 0. it can then be converted to a short, and results depends on endianess, little or big. Here is the example, quoted.

unsigned char endian[2] = {1, 0};
short x;

x = *(short *) endian;

What would be the value of x? Let’s look at what this code is doing.
You’re creating an array of two bytes, and then casting that array of
two bytes into a single short. By using an array, you basically forced
a certain byte order, and you’re going to see how the system treats
those two bytes. If this is a little-endian system, the 0 and 1 is
interpreted backwards and seen as if it is 0,1. Since the high byte is
0, it doesn’t matter and the low byte is 1, so x is equal to 1. On the
other hand, if it’s a big-endian system, the high byte is 1 and the
value of x is 256.

i wonder: when you are instantiating an array with a given number of memory bytes allocation (here, two bytes), how can conversion be done to any type (short, int…) as long as the array has been allocated the number of bytes corresponding to this byte? if not enough memory has been allocated to ‘contain this type’, will the next memory address still be read ? for instance if i want to cast endian to a long, will this be performed, reading four bytes from the beginning of endian, or will this fail ?

Then, a question on endianess: this is a characteristic of processor regarding habits to write bytes in memory with most significative byte at lowest memory location (big endian)or at highest memory location (little endian). in this case, an array with two one-byte element has been allocated. why is it that 1 is said the most significative byte ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:04:44+00:00Added an answer on June 12, 2026 at 7:04 pm

    Don’t forget that the compiler will only write assembly code. If you ignore all the warnings that the compiler, you can examine the assembly code produced by the compiler and figure out what really happens.

    I took this simple program:

    #include <iostream>
    
    int main()
    {
        unsigned endian[2] = { 0, 0 } ;
        long * casted_endian = reinterpret_cast<long*>( endian );
        std::cout << *casted_endian << std::endl;
    }
    

    and I extracted this code using objdump. Let’s decipher it.

     804879c:   55                      push   %ebp
     804879d:   89 e5                   mov    %esp,%ebp
     804879f:   83 e4 f0                and    $0xfffffff0,%esp
     80487a2:   83 ec 20                sub    $0x20,%esp
    

    These are lines are just the prologue of the function, ignore them.

        unsigned endian[2] = { 0, 0 } ;
     80487a5:   c7 44 24 14 00 00 00    movl   $0x0,0x14(%esp)
     80487ac:   00 
     80487ad:   c7 44 24 18 00 00 00    movl   $0x0,0x18(%esp)
     80487b4:   00 
    

    From those 2 lines, you can see that (0x14)%esp is initialized with 0. So you know that the array endian is on the stack, at the address in the register %ESP (stack pointer) + 0x14.

        long * casted_endian = reinterpret_cast<long*>( endian );
     80487b5:   8d 44 24 14             lea    0x14(%esp),%eax
    

    LEA is just an arithmetic operation. EAX now contains %ESP+0x14, which is the address of the array on the stack.

     80487b9:   89 44 24 1c             mov    %eax,0x1c(%esp)
    

    And at the address ESP + 0x1c (which is the location of the variable casted_endian) we put EAX, so the address of first byte of endian.

        std::cout << *casted_endian << std::endl;
     80487bd:   8b 44 24 1c             mov    0x1c(%esp),%eax
     80487c1:   8b 00                   mov    (%eax),%eax
     80487c3:   89 44 24 04             mov    %eax,0x4(%esp)
     80487c7:   c7 04 24 40 a0 04 08    movl   $0x804a040,(%esp)
     80487ce:   e8 1d fe ff ff          call   80485f0 <std::ostream::operator<<(long)@plt>
    

    Then we prepare the call to operator << with the relevant argument without any more checks. So that’s it, the program won’t make any more checks. The type of the variable is completely irrelevant to the machine.

    Now two things can happen when operator<< will read the part of *casted_endian that are not in the array.

    Either its address is in a memory page that is currently mapped, or it is not. In the first case, operator<< will read whatever is at that address without complaining. This will probably write on screen something weird. In the second case, your OS will complain about the program trying to read something that he can’t read, and provoke an interruption. This is the famous segmentation fault.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What does ||= mean? In this previous question, I asked about an
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: How does this bash fork bomb work? Today one of my friend
Possible Duplicate: Why does this() and super() have to be the first statement in
Possible Duplicate: What does the leading semicolon in JavaScript libraries do? I am reading
Possible Duplicate: Does readdir() guarantee an order? I'm guessing this isn't the case, and
Possible Duplicate: Does the order of class definition matter in PHP? class a {
Possible Duplicate: Does reading from stdin flush stdout? C++ Standard guarantees that all data
Possible Duplicate: Does $_REQUEST have security problem? Does anyone have verifiable citations on this?
Possible Duplicate: Does this type of memory get allocated on the heap or the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.