Tuesday, July 14, 2009

Declaring an array in C++?

Two (related) questions:





What's the difference between





public int[ ] sourceType;





in a header file, and





int sourceType[ ];





in the body of a program?





**** **** **** **** **** **** ****


The program I have is giving me an index out of bounds error when iSource (below) becomes 21. The only place that sourceType is mentioned is in a header file, where it is declared as public int[ ] sourceType. A size is never specified for it (anywhere in the program). Does anyone know why the program is acting as if the array has a size limit of 21 (indexes 0-20) when no size is ever declared?





for (iSource = 1; iSource %26lt;= es. numSources; iSource++)


{





es.sourceType [iSource] = 32;


}

Declaring an array in C++?
Those two variables are completely independent of each other.





You didn't provide the architecture, but I'm guessing that the one from the header file is a field in a class and the second one is inside a function somewhere as a local variable.





I'm guessing that the sourceType referenced in the for loop is the field one (public int[] sourceType) since it's being dot dereferenced (es.sourceType).





The reason you're getting the out-of-bounds error is because es.numSources is %26gt;= 21.





I've got a feeling that loop should read:


for (iSource = 0; iSource %26lt; es.numSources; ++iSource)


{


es.sourceType[iSource] = 32;


}





If the loop is correct then investigate why es.numSources (probably declared in a header as: public int numSources;) is larger than it should be.





Hope that helps.
Reply:All right, this code makes very little sense to me, but I'm going to give you a few of my thoughts on question 2.





1. es.numSources... That dot operator on es makes this look more like C# code than C++. Nonetheless since C++ does support classes, I'll assume C++ here.





2. Any C/C++ compiler I've ever used has required a size/initialization of the array before you use it. So at some point, this array IS being given a size.





3. In your typical for loop iterating through an array, you don't want your test to be "less than or equal to," just "less than."


Reason:


Array indexes start at 0. So in an array of 5 objects, you would have object[0], object[1], ... object[4].


object[5] is in fact the *sixth* object, and that's not possible in an array of size 5.





4. Your test case indicates you are looping until you've gone through all of es's integers. Later, in the loop code, you are attempting to access es.sourceType's objects. These are not the same. Unless you know with absolute certainty that the size of es.sourceType matches the size of es, you cannot hope to accomplish anything with this code.





So to summarize, try changing your code to read:





for (iSource = 0; iSource %26lt; es.numSources; iSource++)


{


es[iSource] = 32;


}





*OR*





for (iSource = 0; iSource %26lt; es.sourceType.numSources; iSource ++)


{


es.sourceType[iSource] = 32;


}





... and see what happens.


No comments:

Post a Comment