473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bounded Array class

Is there any way I can improve this bounded array class? What other ops
should I overload? How do I use operator() to make multiple subscripts?
Here's the code:

class OutOfBounds{};

template <class T, unsigned long long Size>
class BArray
{
public:
T& operator[](const T& ix)
{
if(ix>=Size||ix<0)
throw OutOfBounds();
else
return myData[ix];
}
private:
T myData[Size];
};

Thanks!!!!

Mar 3 '06 #1
8 3426
Maybe you can and some useful array operations, see begin(), end(),
rbegin(), rend(), and so on.
And nest class const_iterator and iterator are needed.

with these features you can use you BArray class with Algorithm in STL.

Mar 3 '06 #2

shellux wrote:
Maybe you can and some useful array operations, see begin(), end(),
rbegin(), rend(), and so on.
And nest class const_iterator and iterator are needed.

with these features you can use you BArray class with Algorithm in STL.


Actually, I just want to use it as a standard array replacement; I'll
use std::vector when I need Algorithm. Thanks, though.

Mar 3 '06 #3
Protoman wrote:
Is there any way I can improve this bounded array class? class OutOfBounds{};
This is the first part I would start with: although it is not required,
exceptions being thrown, especially from library components, benefit
from deriving from 'std::exception', either directly or indirectly.
In this case, something like 'std::range_error' seems to be an
appropriate base class.
template <class T, unsigned long long Size>
class BArray
{
public:
Here a default constructor is missing which initializes the elements:

BArray(): myData() {}

Although it makes no difference for non-POD types, POD types would
go uninitialized.
T& operator[](const T& ix)
Are you sure that you want to subscript your array class with the
element type? You might want to add the flexibility to replace your
index type but I doubt that there are many occasions where the index
type happens to be identical to the element type. Obviously, you also
want to add a 'const' version of this operator.
{
if(ix>=Size||ix<0)
throw OutOfBounds();


I'd rahter throw something like

throw std::ranger_error("array index out of bound");

possibly even with the used index and the violated bound.

As noted in another article, I would also equip this class with
proper iterators: the idea behind STL is that it is extremely easy
to get support for all algorithms for a sequence. All you need are
iterators accessing the sequence which can be as simple as pointers
which could be a reasonable approach in the above case.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 3 '06 #4
Protoman wrote:
shellux wrote:
Maybe you can and some useful array operations, see begin(), end(),
rbegin(), rend(), and so on.
And nest class const_iterator and iterator are needed.

with these features you can use you BArray class with Algorithm in STL.


Actually, I just want to use it as a standard array replacement; I'll
use std::vector when I need Algorithm. Thanks, though.


An array doesn't preclude the use of algorithms.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 3 '06 #5
OK, I modified it, but its saying, that, in the copy ctor, that there's
no field called rhs. Here's the code:
class OutOfBounds{};

template <class T, unsigned long long size>
class BArray
{
public:
BArray():myData(new T[size]){}
BArray(const BArray& rhs):rhs.myData(myData){}
~BArray(){delete[] myData;}
T& operator[](const T& ix)
{
if(ix>=size||ix<0)
throw OutOfBounds();
else
return *(myData+index);
}
private:
T* myData;
};

Any help? Thanks!!!!!!!

Mar 4 '06 #6
Protoman wrote:
template <class T, unsigned long long size>
class BArray
{
public:
BArray():myData(new T[size]){}
BArray(const BArray& rhs):rhs.myData(myData){}
The above line shall read

BArray(const BArray& rhs): myData(rhs.myData) {}

to fix the immediate problem. However, this results in the
wrong semantics: this would copy the pointer causing the
copied array to be deleted twice. You rather need to do
something like this:

BArray(BArray const& rhs):
myData(new T[size])
{
std::copy(rhs.myData, rhs.myData + size, this->myData);
}
~BArray(){delete[] myData;}
.... and you also need to define a copy assignment:

BArray& operator= (BArray const& rhs)
{
std::copy(rhs.myData, rhs.myData + size, this->myData);
return *this;
}
T& operator[](const T& ix)
{
if(ix>=size||ix<0)
throw OutOfBounds();
else
return *(myData+index);
}
private:
T* myData;
};


Note, however, that nobody suggested that you replace the statically
sized array with a dynamically allocated one! At least I consider
this the wrong approach to a statically size array: I would go with
the embedded array.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 4 '06 #7

Dietmar Kuehl wrote:
Protoman wrote:
template <class T, unsigned long long size>
class BArray
{
public:
BArray():myData(new T[size]){}
BArray(const BArray& rhs):rhs.myData(myData){}
The above line shall read

BArray(const BArray& rhs): myData(rhs.myData) {}

to fix the immediate problem. However, this results in the
wrong semantics: this would copy the pointer causing the
copied array to be deleted twice. You rather need to do
something like this:

BArray(BArray const& rhs):
myData(new T[size])
{
std::copy(rhs.myData, rhs.myData + size, this->myData);
}
~BArray(){delete[] myData;}


... and you also need to define a copy assignment:

BArray& operator= (BArray const& rhs)
{
std::copy(rhs.myData, rhs.myData + size, this->myData);
return *this;
}


Thanks!!!! Where is std::copy defined? What header?
T& operator[](const T& ix)
{
if(ix>=size||ix<0)
throw OutOfBounds();
else
return *(myData+index);
}
private:
T* myData;
};


Note, however, that nobody suggested that you replace the statically
sized array with a dynamically allocated one! At least I consider
this the wrong approach to a statically size array: I would go with
the embedded array.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence


What if a user created BArray<unsigned long long,10000000> array? Do
you really want God only knows how many bytes on the STACK?!!!!!? But
thanks for the advice.

Mar 4 '06 #8
Protoman wrote:
Thanks!!!! Where is std::copy defined? What header?
Hm, seems like you successfully got me to do you homework.
Well, you need at least research which header the algorithms
are in...
What if a user created BArray<unsigned long long,10000000> array? Do
you really want God only knows how many bytes on the STACK?!!!!!?


There is no defense against blatant stupidity: Pick a sufficiently
large number of elements and the array won't fit into memory at all.
If you really want to defend against too large arrays on the stack,
you should employ some template meta programming which starts putting
the array on the heap if it has grown beyond a certain size. For
reasonably small, fixed-size arrays I would call it inappropriate to
involve memory allocation.

BTW, I just noticed that with the current implementation of the
default constructor, PODs go uninitialized. In fact, writing a
commercial strength array class with dynamic memory allocation
requires considerable more work than is currently done. Effectively,
you need to allocate plain memory, initialized it appropriately, and
release it manually because it is constructed manually.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 4 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gina Yarmel | last post by:
I am writing an application that has two classes that I would like to treat like vectors (in the mathematical sense); I'd like * and + operations for scalar multiplication. I also need the...
0
by: Shivang | last post by:
Hello Everybody, I am using Datagrid control of Winform. I want to have datagrid populated with Unbounded and bounded rows intermittently. I am using tablestyle object as Tablesytle property of...
1
by: Krzysztof Karnicki | last post by:
I have such a problem… I have create my custom DataGridColumn inheriting from System.Windows.Forms.DataGridColumnStyle on using it on DataGrid, to show rows painted by me self. Because dates ...
2
by: Islamegy | last post by:
Some of my win forms was delay couple of seconds when loaded for first time.. i thought there is a problem with the sqlconnection or database but the adapter make no problem when bind to grid?! ...
3
by: Vikramaditya Singh | last post by:
i want to show a serial no column in a data grid which is bounded to a data table. Is it possible. does datagrid has some property to do this
1
by: basulasz | last post by:
May be it is a bit easy question but i don't know the way to display data in bounded colums. I get data from DB with a datareader, and I want to display them on bounded colums. I dont want to...
7
by: GabyFont | last post by:
Hi, I'm new to Access so excuse if my question is too simple. I'm creating an app to manage spare objects in my company. So a user select from list boxes the criteria they want and hit a button...
5
by: karsagarwal | last post by:
I have a bounded form and after I click the button to update/save. THe fields are still there. Is there a way to clear off the fields in the bounded form. Thanks, SA Here's the code that I...
5
by: agarwasa2008 | last post by:
Hi, I have a linked table called tbltest and some bounded forms (which add, update, delete records) that were created using that linked table. For some necessary reasons I had to create another...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.