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

C programming or Fun with pointers

  • Upload
    didina

  • View
    61

  • Download
    0

Embed Size (px)

DESCRIPTION

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). Memory of a C program. How is this memory laid out?. A simple memory model. - PowerPoint PPT Presentation

Citation preview

X86_64 programming

C programmingorFun with pointersTutorial #2CPSC 261Memory of a C programWhat does a C program need memory for?CodeStatic dataDynamic data (heap)Dynamic data (stack)Libraries (dynamically linked)Memory of a C programHow is this memory laid out?

A simple memory modelMemory 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?)

...Char pointersVariables with type char * fit this model nicely

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

0123456p...Char pointersUsing the pointer accesses one byte

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

230123456p...Char pointersIncrementing the pointer works

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

0123456p...Other pointersOther pointer types dont fit quite so nicely

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

0123456p...Other pointersIncrementing the pointer

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

0123456p...Pointer rulesThe value in a pointer is the smallest address of all the bytes accessed through the pointerint *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)

Other pointersUsing the pointer first try

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

000230123456p...Other pointersUsing the pointer second try

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

230000123456p...Which one is right?230000123456...000230123456...Unfortunately, both!!Endian-nessIn a little-endian computer, the least-significant byte of a multi-byte quantity is stored in the byte with the smallest addressIn a big-endian computer, the most-significant byte of a multi-byte quantity is stored in the byte with the smallest addressOther pointersUsing the pointer big-endian

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

000230123456p...Other pointersUsing the pointer little-endian

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

230000123456p...X86 (and X86_64) is little-endianThe standard order for data communicated between computers is big-endian (also called network byte order)How to swap bytes?Go from one endianness to the other?234519660123661945230123Reading dataFor character data:int *p = ...; // Some valid initializationfscanf(f, %d, p);

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

How many bytes get read?fscanf(f, %d, p);1 or more byte depending on the input5232123123123

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

Writing dataFor character data:int *p = ...; // Some valid initializationfprintf(f, %d, *p); // Note the *

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

How many bytes get written?fprintf(f, %d, *p); 1 or more bytes depending on the value of *p5232123123123

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