473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

delete operator

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

Jul 23 '05 #1
2 1974
* 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?
Jul 23 '05 #2
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.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
4
by: mail.dsp | last post by:
Suppose in a class we overload four operators: operator new operator delete operator new operator delete class Test{ public: void * operator new (size_t t){ cout<<"\nCalling... new";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.