new and delete - overview

Nasza ocena:

3
Wyświetleń: 707
Komentarze: 0
Notatek.pl

Pobierz ten dokument za darmo

Podgląd dokumentu
new and delete  - overview - strona 1 new and delete  - overview - strona 2

Fragment notatki:

new and delete • new and delete are operators that create and destroy an object • Syntax for a single object: = new ; delete ; • Example for a single object: int *ptr; /* int or any non-function type */ ... if (!(ptr = new int)) { /* allocate memory */ fprintf(stderr, "Out of memory!"); exit (1); } /* Use *ptr */ *ptr = 17; /* deallocate sizeof(int) bytes */ delete ptr; • Syntax for an array of objects: = new [ ]; delete [] ; • Example for an array: int *ptr; /* int or any non-function type */ ... if (!(ptr = new int[10])) { /* allocate memory */ fprintf(stderr, "Out of memory!"); exit (1); } /* Use *ptr */ ptr[5] = 17; /* or *(ptr+5)=17 */ /* deallocate 10*sizeof(int) bytes */ delete [] ptr; ... zobacz całą notatkę



Komentarze użytkowników (0)

Zaloguj się, aby dodać komentarz