473,763 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determine the way data is stored in memory?

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 representationo f 5 stored..
i.e 15 is 000000000000111 1 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

Jan 22 '06 #1
18 2844
Abhishek wrote:
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 representationo f 5 stored..
i.e 15 is 000000000000111 1 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's nothing really to elaborate -- it depends on the
hardware you're using. These generally come in low- and
high-endian flavours (lower order vs higher order bits first,
respectivelly), but there's nothing proscribing something
weirder.

Cheers

Vladimir
Jan 22 '06 #2
>lets consider that I have defined an integer like this.
int a=5;
now taking the fact that an integer is allocated 2 bytes
That is an assumption that is true only on some machines.

Also be aware that the C standard denies and stomps on and spits
on the assumption that "byte" is equivalent to "octet", although
some implementations do that.
in memory and
the memory address increases in the left to right direction, which way
Memory addresses do not have "left" or "right" direction.
The assumption that an integer is represented as big-endian
is only true on some machines.
is the 16bit representationo f 5 stored..
i.e 15 is 000000000000111 1 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 u is a structure in a UNIX V6 kernel. I don't see how it's
relevant here.
tellme the
structure in which it is stored..is it like this???
00 00 00 00 00 00 11 11
^lesser address ^greater address.
If you are claiming that two bits are stored per byte, and that a
16-bit integer takes up 8 octets, that's not true of any C
implementation.
or is it in different format?
Please elaborate.
Thank you very much for ur patience.


A core dump in binary of the integer you describe might be:

00000000 00001111

ASSUMING: CHAR_BIT = 8, 16-bit integer, and bit-endian.

Gordon L. Burditt
Jan 22 '06 #3
Vladimir S. Oka wrote:
Abhishek wrote:
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 representationo f 5 stored..
i.e 15 is 000000000000111 1 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's nothing really to elaborate -- it depends on the
hardware you're using. These generally come in low- and
high-endian flavours (lower order vs higher order bits first,
respectivelly), but there's nothing proscribing something
weirder.

Cheers

Vladimir

Sorry, hit send too quickly...

The way to determine how your machine stores data:

Initialise an int variable to something with different high- and
low-order bits. Point to it using a char* and read out the
value there. If on your system char is narrower than an int,
you're in luck, and will be able to tell the endianness based
on the value read. if sizeof int == sizeof char, you could try
to use long, instead of int, or any integer type that's wider
than char.

In any case the exercise should be fun... ;-)

Cheers

Vladimir
Jan 22 '06 #4
If your C implementation provides stdint.h you can use this:

void check_endiannes s(void)
{
int32_t b = 1;

int8_t* i;
i = (int8_t*) &b;

if(*i == 1)
{
printf("Little endian\n");
}
else
{
printf("Big endian\n");
}

}

Abhishek wrote:
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...

[snip]

Jan 22 '06 #5
thank you.

Jan 22 '06 #6
can you please explain how can this program validate such an
assumption?
Please explain it to me line by line. I have not used that header file
anytime before.
Thank you very much for your interest.
bye

Jan 22 '06 #7

"Abhishek" <ab************ *@gmail.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
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 representationo f 5 stored..
i.e 15 is 000000000000111 1 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


Jan 22 '06 #8
Now look:

void check_endiannes s(void)
{

int32_t b = 1;

int8_t* i;
i = (int8_t*) &b;

if(*i == 1)
{
printf("Little endian\n");
}
else
{
printf("Big endian\n");
}

}

int32_t b = 1;
This defines an 32 bit integer and assigns it's value to 1. Now imagine
how this happens in memory (hex notation):
0x00 0x00 0x00 0x01 -> This how the actual number is in hex
If it is big-endian(the number ends in the big end, i.e. the last
memory location from left to right) the number will be stored as:

Addr0 Addr1 Addr2 Addr3
0x00 0x00 0x00 0x01

If it was little end, it will end in the little end(smallest address),
so it will be:

Addr0 Addr1 Addr2 Addr3
0x01 0x00 0x00 0x00

Then int8_t* i; creates a pointer to a byte(an octet in this case) and
this i = (int8_t*) &b; assigns Addr0 to i. So I have the contents of
Addr0 in my *i variable.

Now look up again. If *i == 1 it means that the number ended on the
little end => little endian, if not => big endian. Do you understand
the code now?

Regards

Abhishek wrote:
can you please explain how can this program validate such an
assumption?
Please explain it to me line by line. I have not used that header file
anytime before.
Thank you very much for your interest.
bye


Jan 22 '06 #9
Abhishek wrote:
can you please explain how can this program validate such an
assumption?

Can you please lean to quote - how can someone explain a program if you
don't quote it?

The header provides a number of C99 typdefs for exact and inexact
integer types, specifying the size in bits. So a uint32_t is a 32 bit
unsigned int.

The program simply assigns 1 to a 32 bit int and tests to see if the
first byte is 1, if so, you have a little endian machine.

Ian

--
Ian Collins.
Jan 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6149
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
5
2244
by: Hennie de Nooijer | last post by:
Hi, This is a diffcult issue to explain. I hope to make my problem clear to you. SITUATION I'm building A SLA Query for a customer. This customer has an awkward way to determine the SLA results ;-) Depending on a category which is stored in a headertable (Requests) a field and logic is determined how to get a proper Close_Date. This Close_date can be the closedate of the request. It is also possible that the close_date is a certain...
9
4031
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the construct ptr = realloc(ptr, size); *ptr = malloc(string_length); strncpy(ptr, src, string_length); to call realloc() multiple times. This should be ok, right?
1
5946
by: James Wansley | last post by:
I am currently running a c# service that dynamically loads assemblies using System.Reflection.Assembly.LoadFrom("aDll.dll"). These assemblies are stored in an arraylist after they are loaded. I would like to periodically report the memory usage of each assembly within the arraylist. I tried System.Diagnostics.ProcessModule.ModuleMemorySize, but this only shows the size of the structures within the .dll, not the current memory usage. ...
2
1166
by: D | last post by:
I have normalized data in sql server in a table like (testset, testnumber, result) . There can be 5-10 tests in a testset and anywhere from 1500 rows to 100000 rows of testsets. I need to start searching for patterns within the data such as the total number of times 15,16 & 17 occurred. My first thoughts were to write a stored procedure but that hasn't proved to be the best as I am still trying to get that to work. In the essence of...
3
2105
by: Scottie_do | last post by:
I have a 20meg in-memory stack-based array and I'd like to normalise in the client's memory... then add foriegn keys, and display results on a datagrid. I discovered that converting the stack to a datatable my memory utilisation increases to 120 megs. (lots of overhead) Couple of questions 1)- Is that memory increase typical? All I want to do is bind it to a datagrid. 2)- Suppose I dump it to a CSV, or SQL. Is it possible to only...
5
3407
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character text field. I want to know which Data type I need to use so that it doesnt waste memory. thanks in advance, rAinDeEr
7
2862
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please explain me what does the above statement mean? <script runat="server"> Class Clock
1
1257
by: AMDRIT | last post by:
Occasionally I try my hand at a simple data storage engine. Today I ran across an article on the web http://msdn2.microsoft.com/en-us/library/aa289151(vs.71).aspx, and it got me thinking again. There doesn't seem to be a definative source, at least on in layman terms, on this subject. Here is what I believe to know so far. (As if I was to model after SQL Server).
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5271
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.