delete operator

R

Radde

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??
 
A

Alf P. Steinbach

* 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.
 
K

Ken Human

Radde said:
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;
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top