Data structures and algorithms lab5

1,413 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,413
On SlideShare
0
From Embeds
0
Number of Embeds
1,080
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Data structures and algorithms lab5

  1. 1. DATA STRUCTURES AND ALGORITHMS LAB 5 Bianca Tesila FILS, March 2014
  2. 2. OBJECTIVES  Pointers
  3. 3. POINTERS: INTRODUCTION  What is a pointer?
  4. 4. POINTERS: SHORT REMINDER FROM THE LECTURES  Memory space of a program=a sequence of bytes  The bytes are numbered starting from 0: 0, 1, 2 …  Every variable occupies a certain number of consecutive bytes in memory  An int occupies 4 bytes  A double occupies 8 bytes  A char occupies 1 byte  The address of a variable = the index of the first byte occupied in memory  If an int variable occupies the bytes numbered 1000, 1001, 1002, 1003, the address of the variable is 1000
  5. 5. POINTERS: SHORT REMINDER FROM THE LECTURES  Every variable stores its value in the bytes it occupies in the program’s memory  A char stores its value in 1 byte  An int stores its value as 4 consecutive bytes  Pointer = special type of variable  Its size = architecture-dependent (32 bits = 4 bytes in our case)  Stores an address (the index of a byte in memory)  The pointer points to the address it stores  Multiple types of pointers  int* p (pointer to int)  char* p (pointer to char)  int** p (pointer to pointer to int)  double*** p  ...  Each type of pointer has 4 bytes and stores an address  Differences:  int x;  int *p, char* q;  p = q = &x;  *p // refers to the int which starts at the address &x  *q // refers to the char located at the address &x (the first byte of the 4- byte int variable x)
  6. 6. POINTERS: SHORT REMINDER FROM THE LECTURES !!Example: int x; int *p; char* q; p = q = &x; *p // refers to the int which starts at the address &x *q // refers to the char located at the address &x (the first byte of the 4-byte int variable x)
  7. 7. POINTERS: OPERATORS & (reference operator) The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". myvar = 25; foo = &myvar; bar = myvar; The variable that stores the address of another variable (like foo in the previous example) is what in C++ is called a pointer.
  8. 8. POINTERS: OPERATORS * (deference operator) An interesting property of pointers is that they can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*). The operator itself can be read as "value pointed to by". baz = *foo; This could be read as: "baz equal to value pointed to by foo", and the statement would actually assign the value 25 to baz, since foo is 1776, and the value pointed to by 1776 (following the example above) would be 25.
  9. 9. POINTERS  C++ has both pointers and references, whereas Java has only references. C++ pointers are manipulated more like Java references. You cannot manipulate (using assignment statements) C++ references at all.  The main difference between C++ pointers and references (in Java or C++) is that the compiler manages references (knows you want the contents, not the address) whereas you (the programmer) must de-reference pointers to get at data. In other words, the compiler automatically does the de-referencing when it's a reference.  To allocate memory, as in Java, use the keyword new . To deallocate memory, use delete. There is no garbage collection so all memory allocated by programmer must be deallocated by the programmer. The compiler manages memory that it allocates, e.g., int n; The compiler allocates memory for n and deallocates its memory when it goes out of scope.
  10. 10. POINTERS  For the beginning, let’s analyze PointersExample.cpp !!Exercise: Implement a function that reverses the elements of an array. Hint: void invert(int *array, int arraySize) An array variable contains the starting address of the array (the array element of the first address) and a pointer that is equal to the type of array elements.

×