Docsity

Windows Programming Lecture 2: Pointers and Memory Model, Slides of Windows Programming

Partial preview of the text

Download Windows Programming Lecture 2: Pointers and Memory Model and more Slides Windows Programming in PDF only on Docsity!

Windows Programming

Lecture 02

Pointers

Address Bus

  • Older computers had 16-bit address bus
  • Today most computers have 32-bit address bus

Memory model

  • All pointers are 32-bit pointers in Win programming.
  • There is no concept of near or far pointers.
  • There is only one memory model i.e.

flat memory model in 32-bit environment

Pointer Declaration

*int i = 20; int ptr = &i;

Address of i (&i)

20

variable

Constant or literal ptr i pointer (^) points to

Pointers

  • Address of a variable is address of its lowest byte

pointer to int

207 208 209 210 211 212 213

Operators used with pointers

( * ) Indirection / De_referencing

( & ) Address / Reference

Pointer Arithmetic

  • Two pointers can be added / subtracted
  • An integer can be added to or subtracted from a pointer.
  • Only addition(+) and subtraction(-) are possible in pointer arithmetic.

Pointer Arithmetic

*double d, ptr_d;

*char c, ptr_c;

ptr_d = &d;

ptr_c = &c;

ptr_d = ptr_d + 1; // Skips 8 bytes in memory

ptr_c = ptr_c +1; // Skips 1 byte in memory

Pointer Arithmetics

int m, *ptr; ptr=&m;

270

274

270

270

271

271 272 273 274

272 273 274

ptr=pt + 1;

ptr

ptr

In Win32 programming int and long are of

same size (4 bytes)

  • Total number of possible addresses in a 32-bit pointer is 2 32.
  • Lowest address: 0
  • Highest address: 2 32 -

=4,29,49,67, =0xffffffff

Questions

Which bytes are modified? *long double i, ptr; ptr = &i;

***((char ) ((long ) ptr+1)+2) = 91;

***((short int )((long )ptr+2)-1)=91;