delete (C++)

delete (C++)

In the C++ programming language, the delete operator calls the destructor of the given argument, and returns memory allocated by new back to the heap[1]. A call to delete must be made for every call to new to avoid a memory leak. After calling delete the memory object pointed to is invalid and should no longer be used. Many programmers assign 0 (null pointer) to pointers after using delete to help minimize programming errors. Note, however, that deleting a null pointer has no effect (if the deallocation function is one supplied in the standard library[2]), so it is not necessary to check for a null pointer before calling delete.

Example code snippet:

int *p_var = 0;     // new pointer declared
p_var = new int;       // memory dynamically allocated
/* .......
other code
delete p_var;          // memory freed up
p_var = 0;          // pointer changed to 0 (null pointer)

Arrays allocated with new [] can be similarly deallocated with delete []:

int size = 10;
int *p_var = 0;     // new pointer declared
p_var = new int [size];// memory dynamically allocated
/* .......
other code
delete [] p_var;       // memory freed up
p_var = 0;          // pointer changed to 0

Arrays, allocated with new[], must be deallocated with delete[], since the layout of arrays, allocated with new[] is implementation defined[citation needed], and possibly not compatible with new. For example, in order to properly perform object destruction at delete[], some implementations of new[] embed the number of allocated objects into the beginning of the allocated memory chunk, and return pointer to the remaining part of the array.[citation needed].

The delete operator (user defined) is different from operator delete. The delete operator may call operator delete to free up memory.


External links

Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Delete (C++) — В языке программирования C++ оператор delete возвращает память, выделенную оператором new, обратно в кучу. Вызов delete должен происходить для каждого вызова new, дабы избежать утечки памяти. После вызова delete объект, указывающий на этот… …   Википедия

  • delete — de‧lete [dɪˈliːt] verb [transitive] 1. to remove something written on a list, in a document etc: • Libelous remarks about living people have been deleted from the document. • The company will be deleted from the S & P 500 Index. 2. COMPUTING to… …   Financial and business terms

  • DELETE — DELETE  в языках, подобных SQL, DML операция удаления записей из таблицы. Критерий отбора записей для удаления определяется выражением where. В случае, если критерий отбора не определён, выполняется удаление всех записей. В СУБД,… …   Википедия

  • Delete — Delete: Delete оператор языка программирования C++. Delete операция в языках, подобных SQL. Delete клавиша на клавиатуре компьютера …   Википедия

  • Delete — De*lete , v. t. [imp. & p. p. {Deleted}; p. pr. & vb. n. {Deleting}.] [L. deletus, p. p. of delere to destroy. Cf. 1st {Dele}.] To blot out; to erase; to expunge; to dele; to omit. [1913 Webster] I have, therefore, . . . inserted eleven stanzas… …   The Collaborative International Dictionary of English

  • delete — I verb blot out, cancel, censor, cross off, cross out, cut, cut out, dele, discard, do away with, drop, edit out, efface, elide, eliminate, eradicate, erase, excise, expel, expunge, extirpate, get rid of, leave out, modify by excisions,… …   Law dictionary

  • delete — (v.) late 15c., destroy, eradicate, from L. deletus, pp. of delere destroy, blot out, efface, from delevi, originally perf. tense of delinere to daub, erase by smudging (as of the wax on a writing table), from de from, away (see DE (Cf. de )) +… …   Etymology dictionary

  • delete — (izg. dilȋt) m DEFINICIJA inform. tipka na tipkovnici koja služi za brisanje znaka na kojem se nalazi kursor, usp. backspace ETIMOLOGIJA engl.: brisati …   Hrvatski jezični portal

  • delete — cancel, efface, obliterate, blot out, expunge, *erase Analogous words: eliminate, *exclude, rule out: omit (see NEGLECT vb) …   New Dictionary of Synonyms

  • delete — [v] erase, remove annul, black out, bleep, blot out, blue pencil*, cancel, clean, clean up, cross out, cut, cut out, decontaminate, destroy, drop, edit, efface, eliminate, exclude, expunge, obliterate, omit, pass up, rub, rub out, rule out,… …   New thesaurus

  • delete — ► VERB ▪ remove or erase (text). DERIVATIVES deletion noun. ORIGIN Latin delere to blot out …   English terms dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”