-
23
pages
-
English
-
Documents
Description
CoursdeC++Gestion de la mémoire et structurebas niveauCécile Braunsteincecile.braunstein@lip6.frCours de C++ 1 / 16Low leveldatastructuresUntil now we used :• Variables• Reference• ContainersHow it works?Use low level techniques and tools.• Underlie the standard library• Close to the way hardware worksCours de C++ 2 / 16PointersDefinitionA pointer is a value that represents the address of an object.Every distinct object has a unique address. It’s the the part of thecomputer’s memory that contains the object.int main(){"a" 30x400int a = 3;"pa" 0x400int pa ;* 0x404int pb ;* "pb" 0x10000x408pa = &a;pb = (int )malloc(sizeof(*int));pb = 12*return 0;120x1000} Cours de C++ 3 / 16Operatorsonpointers&x : address operatorpx : dereference operator*T p : declaration of a pointer toT ( p has a typeT)* *NULL : constant value, differs from every pointer to any objectCours de C++ 4 / 16OperationsonpointersExerciceWhat is the output of this program. We assume that&x = 0xbf84e7b8#includeusing namespace std;int main(){int x = 5;int p = &x;*cout << "x = " << x << endl; << "p = " << p << " ; p = " << p << endl;* *p=6;*p = p + 1;cout <<"x = " << x << endl;cout << "p = " << p << " ; p = " << p << endl;* *return 0;} Cours de C++ 5 / 16Referencesvs.Pointers#includeusing namespace std;void increment(int& v) Reference’s properties{ v++;}• A reference is a pointerint main(){self dereferencedint a = 3 ; int ...
-
Publié par
-
Langue
English