Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > delete operator

Reply
Thread Tools

delete operator

 
 
Radde
Guest
Posts: n/a
 
      05-29-2005
Hi all,
#include<iostream>
using namespace std;


class A{
private:
int* iArray;
int size;
public:
A()
{
cout<<"No argument constructor"<<endl;
}

A(int s) : size(s)
{
cout<<"One argument constructor"<<endl;
iArray = new int[size];
}

~A()
{
cout<<"Destructor"<<endl;
delete[] iArray;
}
};


int main()
{
A a;

delete a; // Error
return 0;
}

In the above code, when i delete a( a object allocated on stack). What
error compiler supposed to give. First of all, is this allowed in C++.
If not, does all compiler gives error, if so what error??

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-29-2005
* Radde:
> #include<iostream>
> using namespace std;
>
>
> class A{
> private:
> int* iArray;
> int size;
> public:
> A()
> {
> cout<<"No argument constructor"<<endl;
> }
>
> A(int s) : size(s)
> {
> cout<<"One argument constructor"<<endl;
> iArray = new int[size];
> }
>
> ~A()
> {
> cout<<"Destructor"<<endl;
> delete[] iArray;
> }
> };
>
>
> int main()
> {
> A a;
>
> delete a; // Error
> return 0;
> }


When posting code: please indent the code systematically.


> In the above code, when i delete a( a object allocated on stack). What
> error compiler supposed to give.


The compiler is required to issue an error diagnostic, because you cannot
apply 'delete' to a non-pointer.

And 'a' is not a pointer.

The specific error message text (if any, we can e.g. hypothesize a compiler
employing beep codes, although that would probably not endear the compiler
to rock-music-loving hippie programmers) depends on the compiler.


> First of all, is this allowed in C++.


No.


> If not, does all compiler gives error, if so what error??


See above.

Btw., if you change your main program to

int main()
{
A a;
}

you will have a program that the compiler has to accept, but which
will exhibit the dreaded Undefined Behavior, UB, because the A default
constructor does not initialize the pointer that the destructor gives
to 'delete[]'.

The standard places no requirement on such a program, but a quality
compiler may ensure that you (probably) will get some run-time error.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Ken Human
Guest
Posts: n/a
 
      05-29-2005
Radde wrote:
> Hi all,

[...]
> int main()
> {
> A a;
>
> delete a; // Error
> return 0;
> }
>
> In the above code, when i delete a( a object allocated on stack). What
> error compiler supposed to give. First of all, is this allowed in C++.
> If not, does all compiler gives error, if so what error??
>


ken@ken-wn0vf73qmks ~/c
$ cat classExample.cpp
#include <iostream>

class A {
public:
A();
A(int s);
~A();
private:
int * iArray;
int size;
};

A::A() {
std::cout << "No argument constructor." << std::endl;
iArray = new int[1];
}

A::A(int s)
:
size(s)
{
std::cout << "One argument constructor, s: " << s << '.'
<< std::endl;
iArray = new int[s];
}

A::~A() {
std::cout << "Destructor." << std::endl;
delete [] iArray;
}

int main(void) {
A a1; /*This is an object of type A*/
A *a2 = new A; /*This is a pointer to an object of type A*/
/*delete a1;*/ /*Here you're trying to delete an object, it
doesn't work. Let the destructor do its
job when a1 goes out of scope*/
delete a2; /*Here you're trying to delete what a2 points
to. This calls a2's destructor and then frees
the memory pointed to by a2.*/
return 0;
}

ken@ken-wn0vf73qmks ~/c
$ g++ -o classExample classExample.cpp

ken@ken-wn0vf73qmks ~/c
$ ./classExample.exe
No argument constructor.
No argument constructor.
Destructor.
Destructor.

The error you'd get if `delete a1;' wasn't commented out would be
something about delete expecting a pointer rather than an `A' or `class
A.' It's not allowed in C++ and every compiler should give a similar error.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
delete MyClass doing more than MyClass::operator delete()? tom C++ 5 07-14-2006 06:21 PM
How to redefine operator delete/delete[] via macro? Amy C++ 13 02-23-2005 03:36 AM
Mixing new/delete and operator new/delete? Jef Driesen C++ 1 01-19-2005 01:56 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57