Sign in to follow this  

Array Class Deletion before Creation

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my array class ( a generic type using templates btw ) I specify a constructor that takes a 32-bit integer variable to be the number of cells in the array. However in my constructor I want to determine whether or not the requested cell value exceeds 65,535 if it does, dont create the array. Heres the constructor
// Constructor
CArray( int iSize )
{
	// Check for valid size limit
	if( iSize > 65535 )
	{
		// Write Error Message, eventually
		return;
	}
	else
	{
		m_pArray = new DType[ iSize ];	// Create the Array
		m_iSize = iSize;		// Set Array Size
	}

	return;
}

Obviously by stating return in the constructor I'm not achieving anything (dont really know if I should actually do that either but, meh!). At first I thought saying delete this; would be the answer but no, it doesn't work. How can I avoid my array class creating the array if the number of cells is greater than I ever want it to be? - Adam

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why the arbitrary limitation in the first place? To answer your immediate question, you have to throw an exception.

P.S. I love how you threw in "( a generic type using templates btw )". Congratulations. Would you like a cookie?

Share this post


Link to post
Share on other sites
I'm not sure an exception in a constructor is the best thing to do. Before the body of the constructor is even reached, memory is allocated and members are initialized.

If a constructor is called, the object WILL be instantiated and will have to be destroyed afterwards. Therefore, your best bet would be to prevent calling the constructor by making it private and implementing a static function that throws an exception on bad input, and calls the constructor on correct input.

But in a case like yours, I take it the constructor should never be called with an incorrect argument, so you might as well use assert( ) to check if the size is acceptable, in order to catch bugs if you ever pass an incorrect value.

Share this post


Link to post
Share on other sites

#include <cstddef>

template < typename Tp >
struct CArray {

typdef size_t size_type;
const size_type max_size = 65535;
//.....
CArray(size_type n): m_iSize(n), m_pArray((n <= max_size) ? new Tp[n] : 0) {}
//...
};


Is there reason not to use the standard library dynamic array std::vector? because its not very efficent way to implement it and not very exception safe either.

Share this post


Link to post
Share on other sites
P.S. I love how you threw in "( a generic type using templates btw )". Congratulations. Would you like a cookie?

--------------------------------

Is that supposed to be sarcasm? I really can't tell when your saying it on a message board.

Anyway, the reason I wish to impose this limit is for the sake of file sizes in my project. The program using this class will be writing arrays to file and I wish to be able to hold a file size limit, so this is obviously one way of doing it.

Further help on how I can achieve this limit would be helpfull as the AP obviously wasn't.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This is a bit old, and the problem was possibly adequately solved (to the OP's satisfaction)... but I'll reply anyway. :)

Quote:
Original post by Webbster
P.S. I love how you threw in "( a generic type using templates btw )". Congratulations. Would you like a cookie?

--------------------------------

Is that supposed to be sarcasm? I really can't tell when your saying it on a message board.


Yes. That comment came off to me as 'bragging', as that information wasn't at all pertinent to the question at hand.

Quote:
Original post by Webbster
Further help on how I can achieve this limit would be helpfull as the AP obviously wasn't.


I answered your question. What you want to do can be achieved by throwing an exception. To be honest, though, your original code actually does specifically what you asked how to do (prevent creating the internal array in the class if too large a size is requested). But it still leaves you with a properly constructed object; throwing an exception won't. I don't think that if an array object has a limit of N elements, that it should be constructed at all if you request greater than N elements.

I also don't quite understand ToohrVyk's objection. It seems as though he believes that if the constructor throws, the memory allocated for the object being constructed is not freed. This is simply not true no matter how said object was constructed (dynamically or statically.) Perhaps I misunderstand his arguement.

Quote:
Original post by Webbster
Anyway, the reason I wish to impose this limit is for the sake of file sizes in my project. The program using this class will be writing arrays to file and I wish to be able to hold a file size limit, so this is obviously one way of doing it.


It seems as though your abstracting this responsibility 'incorrectly'. I believe it shouldn't be your array classes responsibility to enforce this file size limit (it knows absoloutly nothing about files, or, rather, it shouldn't). Rather, it should be the client of this class that enforces the limit. Hard to say, though, really, without a bit more information.. perhaps your class is just poorly named. :) Basically, I suspect you actually asked the wrong question.. you appear to want to know a way to enforce your file size limit, that fits in with your design.

And really, you should be using std::vector(or std::deque/list, whatever) as snk_kid suggested. It just makes life easier. :)

Share this post


Link to post
Share on other sites

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this