Passing Array into function C++ issue

Started by
14 comments, last by SiCrane 13 years, 2 months ago

Why not just use a Boost Array? That's the closest equivalent to a Java array.

It has all the type safety and container functions without the overhead of a vector.


1. This is For Beginners, as such boost is generally a no-no. Heck, even our template skulldrudgery up above shouldn't be here.

2. "overhead of a vector" is a misnomer. It has the same amount of overhead as an array. One would use boost array instead of a vector in the case when one has a statically sized array but would like the advantage of a uniform interface (begin(), end(), etc.) for access to the array.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement
Simple solution use _countof() Link
"My word is my bond and here I stand."
Just pass the size of the array into the function like Washu said until you start using std::vector and then you can use size()

void modifyArray(int array[],int size)
{
for (int i=0;i<size;++i)
{
//do something useful with array
}
}


Templates look funky until you have a good understanding of them.
Just a reminder, sizeof() is not a function. It is a compile-time operator. If you can't substitute a constant value for sizeof(something) and expect your code to work reliably, then you've used it incorrectly.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Since when is Boost discouraged in For Beginners?

All of my prior experience on Gamedev.net would suggest the exact opposite actually.
I trust exceptions about as far as I can throw them.
In any case, most up to date compilers now offer std::array in the <array> header, as it's at C++0x feature. Slightly less up to date compilers have the equivalent std::tr1::array from TR1.

This topic is closed to new replies.

Advertisement