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

delete operator in c++

105 100+
Hi all,

I have a class say A and created a pointer to call this class like

A *obj=NULL;
obj=new A( );

After I perform all the operations within the code and try to delete this pointer using

delete obj;
obj=NULL;

I get an exception as
" the instruction at "0x7c910f 2b" REFERENCED MEMORY AT "0x00000004".the memory could not be "read".Click OK to terminate the program.

I am not able to figure it out.

If i dont delete it the code runs smoothly.But always have to delete after use once allocated.The whole code is within a function block.

Could anyone please suggest me.

Thanks .
Nov 6 '07 #1
9 1658
Ganon11
3,652 Expert 2GB
Try removing the obj=NULL; portion and see if that works?
Nov 6 '07 #2
sicarie
4,677 Expert Mod 4TB
delete obj;
obj=NULL;
So you're deleting obj and then trying to assign it a NULL value?
Nov 6 '07 #3
Post the code of the destructor.
Nov 6 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Have you stepped through your destructor with your debugger??
Nov 6 '07 #5
mickey22
105 100+
I removed NULL. It did not work.

I have my code in this way:

my own class

Expand|Select|Wrap|Line Numbers
  1. ///abc.h
  2.  
  3. class abc
  4. {
  5.    int off;
  6.    int *img;
  7.    abc(void);
  8.    ~abc( );
  9.    void sum( ); 
  10. };
  11.  
  12. //abc.cpp
  13.  
  14. abc::abc(void)
  15. {
  16.    off=0;
  17. }
  18. abc::~abc( )
  19. {
  20.    delete[] img;
  21. }
  22.  
  23. void abc::sum( )
  24. {
  25.    Filter *f;   //class already created by someone and I am using all its functions
  26.    f=new Filter ( );
  27.    std::string filename=f->datafile();
  28.    raw *r;   //another class same like f created by someone.
  29.    r=new raw( );
  30.    r->add(filename );
  31.    img=new float[100];
  32.    delete r;
  33.    r=NULL;
  34.    delete f;
  35.    f=NULL;
  36. }


I am getting that exception just before add function .I get the filename correctly from the class but when I pass to the add( ) that filename is corrupted and I see it as Bad Ptr when I stepped in thru the debugger step by step. when I delete r, I dont get any exception.

It is little bit confusing.Please let me now if any questions.I am still trying to figure it out.
Nov 6 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
Filter *f; //class already created by someone and I am using all its functions
f=new Filter ( );
This should be:
Expand|Select|Wrap|Line Numbers
  1. Filter *f; //class already created by someone and I am using all its functions
  2. f=new Filter;
  3.  
Also, your abc class:
class abc
{
int off;
int *img;
abc(void);
~abc( );
void sum( );
};
Has no constructor. Therefore no guarantee that the img pointer ever got initialized. If you delete and uninitialized pointer, you crash.
Nov 6 '07 #7
The best way to find it is to start by adding error checking. For example:
Expand|Select|Wrap|Line Numbers
  1. abc::abc()
  2. {
  3.    off=0;
  4.    img = NULL;
  5. }
  6. ...
  7. abc::~abc()
  8. {
  9.    if (img) {
  10.       delete(img);
  11.       }
  12.    img = NULL;
  13. }
  14.  
Adding the code will benefit you in the long run because it will catch the problem every time - even if it shouldn't happen any more.

I'm pretty sure that
Expand|Select|Wrap|Line Numbers
  1. std::string filename=f->datafile();
  2.  
copies the return value of datafile() then it gets copied again on the way in to add(). If you meant to get a pointer or a reference then make sure that's what's happening. For example, a pointer allocated on the stack inside datafile() won't be valid after that function returns. I'm always fuzzy on these things in C++ though.
Nov 6 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
The best way to find it is to start by adding error checking. For example:

Code: ( cpp )
abc::abc()
{
off=0;
img = NULL;
}
...
abc::~abc()
{
if (img) { <<<<<<<<<<<<<<<<<<<<<<<<<<
delete(img);
}
img = NULL;
}
Pointless test. delete does nothing if the pointer is null. However, setting the pointer to null after the delete is a good idea. This is different from C. There, free() crashes on a null pointer.

For example, a pointer allocated on the stack inside datafile() won't be valid after that function returns. I'm always fuzzy on these things in C++ though.
It's not the pointer that's the problem. It's the address inside the pointer. If that address is to another local variable, then you have a problem. But if the address is to something on the heap, then that address will be valid until you delete it.
Nov 7 '07 #9
RRick
463 Expert 256MB
Rob Russell had the right idea by setting img = NULL in the constructor. This is what was missing in the original constructor. When the object was created, img had a non-zero value which caused delete to crash and burn.

A minor point, in C++, NULL has fallen out of favor. Using img = 0 is preferable.
Nov 7 '07 #10

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

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.