Re: How to determine the way data is stored in memory?




"Abhishek" <abhisheksgumadi@xxxxxxxxx> wrote in message
news:1137952362.943466.83820@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> lets consider that I have defined an integer like this.
> int a=5;
> now taking the fact that an integer is allocated 2 bytes in memory and
> the memory address increases in the left to right direction, which way
> is the 16bit representationof 5 stored..
> i.e 15 is 0000000000001111 representation in binary right? if the
> memory address increases in left to right direction with each memory
> address being capable of holding 8bits (i.e 1 byte)..can u tellme the
> structure in which it is stored..is it like this???
> 00 00 00 00 00 00 11 11
> ^lesser address ^greater address.
> or is it in different format?
> Please elaborate.
> Thank you very much for ur patience.
> Bye

There are two common methods that I know of for determining endianess.
These can be extended to show the actual ordering.
1) using casts, get the char at the address of an int set to one(1), if it's
one then it's little endian

int x=1;
if((*(char*)&x)==1) {/* little endian */ }

2) setup a union with a long and char types, set the long to one(1), if the
low char is one the it's little endian
union {long Long; char Char[sizeof(long)]} u;
u.Long=1;
if(u.Char[0]==1) {/* little endian */}

The second case could be extended to show you the exact ordering by spitting
out the other values of Char[].


Rod Pemberton




.



Relevant Pages

  • Re: malloc()/realloc() - have I got this right?
    ... In one of the questions, you have to write a function to read lines of arbitrary length from the command line, using mallocand reallocto allocate the necessary memory to hold the lines. ... char *getline ... int line_len = 0; ... I'm hoping someone can tell me if I've got this function basically right - have I introduced some bug which is causing the crash, or is it a Windows thing that I don't need to concern myself with? ...
    (comp.lang.c)
  • Re: socket communication: send & receive doesnt work right
    ... So I don't want to send a string as bytes. ... litttle endian then unpredictable results may be obtained. ... public void send_doubles(double vals, int len) throws ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: Alignment, placement new, trap representations
    ... > int i; ... placement new should be used with memory allocated on the freestore. ... This syntax also avoids the signed char issue. ... the archives for "trap representation" "signed char"). ...
    (comp.lang.cpp)
  • Re: BUG: memory where pointer pointing to changed accidently
    ... int isDir; ... void insert(TreeNodePtr *, TreeNodePtr); ... char* token; ... And thus this it will point to some memory you don't ...
    (comp.lang.c)
  • Re: Can I do ANYTHING on allocated memory?
    ... memory, can I do with it whatever I want (as in the example, use memory allocated as char *, as memory where I store integers)? ... int main{ ...
    (comp.lang.c)