Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,159 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,915 people online right now. Registration is fast and FREE... Join Now!




Validating array sizes in C++

 
Reply to this topicStart new topic

Validating array sizes in C++, Is this how to do it?

Imek
6 Dec, 2007 - 03:38 AM
Post #1

D.I.C Head
**

Joined: 25 Oct, 2007
Posts: 52


My Contributions
Hey,

I know that working out the size of an array is a common question, but mine's slightly different.

I'm working on a fairly big C++ project and am new to the language, but from previous experience I've been programming defensively - i.e. checking all parameters to member functions and such.

One example of this is checking that an array isn't too big or too small when it is provided. Here's the kind of thing I've been doing:

CODE
MyClass::MyClass(int *arrThings, unsigned int iNoVals)
{
    for (unsigned int i=0; i<iNoVals; i++)
    {
        if (!arrThings[i])
        {
            throw Exception("Problem! The array provided to the MyClass constructor was too small for the provided size.");
        }
        // Validate individual value here
    }
    if (arrThings[i])
    {
        throw Exception("Problem! The array provided to the MyClass constructor was too large for the provided size.");
    }
}


I have a feeling that this isn't right, but I've busted my project so it won't compile for the moment, and so can't test it properly. Is this the case?

Thanks,

-Joe
User is offlineProfile CardPM
+Quote Post

Bench
RE: Validating Array Sizes In C++
6 Dec, 2007 - 03:52 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Try using a vector instead, then you won't need to worry about the bounds of an array.

According to one particular (and well respected) C++ guru "arrays are evil"
http://www.parashift.com/c++-faq-lite/cont...s.html#faq-34.1
User is offlineProfile CardPM
+Quote Post

Imek
RE: Validating Array Sizes In C++
6 Dec, 2007 - 08:45 AM
Post #3

D.I.C Head
**

Joined: 25 Oct, 2007
Posts: 52


My Contributions
Well, I generally didn't want to use vectors unless I really needed to. But I suppose from those arguments this kind of thing counts as needing to use them.. Thanks for the advice.
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Validating Array Sizes In C++
6 Dec, 2007 - 09:18 AM
Post #4

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
Checking for array bounds is a good thing and I commend you for practicing
safe programming.


One of the things to look at when validating arrays is how a particular
function will be using the array. For example, if the function is only
going to access the array but not make any modifications, consider
passing in the array as a const.


void readArray(const int *myArray, int size);


The other thing to check for (if you are using pointers
to access the array) is to check and make sure that it is not NULL before
trying to de-reference the array


if(!myArray) return;
// Ok to access
myArray[idx] = 0;



On to your code.
The second if statement is not part of your loop so arrThings[i] will error.
Also doing this if(!arrThings[i]) will not guarantee that the array is valid.
All the check does is determine if the value is not 0. However, 0 may be a possible value for
the array passed in.

The values inside of the array (as far as I understand) will not provided any information regarding
the bounds of the array itself.

I would use Bench's idea,
or you can add your own Add(int val); function so that the user
can call this to place values into your class which then allows you to control your internal state.

Cheers
User is offlineProfile CardPM
+Quote Post

Imek
RE: Validating Array Sizes In C++
6 Dec, 2007 - 10:24 AM
Post #5

D.I.C Head
**

Joined: 25 Oct, 2007
Posts: 52


My Contributions
Ah yes, the const thing - I've been going through all my code since I discovered that it was a good idea to make everything possible const, but I think I missed this kind of thing - thanks. It seems that, in general, it's a good idea to use all these new-fangled vector/reference/template thingies when they're visible from outside the class.

Oh yeah, and I meant to initialise i outside of that for loop so that I'd be able to get it afterwards. To be honest I just threw together that bit of code as an example of the type of thing I was doing, but well spotted.

Anyway, it looks that I have a bit of changing around to do; thanks for the reply.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:44PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month