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? - //func2.h
-
-
class b
-
{
-
private:
-
float * data2;
-
b(float *);
-
~b();
-
test2();
-
};
-
-
//func2.cpp
-
void b::b(float *_data2)
-
{
-
data2=_data2;
-
}
-
-
void b::~b()
-
{
-
if(data2!=NULL) { delete[] data2; data2=NULL; }
-
}
-
-
//func1.h
-
-
class a
-
{
-
private:
-
float *data1;
-
a();
-
~a();
-
test();
-
};
-
-
//func1.cpp
-
void a::a()
-
{
-
data1=NULL;
-
}
-
-
void a::~a()
-
{
-
if(data1!=NULL) { delete[] data1; data1=NULL; }
-
}
-
-
void a::test()
-
{
-
b *obj=NULL;
-
obj=new b(data1);
-
delete b;
-
b=NULL;
-
}
Thanks
9 1607
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. -
void b::b(float *_data2)
-
{
-
data2 = new float(*_data2);
-
}
-
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);
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: -
class B () {
-
float* _data;
-
public:
-
B(float* data) : _data( new double(*data)) { }
-
};
-
If yes can I intialise in this way :
data2=NULL;
data2=new float(*_data2);
yes, you can; it's the same that 'weaknessforcats' wrote.
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.
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: -
b::b(float *data2)
-
{
-
this->data2 = new float(*data2);
-
}
-
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?
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?
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.
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 .
Can't you just copy the values of your pointers by dereferencing them ?
Like this : -
Class A {
-
public:
-
A();
-
float* var_A_;
-
-
}
-
-
Class B {
-
public:
-
B();
-
copy(A obj);
-
-
private:
-
floar* var_B_;
-
}
-
-
B::copy(A* obj) {
-
Float* var1 = A->var_A_;
-
Float* var2 = *var1;
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:
...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |