473,382 Members | 1,180 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,382 software developers and data experts.

What is the actual concept of delete operator?

11
Hello all. I am c++ beginner. I am not getting the real concept of delete operator. If it deallocates the memory, then why i am getting such output..!!


Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int *a , b=5;
  4.     a = & b;
  5.     cout << *a << endl;
  6.     delete a;
  7.     cout << *a;
  8.     getch();
  9.     return 0;   
  10. }
Output:
5
5
Dec 31 '12 #1

✓ answered by weaknessforcats

The rule is: He who allocates is he who deletes.

When you allocate using the new operator, you receive a pointer to that object. The object will exist until you delete it.

The compiler never uses the delete operator since it always deletes when the object goes out of scope.

In the case above, when the pointer to the object allocated by new goes out of scope, the pointer is deleted but not the object it points at. That's because the compiler allocated the pointer but you allocated the object. Here you must explicitly call delete to tell the compiler to deallocate the object.

Your posted example won't execute. It will crash trying to delete an object the compiler allocated but you did not.

4 1640
weaknessforcats
9,208 Expert Mod 8TB
The rule is: He who allocates is he who deletes.

When you allocate using the new operator, you receive a pointer to that object. The object will exist until you delete it.

The compiler never uses the delete operator since it always deletes when the object goes out of scope.

In the case above, when the pointer to the object allocated by new goes out of scope, the pointer is deleted but not the object it points at. That's because the compiler allocated the pointer but you allocated the object. Here you must explicitly call delete to tell the compiler to deallocate the object.

Your posted example won't execute. It will crash trying to delete an object the compiler allocated but you did not.
Dec 31 '12 #2
MrError
11
What do you mean by explicitly calling delete and no, it did not crash!
Jan 1 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
An explicit delete is when you code the word delete.

Implicit delete:

Expand|Select|Wrap|Line Numbers
  1. {
  2.     int x;
  3. }
Explicit delete:
Expand|Select|Wrap|Line Numbers
  1. {
  2.    int * ptr = new int;
  3.    delete ptr;
  4. }
As far as crashing goes, you cannot delete a local variable. If your program did not crash, you may look into a better compiler. Visual Studio.NET 2008 has no compile errors but the program will crash when attempting to delete the local variable.
Jan 1 '13 #4
MrError
11
Got it.
Thanks a lot for your help.
Jan 1 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: A | last post by:
Hi, Consider this: char* ptr = "a string"; Is ptr a pointer to a dynamically created object for which i must use the delete operator to deallocate memory? I'm sure the "a string" part...
1
by: Pelle | last post by:
Hello all, I have to admit, that the idea that occurred to me recently is weird. It was somehow inspired by the huge response on the "delete operator" thread, but goes into a somewhat different...
3
by: Megha Vishwanath | last post by:
Hi, I'd like to know how heap aggregates created with a "new" operator at non-contiguous memory locations get deallocated using the "delete " operator in VC++. A garbage collectors in Java...
2
by: Stijn Oude Brunink | last post by:
Hello, I want to use the vector class to work with arrays of classes but I seem to get in conflict with the delete operator used in the specific class. The code below gives an assertion?! How is...
7
by: Nemo | last post by:
Hello Folks, I need to manipulate a list of char strings as follows, but when I want to delete the pointer created with new at the end, delete operator crashes, any idea? char* list;...
13
by: Nemo | last post by:
Hello Folks, I need to manipulate a list of char strings as follows, but when I want to delete the pointer created with new at the end, delete operator crashes, any idea? char* list;...
9
by: bob | last post by:
Let's say you use the delete operator as follows: Rocket *rocket = new Rocket; void *voidptr = (void *) rocket; delete voidptr; Does the memory get deleted right? Do delete operations on...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
2
by: kapilkumawat | last post by:
I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.