|
-
May 26th, 2008, 09:09 AM
#1
Avoiding Dynamic Allocation
How can I write this same code without the use of dynamic allocation?
Code:
class AClass {
BClass *ptr;
public:
AClass() : ptr(new BClass (10)) {}
};
Thanx,
John
-
May 26th, 2008, 09:26 AM
#2
Re: Avoiding Dynamic Allocation
If there are no circular dependencies you can simply write
Code:
class AClass
{
BClass BC[10];
};
- Guido
-
May 26th, 2008, 10:32 AM
#3
Re: Avoiding Dynamic Allocation
 Originally Posted by GNiewerth
If there are no circular dependencies you can simply write
Code:
class AClass
{
BClass BC[10];
};
I think you've misread the OP's post, since 10 is the value to initialise the BClass with, not that 10 of them are needed.
You could do the following:
Code:
class AClass {
BClass bClass;
public:
AClass() : bClass(10) {}
};
-
May 26th, 2008, 11:14 AM
#4
Re: Avoiding Dynamic Allocation
Thanks for the response.
I know that code written this way is prone to memory leak. How can I remove memory leaks from my programs and also avoid repeated deletion?
Thanx,
John
-
May 26th, 2008, 11:21 AM
#5
Re: Avoiding Dynamic Allocation
I know that code written this way is prone to memory leak. How can I remove memory leaks from my programs and also avoid repeated deletion?
If you took the suggestion to store an object instead of a pointer as the member then you do not have to worry about a memory leak.
-
May 26th, 2008, 11:30 AM
#6
Re: Avoiding Dynamic Allocation
In the original code:
Code:
class AClass
{
BClass *ptr;
public:
AClass() : ptr(new BClass (10)) {}
};
If there is requirement that you must have pointer to a class-type, you need not to initialize in initializer's list (like above). You can safely do:
Code:
AClass()
{
ptr = new BClass (10);
}
Which avoids code clutter. Initializer list is mandatory for ONLY following cases:
1. Base class is having non-default constructor.
2. You class is having data member of class-type, which does not have default constructor.
3. For const-data members (basic or class type)
4. For refernce type (X &) - not pointer types.
-
May 26th, 2008, 11:40 AM
#7
Re: Avoiding Dynamic Allocation
Initializer lists should be preferred for most things. However, I'll agree that it's probably a good idea to keep new's out of them.
To avoid a memory leak if you *do* use new in a constructor, use delete in the corresponding destructor.
-
May 26th, 2008, 11:50 AM
#8
Re: Avoiding Dynamic Allocation
If there is requirement that you must have pointer to a class-type, you need not to initialize in initializer's list (like above). You can safely do:
Yes, I think the important point is that it is safer not to use new in the initialisation list: GotW #66: Constructor Failures. In particular:
Moral #3: Always perform unmanaged resource acquisition in the constructor body, never in initializer lists. In other words, either use "resource acquisition is initialization" (thereby avoiding unmanaged resources entirely) or else perform the resource acquisition in the constructor body.
Moral #4: Always clean up unmanaged resource acquisition in local try-block handlers within the constructor or destructor body, never in constructor or destructor function-try-block handlers.
-
May 26th, 2008, 02:16 PM
#9
Re: Avoiding Dynamic Allocation
Thanks for the replies.
So basically memory leak should be handled by the destructor, using delete etc. What about repeated deletion?
Thanx,
John
-
May 26th, 2008, 02:26 PM
#10
Re: Avoiding Dynamic Allocation
What about repeated deletion?
Set pointers to NULL after using delete on them (unless the pointer goes out of scope immediately after that). Of course, this will not help if you use delete on another pointer to the same object.
-
May 26th, 2008, 02:45 PM
#11
Re: Avoiding Dynamic Allocation
 Originally Posted by JohnSmith70
Thanks for the replies.
So basically memory leak should be handled by the destructor, using delete etc. What about repeated deletion?
Thanx,
John
repeated delete can be avoided by checking the pointer before attempting to delete it.
(same as what laserlight said, with example)
i.e.
Code:
if(ptr)
{
delete ptr;
ptr = 0;
}
1. Check the pointer - only attempt to delete if !NULL
2. delete pointer
3. set pointer to NULL
John 3:16
For God so loved the world ...
-
May 26th, 2008, 02:48 PM
#12
Re: Avoiding Dynamic Allocation
1. Check the pointer - only attempt to delete if !NULL
Not necessary. Deleting a null pointer is perfectly okay.
-
May 26th, 2008, 02:57 PM
#13
Re: Avoiding Dynamic Allocation
 Originally Posted by laserlight
Not necessary. Deleting a null pointer is perfectly okay.
I did not know that.
Thanks.
John 3:16
For God so loved the world ...
-
May 26th, 2008, 02:59 PM
#14
Re: Avoiding Dynamic Allocation
More importantly in this case, you'll have to define the operator= and copy constructor in such a way that no two objects of a class share the same pointer. Typically this means allocate a new dynamic object in both cases; and in the case of operator=, also first delete the existing object (if any).
If your operator= and copy constructor are properly defined, then it will be impossible for a destructor call to result in multiple deletion because there will be only one object holding each dynamically allocated memory block.
-
May 26th, 2008, 03:00 PM
#15
Re: Avoiding Dynamic Allocation
Here's the flavor of double deletion that a beginner is most likely to encounter:
Code:
class Foo
{
public:
Foo()
{
m_ptr = new int;
}
~Foo()
{
delete m_ptr;
}
private:
int * m_ptr;
};
int main()
{
Foo a;
Foo b(a);
}
Since no copy constructor is provided, both a and b will point to the same memory, and both will try to delete that memory when they fall out of scope. This problem is avoided by writing a proper copy constructor and assignment operator.
EDIT: Lindley beat me to it.
- Alon
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|