More about using Variables and Datatypes

Let us continue from my previous post. I said I have a question. Lets look into it.

Memory location is mapped to variable name. But will it map all memory of all 4 bytes to Variable Name. Answer is, it actually maps only the memory location of lower most byte. By the way, Lower most byte is the least significant byte.

Say I have 1023 (binary equivalent - 00000011 11111111). We assume that Most Significant Byte(00000011) is stored in memory location 2001 and 11111111 is stored in memory location 2000. Then variable name will be mapped to memory location 2000.

And why does it map to 2000. Why not 2001 ?

Before coming to the answer we should know some basic terms.
1 bit - 0 or 1
1 byte - 8 bits

Every system has some basic memory storage called WORD. A word is grouping of bytes (It may be 2,4 etc. and it depends on the system.) What the system does is, it gives memory address to each WORD. Assume a system uses 4 bytes for a word, and I write a a C program, where I declare a character variable (which takes 1 byte) and declare 2 more integer variable (which takes 4 bytes). Usually when the program is very small with only 2 variables, during execution those variables are stored in continous memory locations. So I will have a look at the address mapped to the variable. What i see is that memory address 2000 is assigned for character variable and memory addresss 2004 and 2008 is assigned for the two integer variables. So even though the character variables takes only 1 bytes, actually the memory used up is 4 bytes (I dont know why, may be due to the basic memory storage concept).

Note : I will come up with the program used for this purpose later while explaining memory allocated for data types.

So memory locations 2000, 2001, 2002, 2003 where in use. But actually when we store something in the character variable, it gets stored in 2000. Hence Instead of mapping 2003 to the character variable, 2000 is mapped to the character varaible. Also while making basic operations like addition, we usually start from the Least significant digit. So it will be easy if the program fetches the LSB first and then proceed to the MSB. Inorder to avoid the overload in finding the LSB, memory location of LSB is mapped to the variable.

We will better understand this concepts while we come across UNIONS (a secondary data type).

0 comments:

Post a Comment

 
Template designed using TrixTG