Sie sind auf Seite 1von 4

1

1
COMP 320
Nemory layout, pointers, 8 their effects
on representation of simple data
2
Readings
- Read [BO| 3.10.
3
Big Picture
C gives you access to underlying data
representations 8 layout.
Needed for systems programming.
!nconvenient for application programming.
Necessary to understand.
+
Big Picture: Reminder
Nemory = sequence of finite-sized storage cells
!ndexed by numbers
- addresses
- starting with 0
What size cells?
...
0
max
5
Big Picture: Addresses
Size of storage cells typically viewed as bytes.
- Usually smallest addressable unit of memory.
Few machines can directly access bits individually.
- Addresses also known as byte-addresses.
Sometimes viewed as words.
- Nost common size of addressable unit of memory.
- When applicable, use word-addresses.
- Word-address m = byte-address (m #bytesfword)
6
Memory Addresses (Pointers)
Special case of bounded-size natural numbers
- Naximum memory limited by processor word-size
- 2
32
bytes = +GB, 2
6+
bytes = 16 exabytes
A pointer is just another kind of value.
2
7
Pointer Operations in C
Creation
& variable Returns variable's memory address.
Dereference
* pointer Returns contents stored at address.
!ndirect assignment
* pointer = value Stores value at address.
Of course, still have...
Assignment
pointer_variable = pointer Stores pointer in variable.
8
Pointer Operations in C
Pointer Arithmetic
pointer + number pointer - number
E.g., pointer + 1 Adds 1 something to a pointer.
char * p;
char a;
char b;
p = &a;
p += 1;
int * p;
int a;
int b;
p = &a;
p += 1;
!n each, p now points to b.
(Assuming compiler doesn't
reorder variables' space.)
Adds 1*sizeof(char)
to the memory address.
Adds 1*sizeof(int)
to the memory address.
Pointer arithmetic is usually confusing and bad style.
9
char *
Standard C type for strings:
H e l l o , w l o r d .\n\0
s
char * s = Hello, world.\n;
String terminator.
10
void *
A generic type for pointer to anything".
void * p;
int i;
char c;
p = (void *) &i;
p = (void *) &c;
Lose all information about what type of thing is pointed to.
- Reduces effectiveness of type-checking.
- Can't use pointer arithmetic.
Useful in limited circumstances.
- Will need in last project.
11
Example 1
Here be dragons!
Example of syntax and operations (not of good style):
int1 1036
int2 8
After
+
th
line:
int_ptr1
int_ptr2
int1 8
int2 8
At end:
int_ptr1
int_ptr2
Changed int1!
int int1 = 1036; /* some data to point to */
int int2 = 8;
int * int_ptr1 = & int1; /* get addresses of data */
int * int_ptr2 = & int2;
* int_ptr1 = * int_ptr2; /* copy between addresses */
low
high
addresses
12
Example 2
Fails to type check. int_ptr2 is not an int.
Same as previous example.
int int1 = 1036; /* some data to point to */
int int2 = 8;
int * int_ptr1 = & int1; /* get addresses of data */
int * int_ptr2 = & int2;
* int_ptr1 = int_ptr2;
* int_ptr1 = int2;
What happens?
?

3
13
Example 3
Fails to type check. int_ptr2 is not an int *.
Changes int_ptr1. Doesn't change int1.
int int1 = 1036; /* some data to point to */
int int2 = 8;
int * int_ptr1 = & int1; /* get addresses of data */
int * int_ptr2 = & int2;
int_ptr1 = * int_ptr2;
int_ptr1 = int_ptr2;
What happens?
?

1+
The Simplest Pointer in C
Special constant pointer NULL
- Points to no data.
- Dereferencing illegal. Causes segmentation fault.
- To define, include <stdlib.h> or <stdio.h>.
15
Pass-by-Reference
#include <stdio.h>
void
set_x_and_y(int * x,
int * y)
{
*x = 1001;
*y = 1002;
}
void
f()
{
int a = 1;
int b = 2;
set_x_and_y(&a,&b);
printf("a=%d, b=%d\n",a,b);
}
1
2
a
b
x
y
1001
1002
16
Pointers
Will discuss more later, in conjunction with.
- data structures
- memory management
17
Data Type Sizes in C
!n C, every data type must have a fixed size.
- Known at compile-time.
- Processor- and compiler-dependent.
- Available to program via compiler-evaluated sizeof().
- SPARC examples:
sizeof(int) = 4
sizeof(long int) = 4
sizeof(long long int) = 8
sizeof(char) = 1
sizeof(enum ) = 4
sizeof(any pointer) = 4
18
Alignment
Data typically aligned in memory, e.g.,
- Word-aligned: values at addresses divisible by +.
- Byte-aligned: value at addresses divisible by 1.
- Simplifies address calculations.
- Typically required by assembly language.
Nore details later.
c1 si
c2
0x1000
0x100+
i 0x1008
char c1;
short int si;
char c2;
int i;
4
19
Endianness
When storing word 0xDEADBEEF at address 0x1000,
in what order are bytes stored?
Big-endian Little-endian
Other endianness
possible, but no longer
used.
DE
AD
BE
EF
0x1000
0x1001
0x1002
0x1003
EF
BE
AD
DE
0x1000
0x1001
0x1002
0x1003
SPARC, N!PS, PPC, 680x0, ... !A-32, Alpha, ...
Particularly important
issue in networking.
20
Memory Layout Convention (UNIX)
0
max
Unused
Text (Code)
Data
Program stack
Allows NULL (0x0)
to be treated specially.
Stack's use explained
later. For implementing
function calls.
Error if overlap.
Each process gets a
separate address space
of 0..max.
How? virtual memory,
as explained in ELEC
220.

Das könnte Ihnen auch gefallen