To tylko jedna z 2 stron tej notatki. Zaloguj się aby zobaczyć ten dokument.
Zobacz
całą notatkę
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)