Top Banner
C programming or Fun with pointers Tutorial #2 CPSC 261
22

C programming or Fun with pointers Tutorial #2 CPSC 261.

Jan 01, 2016

Download

Documents

Juliana Haynes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C programming or Fun with pointers Tutorial #2 CPSC 261.

C programmingor

Fun with pointers

Tutorial #2CPSC 261

Page 2: C programming or Fun with pointers Tutorial #2 CPSC 261.

Memory of a C program

• What does a C program need memory for?– Code– Static data– Dynamic data (heap)– Dynamic data (stack)– Libraries (dynamically linked)

Page 3: C programming or Fun with pointers Tutorial #2 CPSC 261.

Memory of a C program

• How is this memory laid out?

Page 4: C programming or Fun with pointers Tutorial #2 CPSC 261.

A simple memory model

• Memory manipulated by a C program is just a bunch of bytes

• They start at some address (0?)• And end at some other address (2^64 - 1?)

...

Page 5: C programming or Fun with pointers Tutorial #2 CPSC 261.

Char pointers• Variables with type char * fit this model nicely

char *p; p = (char *) 0;

0 1 2 3 4 5 6

p

...

Page 6: C programming or Fun with pointers Tutorial #2 CPSC 261.

Char pointers• Using the pointer accesses one byte

char *p; p = (char *) 0; *p = 23;

23

0 1 2 3 4 5 6

p

...

Page 7: C programming or Fun with pointers Tutorial #2 CPSC 261.

Char pointers• Incrementing the pointer works

char *p; p = (char *) 0; p++;

0 1 2 3 4 5 6

p

...

Page 8: C programming or Fun with pointers Tutorial #2 CPSC 261.

Other pointers• Other pointer types don’t fit quite so nicely

int *p; p = (int *) 0; How big is *p?

0 1 2 3 4 5 6

p

...

Page 9: C programming or Fun with pointers Tutorial #2 CPSC 261.

Other pointers• Incrementing the pointer

int *p; p = (int *) 0; p++;

0 1 2 3 4 5 6

p

...

Page 10: C programming or Fun with pointers Tutorial #2 CPSC 261.

Pointer rules

• The value in a pointer is the smallest address of all the bytes accessed through the pointer

int *p = (int *) 0;• The bytes accessed are 0, 1, 2, and 3 (ints are

4 bytes)long *p = (long *) 0;• The bytes accessed are 0, 1, 2, 3, 4, 5, 6, and 7

(longs are 8 bytes)

Page 11: C programming or Fun with pointers Tutorial #2 CPSC 261.

Other pointers• Using the pointer – first try

int *p; p = (int *) 0; *p = 23;

0 0 0 23

0 1 2 3 4 5 6

p

...

Page 12: C programming or Fun with pointers Tutorial #2 CPSC 261.

Other pointers• Using the pointer – second try

int *p; p = (int *) 0; *p = 23;

23 0 0 0

0 1 2 3 4 5 6

p

...

Page 13: C programming or Fun with pointers Tutorial #2 CPSC 261.

Which one is right?

23 0 0 0

0 1 2 3 4 5 6

...

0 0 0 23

0 1 2 3 4 5 6

...

Page 14: C programming or Fun with pointers Tutorial #2 CPSC 261.

Unfortunately, both!!

• Endian-ness– In a little-endian computer, the least-significant

byte of a multi-byte quantity is stored in the byte with the smallest address

– In a big-endian computer, the most-significant byte of a multi-byte quantity is stored in the byte with the smallest address

Page 15: C programming or Fun with pointers Tutorial #2 CPSC 261.

Other pointers• Using the pointer – big-endian

int *p; p = (int *) 0; *p = 23;

0 0 0 23

0 1 2 3 4 5 6

p

...

Page 16: C programming or Fun with pointers Tutorial #2 CPSC 261.

Other pointers• Using the pointer – little-endian

int *p; p = (int *) 0; *p = 23;

23 0 0 0

0 1 2 3 4 5 6

p

...

Page 17: C programming or Fun with pointers Tutorial #2 CPSC 261.

X86 (and X86_64) is little-endian

• The “standard” order for data communicated between computers is big-endian (also called “network byte order”)

Page 18: C programming or Fun with pointers Tutorial #2 CPSC 261.

How to swap bytes?• Go from one endianness to the other?

23 45 19 66

0 1 2 3

66 19 45 23

0 1 2 3

Page 19: C programming or Fun with pointers Tutorial #2 CPSC 261.

Reading data

• For character data:int *p = ...; // Some valid initializationfscanf(f, “%d”, p);

• For binary datafread(p, sizeof(int), 1, f);

Page 20: C programming or Fun with pointers Tutorial #2 CPSC 261.

How many bytes get read?

fscanf(f, “%d”, p);• 1 or more byte depending on the input

5232123123123

fread(p, sizeof(int), 1, f);• Always 4 bytes, why?

Page 21: C programming or Fun with pointers Tutorial #2 CPSC 261.

Writing data

• For character data:int *p = ...; // Some valid initializationfprintf(f, “%d”, *p); // Note the *

• For binary datafwrite(p, sizeof(int), 1, f);

Page 22: C programming or Fun with pointers Tutorial #2 CPSC 261.

How many bytes get written?

fprintf(f, “%d”, *p); • 1 or more bytes depending on the value of *p

5232123123123

fwrite(p, sizeof(int), 1, f);• Always 4 bytes, why?