Lec03-pointers.pdf - AST21105 Object-Oriented Programming and Design Topic 3 Pointers Lecture notes are based on textbook and various resources over the

Lec03-pointers.pdf - AST21105 Object-Oriented Programming...

This preview shows page 1 - 9 out of 76 pages.

AST21105 Object-Oriented Programming and Design Topic 3 Pointers Lecture notes are based on textbook and various resources over the Internet
Image of page 1
POINTER BASICS 2
Image of page 2
Variables and Memory o Variables present in the source code are allocated in the main memory of the computer. Behind the scenes at runtime, each variable uses an area of the computer's memory to store its value. o A variable or data object has three properties: n its data type, n its value, and n its storage location (i.e. memory address). 3 10 5 7 Memory Address 1000 1004 1008 Values in memory Memory
Image of page 3
Memory Address o In reality, n memory addresses are usually displayed as hexadecimal numbers. For example, you may see something like o 0073FEBC (8 digits; 4 bits each) for a 32-bit program, o 000000D0B48FFB14 (16 digits) for a 64-bit program, o On some platforms, you may also see a 12-digit address like 0x7ffeefbff560. This is because today's CPU and compilers use up to only 48 bits for memory addresses (a large enough memory address space, 2 48 bytes = 256 terabytes, for a normal program nowadays). n memory addresses used in your program are what we call " virtual addresses " (logical addresses); the operating system and the CPU hardware need to do an additional step to convert them to physical addresses (a real location on the RAM). 4
Image of page 4
5 What are Pointers? o A pointer or pointer variable is a variable that holds a memory address of another object (typically another variable) in memory. ± a’ Memory Address 1000 1012 1004 1008 1016 Variables in memory 1020 Memory 1000 If one variable contains the address of another variable, the first variable is said to point to the second. Pointer / Pointer variable
Image of page 5
6 Declaration of Pointer Variables o If a variable is going to hold an address of another variable , it must be declared as follows. o Actually, we can treat <type>* as a special type which is pointer type <type> * <variable name> ; <type> * <variable name> ; Syntax: where <type> is the type of the variable address that the pointer variable can store (e.g. int, char, double, user- defined type), <variable name> is the name of the pointer variable OR
Image of page 6
7 Declaration of Pointer Variables o Pointer examples: n Recall the syntax for declaring a pointer variable: <type>* <variable name>; // Declare a pointer that points to a variable of integer type int * a; // the value of a is garbage but is not NULL // Declare a pointer that points to a variable of double type double * b; // the value of b is garbage but is not NULL // Declare a pointer that points to a variable of char type char * c; // the value of c is garbage but is not NULL // It is no difference for you to put * close to type OR // close to name int * d; // the value of d is garbage but is not NULL int *d; // same as above, no difference We will talk a bit more about NULL later!
Image of page 7
8 Pointer Operator & (Address-Of) o There are two special pointer operators: & and * (Note: The * here doesn’t mean multiplication ) o The first operator, & is a unary operator (i.e. with single operand) that returns the memory address of another variable n Usage: &<variable name> o We can think of & as returning "the address of" int var1 = 5; // pint receives the address of var1 int
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 76 pages?

  • Spring '19
  • Pointer, Garbage collection

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture