Professional Documents
Culture Documents
1-1
Stack
Data Segment
Code Segment
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;
More on Heap
Internal representation of the earlier declaration would be as follows:
FE12
FE12
*p
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
long long
float double
8
4 8
1-10
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
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
Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)
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
Characters (char)
Roman alphabet, punctuation, digits, and other symbols: Can encode within one byte (256 symbols) ASCII encoding (man ascii for details) In C:
= = = =
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