Successfully reported this slideshow.

Memory management of datatypes


Published on

how & where data types r stored

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Memory management of datatypes

  1. 1. Prepared By: Savani Nirali Sanghani Monika Patel Pooja How and Where C datatypes are stored in Memory
  2. 2. C Data types:  Basic Types: They are arithmetic types and consists of the two types: (a) integer types and (b) floating-point types.  Enumerated types: They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.  The type void: The type specifier void indicates that no value is available.  Derived types: They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function
  3. 3. Integer Types: Type Storage size Value range Char 1 byte -128 to 127 or 0 to 255 Unsigned Char 1 byte 0 to 255 Signed Char 1 byte -128 to 127 int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 Unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295 short 2 bytes -32,768 to 32,767 Unsigned short 2 bytes 0 to 65,535 long 4 bytes -2,147,483,648 to 2,147,483,647 Unsigned long 4 bytes 0 to 4,294,967,295
  4. 4.  Memory representation of char data type in C Char data types may be signed or unsigned. Size of char data type is 8 bit. Both signed and unsigned have different memory representation. Memory representation of unsigned char: In unsigned char all 8 bit is used as data bit Memory representation of unsigned char a= 7; Binary equivalent of 7 is: 111 For 8 bit we will add 5 zero in the left side i.e. 00000111. In the memory:  Here MSD stand for most significant digit and LSD list significant digit.
  5. 5.  Memory representation of signed char: 1 bit: signed bit 7 bit: data bit Note: In C, negative number is stored in the 2’s complement format. Signed bit is 0: Number is positive. Signed bit is 1: Number is negative. Memory representation of char a=7; Binary equivalent of 7 is: 111 For 8 bit we will add 5 zero in the left side i.e. 00000111. Memory representation:
  6. 6.  Memory representation of char a=-7; Binary equivalent of 7 is 111 For 8 bit we will add 5 zero in the left side i.e. 00000111. Since a is negative number so it will store in the memory in the 2’s complement format  1’s complement of a: 11111000 + 1 ____________  2’s complement of a: 11111001 Memory representation:
  7. 7. Floating Point Types: Type Storage value Value range Precision Float 4 byte 1.2E-38 to 3.4E+38 6 decimal places double 8 byte 2.3E-308 to 1.7E+308 15 decimal places Long double 10-byte 3.4E-4932 to 1.1E+4932 19 decimal places
  8. 8.  To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Following is an example: #include <stdio.h> #include <conio.h> #include <float.h> int main(){ printf("Storage size for int : %d n", sizeof(int)); printf("Storage size for float : %d n", sizeof(float)); printf("Minimum float positive value: %En", FLT_MIN ); printf("Maximum float positive value: %En", FLT_MAX ); return 0; } When you compile and execute the above program it produces the following result on Linux: Storage size for int : 4 Storage size for float : 4 Minimum float positive value: 1.175494E-38 Maximum float positive value: 3.402823E+38
  9. 9. The Void Type: Serial Number : Types and Description 1 Function returns as void : There are various functions in C which do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status); 2 Function arguments as void There are various functions in C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void); 3 Pointers to void A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
  10. 10. Enum [Enumerated] data types:  Syntax: enum identifier {value1, value2,.... Value n};  enum is ” Enumerated Data Type “.  enum is user defined data type  In the above example “identifier” is nothing but the user defined data type .  Value1,Value2,Value3….. etc creates one set of enum values.  Using “identifier” we are creating our variables.
  11. 11. Memory Layout  Text or Code Segment Code segment, also known as text segment contains machine code of the compiled program. The text segment of an executable object file is often read-only segment that prevents a program from being accidentally modified.  Data Segments Data segment stores program data. This data could be in form of initialized or uninitialized variables, and it could be local or global. Data segment is further divided into four sub-data segments (initialized data segment, uninitialized or .bss data segment, stack, and heap) to store variables depending upon if they are local or global, and initialized or uninitialized.
  12. 12. Memory Layout (Cont.)  Initialized Data or Data Segment Initialized data or simply data segment stores all global, static, constant, and external variables (declared with extern keyword) that are initialized beforehand.  Uninitialized Data or .bss Segment Contrary to initialized data segment, uninitialized data or .bss segment stores all uninitialized global, static, and external variables (declared with extern keyword). Global, external, and static variable are by default initialized to zero. Object file formats distinguish between initialized and uninitialized variables for space efficiency; uninitialized variables do not have to occupy any actual disk space in the object file.
  13. 13. Memory Layout (Cont.) Figure 1 : Memory Layout of C Program
  14. 14. Memory Layout (Cont.)  Stack Segment Stack segment is used to store all local variables and is used for passing arguments to the functions along with the return address of the instruction which is to be executed after the function call is over. Local pointers are stored in stack segment.  Heap Segment Heap segment is also part of RAM where dynamically allocated variables are stored. In C language dynamic memory allocation is done by using malloc and calloc functions. Global pointers are automatically stored in Heap segment. When some more memory need to be allocated using malloc and calloc function, heap grows upward as shown in Figure 1.
  15. 15. Big and Little Endian  Big Endian : In big endian, you store the most significant byte in the smallest address.  Little Endian : In little endian, you store the least significant byte in the smallest address.
  16. 16. THANK YOU