You are on page 1of 19

C-Programming

1-1

Program Address Space


Any program you run has, associated with it, some memory which is divided into: Code Segment Data Segment (Holds Global Data) Stack (where the local variables and other temporary information is stored) Heap
The Heap grows downwards Heap

The Stack grows upwards

Stack

Data Segment

Code Segment

Local Variables:Stack Allocation


When we have a declaration of the form int a;: a variable with identifier a and some memory allocated to it is created in the stack. The attributes of a are: Name: a Data type: int Scope: visible only inside the function it is defined, disappears once we exit the function Address: address of the memory location reserved for it. Note: Memory is allocated in the stack for a even before it is initialized. Size: typically 4 bytes Value: Will be set once the variable is initialized Since the memory allocated for the variable is set in the beginning itself, we cannot use the stack in cases where the amount of memory required is not known in advance. This motivates the need for HEAP

Pointers
We know what a pointer is. Let us say we have declared a pointer int *p; The attributes of a are: Name: p Data type: Integer address Scope: Local or Global Address: Address in the data segment or stack segment Size: 32 bits in a 32-bit architecture We saw how a fixed memory allocation is done in the stack, now we want to allocate dynamically. Consider the declaration: int *p;. So the compiler knows that we have a pointer p that may store the starting address of a variable of type int. To point p to a dynamic variable we need to use a declaration of the type p = new int;

Pointers : Heap Allocation


Dynamic variables are never initialized by the compiler, so it is a good practice to initialize it.

int *p; p = new int; *p = 0;

In more compact notation:

int *p = new int(0);

More on Heap
Internal representation of the earlier declaration would be as follows:
FE12

FE12

*p

Pointer variable: Data Segment

Dynamic variable: Heap

Now we can delete the dynamic variable from the heap using delete p; Now 2 bytes of memory were freed but the pointer p isnit erased and it can be used to initialize another dynamic variable.

Variable

Data type

Simple Data types in C


Only really four basic types: char int (short, long, long long, unsigned) float double

Size of these types on CLEAR machines:


Type Size (bytes) 1 4 2 8

Sizes of these types vary from one machine to another!

char int short long

long long
float double

8
4 8

Internal Format of Type int and Type double

1-10

Memory(a) Before and (b) After Execution of a Program

1-11

Boolean Type
A conditional expression evaluates to a value of true or false These are the two values for the Boolean type named after the mathematician George Boole When a conditional expression is evaluated, it results in a value of type Boolean In the C programming language there is no explicit Boolean type. Instead, false is 0 and true is any non-zero value. Many C programs use a #define macro to set a Boolean variable to true or false #define FALSE 0 #define TRUE 1 Conditional expressions are also called Boolean expressions A conditional expression is used whenever a true or false decision needs to be made about the value of certain variables or constants in a program
A conditional expression in C may contain any valid mathematical expression It may also contain relational operators and logical operators In addition, it may contain one or more function calls that return a value

Expression Contents

Byte-Oriented Memory Organization


Programs Refer to Virtual Addresses Conceptually very large array of bytes Actually implemented with hierarchy of different memory types SRAM, DRAM, disk Only allocate for regions actually used by program In Unix and Windows NT, address space private to particular process Program being executed Program can clobber its own data, but not that of others Compiler + Run-Time System Control Allocation Where different program objects should be stored Multiple mechanisms: static, stack, and heap In any case, all allocation within single virtual address space

Encoding Byte Values


Byte = 8 bits Binary 000000002 to 111111112 Decimal: 010 to 25510 Hexadecimal 0016 to FF16 Base 16 number representation Use characters 0 to 9 and A to F Write FA1D37B16 in C as 0xFA1D37B Or 0xfa1d37b

0 1 2 3 4 5 6 7 8 9 A B C D E F

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Machine Words
Machine Has Word Size Nominal size of integer-valued data Including addresses Most current machines are 32 bits (4 bytes) Limits addresses to 4GB Becoming too small for memory-intensive applications High-end systems are 64 bits (8 bytes) Potentially address 1.8 X 1019 bytes Machines support multiple data formats Fractions or multiples of word size Always integral number of bytes

Word-Oriented Memory Organization

Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)

32-bit 64-bit Words Words


Addr = 0000 ?? Addr = 0000 ?? Addr = 0004 ??

Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015

Addr = 0008 ??

Addr = 0008 ??

Addr = 0012 ??

Data Representations
Sizes of C Objects (in Bytes) C Data Type Compaq Alpha int 4 long int 8 char 1 short 2 float 4 double 8 long double 8 char * 8 Or any other pointer

Typical 32-bit 4 4 1 2 4 8 8 4

Intel IA32 4 4 1 2 4 8 10/12 4

Characters (char)
Roman alphabet, punctuation, digits, and other symbols: Can encode within one byte (256 symbols) ASCII encoding (man ascii for details) In C:

char char char char

a_char newline_char tab_char backslash_char

= = = =

a; \n; \t; \\;

ASCII

Special
characters | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 |104 |112 |120 NUL| 1 BS | 9 DLE| 17 CAN| 25 SP | 33 ( | 41 0 | 49 8 | 57 @ | 65 H | 73 P | 81 X | 89 ` | 97 h |105 p |113 x |121 SOH| 2 HT | 10 DC1| 18 EM | 26 ! | 34 ) | 42 1 | 50 9 | 58 A | 66 I | 74 Q | 82 Y | 90 a | 98 i |106 q |114 y |122

control

From man ascii:


STX| 3 NL | 11 DC2| 19 SUB| 27 " | 35 * | 43 2 | 51 : | 59 B | 67 J | 75 R | 83 Z | 91 b | 99 j |107 r |115 z |123 ETX| 4 VT | 12 DC3| 20 ESC| 28 # | 36 + | 44 3 | 52 ; | 60 C | 68 K | 76 S | 84 [ | 92 c |100 k |108 s |116 { |124 EOT| 5 NP | 13 DC4| 21 FS | 29 $ | 37 , | 45 4 | 53 < | 61 D | 69 L | 77 T | 85 \ | 93 d |101 l |109 t |117 | |125 ENQ| 6 CR | 14 NAK| 22 GS | 30 % | 38 - | 46 5 | 54 = | 62 E | 70 M | 78 U | 86 ] | 94 e |102 m |110 u |118 } |126 ACK| 7 SO | 15 SYN| 23 RS | 31 & | 39 . | 47 6 | 55 > | 63 F | 71 N | 79 V | 87 ^ | 95 f |103 n |111 v |119 ~ |127 BEL| SI | ETB| US | ' | / | 7 | ? | G | O | W | _ | g | o | w | DEL|

You might also like