1

I'm trying to pullout values from a uint8_t array. But I'm having troubles understanding how these are represented in the memory.

#include <cstdio>
#include <cstring>
#include <stdint.h>
int main(){
  uint8_t tmp1[2];
  uint16_t tmp2 = 511;//0x01 + 0xFF = 0x01FF
  tmp1[0] = 255;//0xFF
  tmp1[1] = 1;//0x01
  fprintf(stderr,"memcmp = %d\n",memcmp(tmp1,&tmp2,2));


  fprintf(stderr,"first elem in uint8 array  = %u\n",(uint8_t) *(tmp1+0));
  fprintf(stderr,"first elem in uint8 array  = %u\n",(uint8_t) *(tmp1+1));

  fprintf(stderr,"2xuint8_t as uint16_t  = %u\n",(uint16_t) *tmp1);
  return 0;
}

So i have an 2 element long array of datatype uint8_t. And I have a single variable uint16_t. So when I take the value 511 on my little endian machine, I would assume this is layed out in memory as

0000 0001 1111 1111

But when I use memcompare it looks like it is actually being represented as

1111 1111 0000 0001 

So little endianness is only used "within" each byte? And since the single bit that is set in the tmp1[1] counts as 256, even though it is further "right" in my stream. The values for each byte (not bit), is therefore bigendian? I'm abit confused about this.

Also if I want to coerce an fprint, to printout, my 2xuint8_t as a single uint16_t, how do I do this. The code below doesn't work, it only printouts the first byte.

fprintf(stderr,"2x uint8_t as uint16_t  = %u\n",(uint16_t) *tmp1);

Thanks in advance

2
  • 1
    The CPU understands how to do little endian, you don't.
    – Patrick
    Oct 24, 2011 at 19:02
  • 1
    @Patrick This is as long as you do not communicate with other machines, which have another endiannessm or just do clever byte tricks. While you do not actually have to bother yourself with it in most cases, it is an important thing to understand and keep in mind. Oct 24, 2011 at 19:20

3 Answers 3

4

Your assumption of what you expect is backwards. Your observation is consistent with little-endian representation. To answer your last question, it would look like this:

fprintf(stderr,"2x uint8_t as uint16_t  = %u\n",*(uint16_t*)tmp1);
1

Don't think of endianness as "within bytes". Think of it as "byte ordering". (That is, the actual bit ordering never matters because humans typically read values in big-endian.) If it helps to imagine that the bits are reversed on a little-endian machine, you can imagine it that way. (in that case, your example would have looked like 1111 1111 1000 0000, but as I said, humans don't typically read numbers such that the most significant values are to the right...but you might want to imagine that's how the computer sees things, if it helps you understand little-endian.)

On a little endian machine, 0xAABBCCDD would be seen as 0xDD 0xCC 0xBB 0xAA in memory, just as you are seeing. On a big-endian machine (such as a PPC box) you'd see the same ordering in-memory as you see when you write out the 32-bit word.

1

First, if you want be 100% sure that your variables are stored in right order in memory, you should put them in a struct.

Then note that memcmp() treats input you give it as a sequence of bytes, since it has no assumptions regarding the nature of the data you give it. Think, for example, of the following code:

#include <stdio.h>
#include <stdint.h>

int main(int argc, char** argv) {
    int32_t a, b;
    a = 1;
    b = -1;
    printf( "%i\n", memcmp( &a, &b, sizeof( int32_t ) ) );
}

It outputs -254 on my little-endian machine regardless of fact that a > b. This is because it has no ideas about what the memory actually is, so it compares them like an array of uint8_t.

If you actually want to visualize how the data is represented on your machine, you may first use fwrite to write a struct into the file and then open it with your favorite hex editor (in my experience, wxHexEditor is great in telling you how the data looks if it is X-bit Y-endian ingeter). Here's the source:

#include <stdio.h>
#include <stdint.h>

typedef struct {
    uint8_t tmp1[2];
    uint16_t tmp2;
} mytmp;

int main(int argc, char** argv) {
    mytmp tmp;
    tmp.tmp1[0] = 255;
    tmp.tmp1[1] = 1;
    tmp.tmp2 = 511;

    FILE* file = fopen( "struct-dump", "w" );
    fwrite( &tmp, sizeof( mytmp ), 1, file );
    fclose( file );
}

As for treating an array of uint8_t as uint16_t, you would probably want to declare a union or use pointer coercion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.