I see, thank you Cubbi, I appreciate the feedback.
So if I did something such as this:
1 2 3 4 5 6 7
|
namespace {
struct geometryElementTemp //temporarilly stores a <geometry> element
{
//struct variables etc.
};
}
|
the struct would be visible only in that specific cpp file, if I understand this correctly (I haven't used unnamed namespaces so far). That would be pretty appropriate I guess, for something like this.
That seems counterintuitive. Why use arrays at all? |
Well, my file format presents the content without specifying the number of elements and sub-elements, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<geometry>
<source>...</source>
<source>...</source>
<source>...</source>
<triangles>...</triangles>
</geometry>
<geometry>
<source>...</source>
<triangles>...</triangles>
<quads>...</quads>
</geometry>
<geometry>
<source>...</source>
<triangles>...</triangles>
</geometry>
// etc. , until the end of the file
|
Since a file can have from 1-any number of <geometry> elements, and each <geometry> element can have from 0-any number of sub-elements, I figured that I would do the following: essentially store the content in two passes, make a first pass through the file where I skip the actual data and only store the number of <geometry> elements and the number of each type of sub-element (so that the std::vector and my struct only store the number of elements and sub-elements, not any of the actual data). And then once I know the number of elements in the file, allocate the required memory (mostly arrays of floats and ints) and make a second pass through the file, this time storing the actual data in the allocated arrays (which are kept for rendering, while the vector that stored the number of elements is deleted at this point).
It would be definitely easier and simpler to store all the data in a single pass using vectors, but the reason I wanted to use arrays is in order to avoid the overhead by std::vector::capacity compared to std::vector::size. For example, the sample program on this site for vector::capacity compares a vector of 100 elements and shows that the capacity actually has allocated 141 elements for prospective addition of elements to the vector. (
http://www.cplusplus.com/reference/stl/vector/capacity/ ). So I figured that for storing (at least) thousands of polygon-related elements that will not receive any addition or subtraction of elements after their creation, using std::vectors would lead to an immense memory overhead for no reason or advantage.
If you have any suggestions on my approach or I seem to misunderstand the overhead issue I'd certainly appreciate the feedback though.
Thanks in advance, I appreciate the input.
Ogoyant