Array Class Deletion before Creation

Started by
4 comments, last by GameDev.net 19 years, 2 months ago
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
Advertisement
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?
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.
#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.
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.
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. :)

This topic is closed to new replies.

Advertisement