SlideServe
Menu
Préférences
© SlideServe · v2026

Compound Types: References, Pointers

asa avatar
14 diapositives · 309 Views · Oct 05, 2014

Compound Types: References, Pointers. Where are we going with this?. LHS and RHS. LHS = Left Hand Side Means replace contents Set value where this thing is stored RHS = Right Hand Side Means value Evaluate expression, function, etc. Arrive at a value to store in LHS.

Compound Types: References, Pointers - slide 1
01 / 14Compound Types: References, Pointers
1 / 17

Télécharger la présentation

Compound Types: References, Pointers
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Compound Types: References, Pointers Where are we going with this?

  2. LHS and RHS • LHS = Left Hand Side • Means replace contents • Set value where this thing is stored • RHS = Right Hand Side • Means value • Evaluate expression, function, etc. • Arrive at a value to store in LHS

  3. What is a Compound Type? • A compound type is a type defined in terms of another type • There are several in C++ • References and pointers included • Declaration = base type + list of declarators • Declarator = variable name and type related to base type

  4. References • A reference is just allows you to use another name for an object • When defined, we bind the name to the object referenced • The reference MUST be initialized • The reference CAN NOT be changed int ival = 1024; int &refVal = ival; int &refVal2; // good // error!

  5. References int ival = 1024; int &refVal = ival; // good int &refVal3 = refVal; // good refVal = 2; // now ival = 2 int j = refVal3; // now j = 2 also

  6. int ival = 1024; int &refVal = ival; int &refVal3 = refVal; Symbol Table is used by compiler to hold names types, and locations Symbol Table RAM Address 0xfa58 Identifier Type Location 0xfa54 0xfa50 ival int 0xfa20 0xfa4c 0xfa48 refVal int 0xfa20 0xfa44 0xfa40 refVal3 int 0xfa20 0xfa3c 0xfa38 As each variable is declared, the compiler adds the name (identifier) to the symbol table along with type and the memory slot (address/location/pointer) where the variable's value will be stored. References are just another name for the same slot. 0xfa34 0xfa30 0xfa2c 0xfa28 0xfa24 0x00000400 0xfa20 0xfa1c 0xfa18 0xfa14 0xfa10

  7. References • A reference is an alias for an object • Anything you do to it, you do to the object it refers to • Type of reference and object must match exactly int &refVa4 = 1; // error, not object double dVal = 1.0; int &refVal5 = dVal; // error, types

  8. Pointers • A pointer object is a compound type that holds the address (points) to another object • When defined, we do NOT have to initialize (though it is good practice) • A pointer may be reassigned • A pointer may have a pointer to it! • Pointer type must agree with object it points to • Otherwise, operations ambiguous

  9. Pointers int j=5, *ip1 = &j; // ip1 pts to j int *ip2 = ip1; // ip2 also pts to j double f, *dp2 = &f; // dp2 pts to f double g = 0.0; dp2 = &g; // dp2 now points to g dp2 = ip2; // error! Type mismatch ip2 = &f; // error! Type mismatch

  10. int j=5, *ip1 = &j; int *ip2 = ip1; double f, *dp2 = &f; double g = 0.0; dp2 = &g; Symbol Table RAM Address 0xfa58 Identifier Type Location 0xfa54 0xfa50 ival int 0xfa20 0xfa4c 0xfa48 refVal int 0xfa20 0xfa44 0xfa40 0x00000000 refVal3 int 0xfa20 0xfa3c 0x00000000 j int 0xfa24 0x0000fa3c 0x0000fa30 0xfa38 0x???????? 0xfa34 ip1 int* 0xfa28 0x???????? 0xfa30 0x0000fa24 0xfa2c ip2 int* 0xfa2c 0x0000fa24 0xfa28 0x00000005 0xfa24 f dbl 0xfa30 0x00000400 0xfa20 dp2 dbl* 0xfa38 0xfa1c 0xfa18 g dbl 0xfa3c 0xfa14 0xfa10

  11. Pointers • A pointer may be in one of 4 states: • Pointing to an object • Pointing to location just past an object • Null pointer (not pointing at any object) • Invalid (none of the above) • Dereferencing uses * operator int cnt = 0, *ip6 = &cnt; *ip6 += 5; // now cnt = 5 cout << *ip6; // prints cnt value

  12. Pointers to Pointers • A pointer is an object • Unlike a reference, it may have a pointer to it • A pointer holds sufficient space for an address, NOT the object type it points to! • Defining a pointer DOES NOT allocate space for the object type at which it points! int cnt = 0; int *ip6 = &cnt; // ip6 pts at cnt int **ipp7 = &ip6; // ipp7 pts at ip6 int *ip8; // ip8 uninit’ed

  13. Constants • const qualifier makes a variable unchangable • Makes it easy to know what literals mean • Makes it impossible to change the value • Value must be given at initialization const int MAXNUM = 100; MAXNUM = 200; // illegal! const int NAME_LEN = get_size(); const int K; // error! Uninitialized!

  14. Constants • const especially useful as qualifier for parameters • Lets reader know that value of parameter will not be changed in function • Makes it impossible to change the value in the function! int f(const int x) { ... }