473,394 Members | 2,002 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,394 software developers and data experts.

delete operation

105 100+
I have created a pointer to a data in a class A and passing this pointer to a function in another class B.when I try to delete this pointer in class A I get run time exception.
But if I dont delete the object of class B I dont get any error it runs fine.But I have to delete the object of class B.I have illustrated theexample..Could anyone help with it?

Expand|Select|Wrap|Line Numbers
  1. //func2.h
  2.  
  3. class b
  4. {
  5.   private:
  6.     float * data2;
  7.     b(float *);
  8.     ~b();
  9.     test2();
  10. };
  11.  
  12. //func2.cpp
  13. void b::b(float *_data2)
  14. {
  15.   data2=_data2;
  16. }
  17.  
  18. void b::~b()
  19. {
  20.   if(data2!=NULL) { delete[] data2; data2=NULL; }
  21. }
  22.  
  23. //func1.h
  24.  
  25. class a
  26. {
  27.   private:
  28.     float *data1;
  29.     a();
  30.     ~a();
  31.     test();
  32. };
  33.  
  34. //func1.cpp
  35. void a::a()
  36. {
  37.   data1=NULL;
  38. }
  39.  
  40. void a::~a()
  41. {
  42.   if(data1!=NULL) { delete[] data1; data1=NULL; }
  43. }
  44.  
  45. void a::test()
  46. {
  47.   b *obj=NULL;
  48.   obj=new b(data1);
  49.   delete b;
  50.   b=NULL;
  51. }

Thanks
May 5 '08 #1
9 1607
weaknessforcats
9,208 Expert Mod 8TB
It looks like your constructor and destructor are private. They should be public.

Next the constructor takes a float* argument and you jusy copy the argument to the class data member. Then you delete the class data member in the destructor.

If the float* argument points to a float on the stack, you will crash at run time trying to delete a variable you didn't allocate.

You need to rewrite your constructor to allocate a new float on the heap and put that address in your class data member. Now you can safely delete in the destructor.

Expand|Select|Wrap|Line Numbers
  1. void b::b(float *_data2)
  2. {
  3. data2 = new float(*_data2);
  4. }
  5.  
May 5 '08 #2
mickey22
105 100+
Thank you ..using new operator solved my problem.

One question: is it not required to initialise data2 in class B to NULL before using new operator?

If yes can I intialise in this way :
data2=NULL;
data2=new float(*_data2);
May 5 '08 #3
mickey0
142 100+
I see many strange things (for me):
1. the underscore _, normally is used in the data member of a class and not as member parameters (the opposite you're doing);
2. in the destructor check if _data != NULL, is unuseful;
3. I never heard that destructor/constructor have to return a type (void). Maybe some compiler accepts it but not mine
4. It's a good practice write class name with "uppercase" ( "class B" and not "class b")
One question: is it not required to initialise data2 in class B to NULL before using new operator?
correct; it isn't required;
but I initialize it like this:
Expand|Select|Wrap|Line Numbers
  1. class B () {
  2.   float* _data;
  3. public:
  4.       B(float* data) : _data( new double(*data)) { }
  5. };
  6.  
If yes can I intialise in this way :
data2=NULL;
data2=new float(*_data2);
yes, you can; it's the same that 'weaknessforcats' wrote.
May 5 '08 #4
mickey22
105 100+
Hi all,

When I initialise in this way,

b::b(float *_data2)
{
data2 = new float(*_data2);
}

copying of the data from class A is not done properly.data2 contains some weird values.

So I tried to do as:

b::b(float *_data2,int _x,int _y)
{
x=_x;
y=_y;
data2=new float[x*y];
data2=_data2;
}

I get a runtime exception when I try to run this but the copying of data is done properly.
I am trying to copy data say float *data1 of one class A to a member variable float *data2 in another class B.
Could anyone help me with this please?
Thanks.
May 12 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
b::b(float *_data2)
{
data2 = new float(*_data2);
}
You don't show your main(). However, this is correct assuming _data2 points to a valid float.

BTW: The underscore does not mean a member variable. Member variables are identifed by using the this pointer:

Expand|Select|Wrap|Line Numbers
  1. b::b(float *data2)
  2. {
  3.     this->data2 = new float(*data2);
  4. }
  5.  
b::b(float *_data2,int _x,int _y)
{
x=_x;
y=_y;
data2=new float[x*y];
data2=_data2;
}
data2 is a pointer. When you assign _data2 (also a pointer) to data2 all that happens is a) the address in _data2 is copied to data2 and b) the address returned by new has been overwritten resulting in a leak.

Further, why allocate an array of float to hold the value of a single float?

This is a completely erronous approach.

How about posting code where the error occurs?
May 12 '08 #6
mickey22
105 100+
yes I have observed there is memory leak when I copy the adresses of the pointers and delete them in class A and class B.Thats where I get an error when I try to peform delete in class A.
delete[] data2;

How can I copy the values pointed by data2 to data1 point to the same values having data1 and data2 as different adresses so that I can delete them in both the classes?Can you suggest me the way?
May 12 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
How can I copy the values pointed by data2 to data1 point to the same values having data1 and data2 as different adresses so that I can delete them in both the classes?Can you suggest me the way?
If I read this correctly, you are wanting data2 and data1 to point to the same value? And then, when you delete in the destructors there are no problems.

If this is what you want, then you can't use pointers in your classes. Instead, you use a reference counted handle object. There is one here complete with code that you can use: http://bytes.com/forum/thread651599.html.
May 12 '08 #8
mickey22
105 100+
Actually I have solved using the copy function memcpy and just copied the data pointed by data1 to pointer data2.Now I dont have any problem in the delete operations .

thanks a lot .
May 12 '08 #9
Airslash
221 100+
Can't you just copy the values of your pointers by dereferencing them ?

Like this :

Expand|Select|Wrap|Line Numbers
  1. Class A {
  2. public:
  3. A();
  4. float* var_A_;
  5.  
  6. }
  7.  
  8. Class B {
  9. public:
  10. B();
  11. copy(A obj);
  12.  
  13. private:
  14. floar* var_B_; 
  15. }
  16.  
  17. B::copy(A* obj) {
  18. Float* var1  = A->var_A_;
  19. Float* var2 = *var1;
  20. }
  21.  
Just a quick thouht about the situation, you assign the value of the pointer in A (not the Adress) to the new variable.

Another trick would be to use the copyconstructor of your float type.
Float* var2 = new Float(var1);

anyways just my thought
May 13 '08 #10

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

Similar topics

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...
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
2
by: Bob Tinsman | last post by:
This problem shows up in Firefox 1.5.0.1 and Rhino 1.6R2. I've found that if I have an XML node expression that ends in a filter, I can't use it with the delete operator. In the following...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
5
by: Massimo | last post by:
The iussue: Sql 2K I have to keep in the database the data from the last 3 months. Every day I have to load 2 millions records in the database. So every day I have to export (in an other...
6
by: satish mullapudi | last post by:
Hi All, I am getting strange situation. These r the steps I have followed: 1. Created an EMPLOYEE table with around 14 fields & 688038 records. (so a large table indeed). 2. Tried to delete all...
2
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating...
11
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved,...
10
beacon
by: beacon | last post by:
Hi everybody, This is probably going to sound unorthodox, but I have to log records that are deleted...I know, go figure. Anyway, I have a form with a (continuous) subform, and on the subform I...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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...
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...

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.