Array length in C++?

Associate
Joined
22 Sep 2009
Posts
2,085
Location
Leicester
Not something I've ever had to do before (shockingly), maybe I'm just being dim but shouldn't:

Code:
void c_maze::blankCellsList(s_blanks *list)
{
	int listSize = sizeof(list) / sizeof(s_blanks);
Work?
 
Last edited:
No. For a variety of reasons:

sizeof(list) will return the size of the pointer and not the size of the block it's pointing at.

It's also bad to assume there's no compiler padding etc by doing pointer arithmetic unless it's byte aligned. If the type changes then you code breaks.
 
That is one option.

Usually, unless you're designing for supercomputer high performance or embedded system efficiency, then you'll not need to stray away from the standard libraries.
 
I was just trying to avoid vectors to keep the library portable to C, but when I think about it C has very limited use these days anyway. Vectors it is!
 
Back
Top Bottom