473,664 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete operator

Hi all,
#include<iostre am>
using namespace std;
class A{
private:
int* iArray;
int size;
public:
A()
{
cout<<"No argument constructor"<<e ndl;
}

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

~A()
{
cout<<"Destruct or"<<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 1995
* Radde:
#include<iostre am>
using namespace std;
class A{
private:
int* iArray;
int size;
public:
A()
{
cout<<"No argument constructor"<<e ndl;
}

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

~A()
{
cout<<"Destruct or"<<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.cp p
#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.cp p

ken@ken-wn0vf73qmks ~/c
$ ./classExample.ex e
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
2587
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 during construction, a form of
1
3844
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 are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
5
5235
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 discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
20
4139
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
912
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 nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
2
2081
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 operator, placement, nothrow, arrays, etc... My books cover the topic, I've found FAQs on the web that cover the topic, and so on, but all the sources I've found are disjointed. There's a bit on this page, a bit on that page, and so on. The...
3
4643
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 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
5
5516
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) { cout << "A::operator delete" << endl;
10
2085
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 sizeof(foo)-sized buffer but does NOT call foo::foo().
4
1832
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
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.