SlideServe
Menu
Préférences
© SlideServe · v2026

Dynamic Objects

carmennoel avatar
14 diapositives · 183 Views · Jan 04, 2020

Learn about managing memory in programming, including dynamic and static objects, memory allocation, and pointers in C++. Explore examples of new, delete operations and potential memory issues.

Dynamic Objects - slide 1
01 / 14Dynamic Objects
1 / 17

Télécharger la présentation

Dynamic Objects
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. Programming Dynamic Objects

  2. Static object Memory is acquired automatically int A[10]; Memory is returned automatically when object goes out of scope Dynamic object Memory is acquired by program with an allocation request new operation Dynamic objects can exist beyond the function in which they were allocated Object memory is returned by a deallocation request delete operation Static verses Dynamic Objects

  3. Memory for Dynamic Objects • Memory for dynamic objects • Requested from the free store (heap) • Free store is memory controlled by operating system • Learn more about memory management in CS252 • new operation specifies • The type and number of objects • If there is enough memory to satisfy the request • A pointer to sufficient memory is returned by the operation • If there is not enough memory to satisfy the request • An error is generated

  4. Default New • Syntax Ptr = new SomeType; • Where • Ptr is a pointer of type SomeType • Be careful • The newly acquired memory is uninitialized unless there is a default SomeType constructor

  5. Default New Example int *iptr = new int; Rational *rptr = new Rational;

  6. Specific New • Syntax SomeType *Ptr = new SomeType(ParameterList); • Where • Ptr is a pointer of type SomeType • Initialization • The newly acquired memory is initialized using a SomeType constructor • ParameterList provides the parameters to the constructor

  7. Specific New Example int *iptr = new int(10); Rational *rptr = new Rational(1,2);

  8. Array of New • Syntax SomeType *P; P = new SomeType[Expression]; • Where • P is a pointer of type SomeType • Expression is the number of objects to be constructed -- we are making an array • Note • The newly acquired list is initialized if there is a default SomeType constructor • P is a dynamic array.

  9. Array of New Example int *A = new int [3]; Rational *R = new Rational[2]; A[1] = 5; Rational r(2/3); R[0] = r;

  10. List Example cout << "Enter list size: "; int n; cin >> n; int *A = new int[n]; GetList(A, n); SelectionSort(A, n); DisplayList(A, n);

  11. Delete • Forms of request delete P; // used if storage came from new delete [] P; // used if storage came from new[] • Storage pointed to by P is returned to free store • P is now undefined

  12. Delete Example int n; cout << "Enter list size: "; cin >> n; int *A = new int[n]; GetList(A, n); SelectionSort(A, n); DisplayList(A, n); delete [] A;

  13. Dangling Pointer Problem int *A = new int[5]; for(int i=0; i<5; i++) A[i] = i; int *B = A; delete [] A;

  14. Memory Leak Problem int *A = new int [5]; for(int i=0; i<5; i++) A[i] = i; A = new int [5];