You are not logged in. Please login at www.codechef.com to post your questions!

×

Help to DELETE the "DELETE" error

1
1

Please look into this code.. http://www.codechef.com/viewsolution/3544714

It got accepted on commenting the three delete functions at the end, but gives a SIGABRT error on submitting as it is..

Plz Help.

asked 10 Mar '14, 07:58

milindshah077's gravatar image

1★milindshah077
15127
accept rate: 0%


IM NOT SURE, but imho pointer is not deleted the object it is pointing to is deleted.

int *p;
delete []p; // Produces Segmentation Fault
delete p;   // Produces Segmentation Fault

(1) ordinary delete Deallocates the memory block pointed by ptr (if not null), releasing the storage space previously allocated to it by a call to operator new[] and rendering that pointer location invalid.

(2) nothrow delete Same as above (1).

(3) placement delete Does nothing.

The second and third versions cannot be implicitly called by a delete-array expression (the delete[] operator always calls the first version of this function exactly once for each of its arguments). But these versions are called automatically by a new-expression if any of the object constructions fail (e.g. if the constructor of an object throws while being constructed by a array new-expression with nothrow, the matching operator delete[] function accepting a nothrow argument is called).

int* a = new int[10];
delete[] a;
a = NULL; // a still exists, but it's a dangling pointer now, so we set it to NULL (or 0)

operator delete[] can be called explicitly as a regular function, but in C++, delete[] is an operator with a very specific behavior: An expression with the delete[] operator, first calls the appropriate destructors for each element in the array (if these are of a class type), and then calls function operator delete[] (i.e., this function) to release the storage.

ref: http://www.cplusplus.com/

link

answered 10 Mar '14, 09:59

garakchy's gravatar image

1★garakchy
1.1k163048
accept rate: 1%

edited 10 Mar '14, 10:22

thanks but i guess the code takes care of that.. it does not delete a null pointer.. then why is this happening?

(10 Mar '14, 18:40) milindshah0771★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×66
×19

question asked: 10 Mar '14, 07:58

question was seen: 4,134 times

last updated: 10 Mar '14, 18:40