Connecting Tech Pros Worldwide Forums | Help | Site Map

delete operator

Radde
Guest
 
Posts: n/a
#1: Jul 23 '05
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??


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 23 '05

re: delete operator


* Radde:[color=blue]
> #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;
> }[/color]

When posting code: please indent the code systematically.

[color=blue]
> In the above code, when i delete a( a object allocated on stack). What
> error compiler supposed to give.[/color]

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.

[color=blue]
> First of all, is this allowed in C++.[/color]

No.

[color=blue]
> If not, does all compiler gives error, if so what error??[/color]

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?
Ken Human
Guest
 
Posts: n/a
#3: Jul 23 '05

re: delete operator


Radde wrote:[color=blue]
> Hi all,[/color]
[...][color=blue]
> 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??
>[/color]

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.
Closed Thread