-
December 13th, 2009, 04:06 AM
#1
Good practice for knowing if something has been allocated?
A common practice I employ within classes is to couple dynamically allocated arrays with a bool to keep track of whether the array is currently allocated. I'd like to know if there was a cleaner way of doing this? I can't find the existence of some kind of isalloc() function, but perhaps this could be done through exceptions (i.e. throws exception if you try and deallocate unallocated array)?
Is there a best practice way of doing this?
-
December 13th, 2009, 04:20 AM
#2
Re: Good practice for knowing if something has been allocated?
Set array pointer initially to NULL. When array is released, set pointer back to NULL. Condition ptr != NULL checks whether array is allocated.
-
December 13th, 2009, 05:06 AM
#3
Re: Good practice for knowing if something has been allocated?
Thankyou very much, that's the perfect solution. Cheers!
-
December 13th, 2009, 09:56 AM
#4
Re: Good practice for knowing if something has been allocated?
These are macros that I always define if I'm in this situation too
Code:
#define DECLARE_ARRAY(type, name);\
type * name = 0;
#define DECLARE_ARRAY_WITH_SIZE(type, name, size);\
type * name = new type[size];
#define ARRAY_SIZE(type, name, size);\
DELETE_ARRAY_NO_RESET(name);\
name = new type[size];
#define DELETE_ARRAY(name);\
if (name){ delete[] name; name = 0;}
#define DELETE_ARRAY_NO_RESET(name);\
if (name) delete[] name;
Last edited by ninja9578; December 13th, 2009 at 09:58 AM.
-
December 13th, 2009, 10:12 AM
#5
Re: Good practice for knowing if something has been allocated?
Originally Posted by Bogdanovist
A common practice I employ within classes is to couple dynamically allocated arrays with a bool to keep track of whether the array is currently allocated. I'd like to know if there was a cleaner way of doing this? I can't find the existence of some kind of isalloc() function, but perhaps this could be done through exceptions (i.e. throws exception if you try and deallocate unallocated array)?
Why not just use std::vector or some other appropriate container class, possibly of your own making if the standard containers do not suffice?
Originally Posted by ninja9578
These are macros that I always define if I'm in this situation too
You're funny
(Although when you are not joking you do leave out the check for a null pointer and write your macros correctly, right? )
-
December 13th, 2009, 01:28 PM
#6
Re: Good practice for knowing if something has been allocated?
-
December 13th, 2009, 01:34 PM
#7
Re: Good practice for knowing if something has been allocated?
Originally Posted by ninja9578
Huh?
Oops, okay, I didn't realise that the syntax was correct: was the empty statement for each function-style macro part of the joke?
EDIT:
For the other point, I am talking about the fact that one would not normally write:
Code:
if (name){ delete[] name; name = 0;}
rather, one would write:
Code:
delete[] name; name = 0;
EDIT #2:
Hate it spoil the joke if it really was one, but if it was not:
I thought the macro thing was a joke because as far as I can tell, DECLARE_ARRAY, DECLARE_ARRAY_WITH_SIZE and DELETE_ARRAY_NO_RESET do not add any value to the code, and may even obfuscate it. DELETE_ARRAY does add value to the code, but it can easily be replaced by an inline function template:
Code:
template<typename T>
inline void destroy_array(T*& p)
{
delete[] p;
p = 0;
}
I am not entirely certain of the utility of ARRAY_SIZE, but again it is easily replaced by an inline function template:
Code:
template<typename T>
inline void reassign_array(T*& p, std::size_t size)
{
delete[] p;
p = new T[size];
}
Furthermore, the point seems moot when we can use std::vector.
EDIT #3:
Ah yes, I figured out why ARRAY_SIZE was bothering me: it is not exception safe. With a function template, we could write:
Code:
template<typename T>
inline void reassign_array(T*& p, std::size_t size)
{
T* temp = new T[size];
delete[] p;
p = temp;
}
Doing something similiar with a function-style macro will likely require use of the do while loop trick to introduce scope.
Last edited by laserlight; December 13th, 2009 at 02:03 PM.
-
December 13th, 2009, 02:38 PM
#8
Re: Good practice for knowing if something has been allocated?
I always wonder why folks in this day and age still want to work with raw arrays. Is it because the platform they are working under doesn't have equivalent stl collection libraries available? Is it because they aren't comfortable working with the templated libraries?
Please let me understand.
-
December 13th, 2009, 02:41 PM
#9
Re: Good practice for knowing if something has been allocated?
Originally Posted by Alex F
Set array pointer initially to NULL. When array is released, set pointer back to NULL. Condition ptr != NULL checks whether array is allocated.
Adding to this... If working with C++, get used to cleaning up any allocated memory in the destructor. If you get use to getting into the habit of writing the cleanup code in the destructor immediately after you've written the allocation code, you're more likely to end up with code that doesn't leak.
-
December 13th, 2009, 02:50 PM
#10
Re: Good practice for knowing if something has been allocated?
Originally Posted by Arjay
Adding to this... If working with C++, get used to cleaning up any allocated memory in the destructor. If you get use to getting into the habit of writing the cleanup code in the destructor immediately after you've written the allocation code, you're more likely to end up with code that doesn't leak.
Yes, and if you take my suggestion of using std::vector of a more appropriate container, then you would not even have to worry about that (unless you are writing your own container class from scratch).
-
December 13th, 2009, 02:51 PM
#11
Re: Good practice for knowing if something has been allocated?
Have you considered coding your own garbage collector?
-
December 13th, 2009, 03:03 PM
#12
Re: Good practice for knowing if something has been allocated?
Originally Posted by laserlight
For the other point, I am talking about the fact that one would not normally write:
Code:
if (name){ delete[] name; name = 0;}
rather, one would write:
Code:
delete[] name; name = 0;
Oh, I see what you're talking about. The problem is that if you have a class like this:
Code:
class foo{
public:
foo(void){
myArray = 0; //may or may not ever actually get used
}
~foo(void){
delete[] myArray; //Doesn't work, you have first check if it was initalized
}
private:
int * myArray;
};
That's what the check was for in the macro.
-
December 13th, 2009, 03:06 PM
#13
Re: Good practice for knowing if something has been allocated?
Originally Posted by ninja9578
That's what the check was for.
No, that check would be useless. If the member pointer was not initialised, the check could still allow the delete[] to happen, leading to undefined behaviour. If the member pointer was initialised to be a null pointer or to point to a valid object (created by new[]), then the check is redundant.
Last edited by laserlight; December 13th, 2009 at 03:08 PM.
-
December 13th, 2009, 03:19 PM
#14
Re: Good practice for knowing if something has been allocated?
Originally Posted by laserlight
Yes, and if you take my suggestion of using std::vector of a more appropriate container, then you would not even have to worry about that (unless you are writing your own container class from scratch).
Agreed.
-
December 13th, 2009, 03:22 PM
#15
Re: Good practice for knowing if something has been allocated?
Originally Posted by ninja9578
Oh, I see what you're talking about. The problem is that if you have a class like this:
Code:
class foo{
public:
foo(void){
myArray = 0; //may or may not ever actually get used
}
~foo(void){
delete[] myArray; //Doesn't work, you have first check if it was initalized
}
private:
int * myArray;
};
That's what the check was for in the macro.
Isn't using delete on a NULL param a no-op so there isn't any reason to guard the delete statement?
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
|