Before starting with Arrays and Pointers lets have a look on basics of operating systems (OS), their types and their use in the programming language. An operating system (OS) is a collection of software that manages computer hardware resources and provides common services for computer programs. Types of OS are DOS, UNIX (LINUX, CentOS, AIX (IBM), Red Hat, Ubuntu, etc), Windows (95, 98, 2K, Me, XP, Vista, 7, 8, etc), SUN, Mac, etc. People generally use windows as a operating system, since it is user friendly and computation is done easily in it, so here we'll be talking about windows OS only. Windows uses visual studio as an integrated development environment (IDE) built by microsoft. Visual studio is used, as we can make changes in applications within the operating system. This OS comes under 32-bits and 64 bits earlier 16-bit was used but its now not used in a programming language. All OS has a microprocessor (µp) and they also comes in 32 and 64 bits. The windows OS of either 32 bits or of 64 bits can work in 64 bits of the µp. The use of OS is to operate applications and they too come in 32 and 64 bits. An application with 32 bit can be operated in 32 bits or 64 bits of the OS but an application of 64 bits cannot be operated in 32 bits OS rather it is operated in 64 bit OS.
Visual studio can create an app of either 32-bits, 64-bits, Itanium Architecture (produced by intel) and any (can be 32 bits, 64-bits or IA). In my earlier blog of datatypes, I talked about the sizes of datatypes, it varies across the OS.
Below is a table of data models:
ARRAYS:
Below is a table of data models:
ARRAYS:
RAM is a 1-dimensional sequence of locations. Each location has its own address and size. In a programming language when we define a variable, automatically RAM provides the space to the variable.
Lets see with an example:
int a;
Since we know "int" datatype is of 4 bytes so here it requests RAM to allocate 4 bytes in the memory. The location is provided with a name "a" (termed as variable) and then the compiler stores the integer into that memory location.
In case of arrays the memory occupied depends on the number of elements given by the user.
int a[10];
Here the number of elements selected is 10 and since the the datatype is of 4 bytes so here it requests RAM to provide 4*10 bytes i.e. 40 bytes of allocation in the memory. The array named "a" has 10 elements in it and each element is of 4 bytes so the memory used by the RAM is 40 bytes.
NOTE: Name of the array decays into address of the 1st element of the array.
int a;
printf("%d",a)
printf("%d",a)
In the above code, the printf command will print the address of the variable, as the value is not assigned to the variable so by default it will show the address.
In 2-D array the memory allocation is dependent upon the n by m matrix:
int a[n][m];
Here n*m*4 bytes of memory is allocated in the RAM.
For calculation of 1-D array in a memory can be represented as following:
a[0]--> a + 0* sizeof (a)
a[5]--> a + 5* sizeof (a)
Here '[ ]' (square brackets) are called as indexing operators.
For calculation of 2-D array there are two orders:
- Row major order
- Column major order
a[i][j]--> a + (i * m + j)* sizeof (a)
where a= base address
i, j= index values
m= total number of columns
for column major order the memory calculation is done as follows:
a[i][j]--> a + (i + j * n)* sizeof (a)
where a= base address
i, j= index values
n= total number of rows
But here the question arises that these operations can not be done simultaneously, then what does C/C++ programming will support from the above? So the answer is C/C++ lang supports row major order where as column major order is supported by matlab.
Now if we assign a value to a variable its stored in 4 bytes (32-bits) of memory allocated by RAM in a binary form. Here's an example for understanding it more simpler form:
int a=5;
Here two things pops out LSB and MSB. MSB is the most significant bit and LSB is the least significant bit. And the allocation of the memory starts from LSB to MSB. In our case LSB is 101 and rest are 0 and is represented as:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
MSB LSB
POINTERS:
Pointers are those which stores the address. Consider the declaration,
int a=5;
int *ptr;
ptr= &a;
Uses of the '*' operator:
- Multiplication
- create/declare a pointer
- dereference a pointer
Uses of the '&' operator:
- Logical AND (&&)
- Bitwise AND (&)
- referencing a pointer
NOTE: Size of all pointers is 32-bits on a 32-bit OS and size of all pointers is 64 bits on a 64 bit OS.
Lets see some more commands on pointers which will let you know more about the pointers:
int a=5; 100[ 5 ]a
int *ptr= &a; 1000[ 100 ]ptr
int **aptr= &ptr; 10000[ 1000 ]aptr
int ***aaptr= &aptr; 20000[ 10000 ]aaptr
*ptr means the value stored at the address contained in ptr or ptr is a pointer to integer a.
**aptr means aptr is a pointer to a pointer to integer a.
***aaptr means aaptr is a pointer to a pointer to a pointer to integer a.
int size[10];\\valid operation
int a[size];\\invalid operation
In the above program the first command given is valid because here we directly assigning a constant value 10 in the indexing operator. But in the second case since size is not a constant variable or a value so it is not a valid operation. It can be done in the following manner:
const int size[10];
int a[size];
Now here we have a constant variable size with 10 elements in it, so the next command is valid now as size is a constant integer. The program further can also be coded as:
#define size 10
int a[size];
Here #define defines the global constant of value of size to be 10 throughout the program. So the next line of the code is also valid for execution.

No comments:
Post a Comment