# pointer length

## Recommended Posts

BloodLust666    104
how do i find the total number of items in an array? ex: bool *Flag = CreateArray(); bool newArray[GetArraySize(Flag)]; edit: I was Flag and newArray to have the same number of items in the array

##### Share on other sites
Roboguy    794

• You don't. Use std::vector or one of STLs other containers

• Line two wouldn't work even if you could, when you make arrays like that the size must be known at compile time

##### Share on other sites
SiCrane    11839
You don't.* Keep track of it or use something like std::vector.

*without some horrible platform dependent hacks.

##### Share on other sites
BloodLust666    104
i need to use that, or else can someone tell me the exact length of the array in SDL_GetKeyState(NULL)?

I want to have another array the same size as the one returned by that function

##### Share on other sites
Roboguy    794
Quote:
 Original post by EvilKnuckles666i need to use that, or else can someone tell me the exact length of the array in SDL_GetKeyState(NULL)?I want to have another array the same size as the one returned by that function

The size of the array returned by SDL_GetKeyState is stored in it's argument.

##### Share on other sites
BloodLust666    104
then y can i pass NULL into the arguement and an array, of an unknown size of which i need to know, is returned?

##### Share on other sites
Roboguy    794
Quote:
 Original post by EvilKnuckles666then y can i pass NULL into the arguement and an array, of an unknown size of which i need to know, is returned?

If the argument is NULL it just ignores it. The reason it can be NULL is because you normally don't need to know the size of the array.

##### Share on other sites
BloodLust666    104
but for what i'm doing i want to know the size

##### Share on other sites
Roboguy    794
Quote:
 Original post by EvilKnuckles666but for what i'm doing i want to know the size

Yes... then you specify the argument (which is a pointer to an integer). The variable given as the argument will contain the number of elements in the array after the call.

##### Share on other sites
Zahlman    1682
SDL is fundamentally a C API (although of course it works in C++ as well). It is a common trick for such APIs to behave like this: a parameter is passed in by pointer, for use as an "out parameter" (in C++ one would use references, but this option is not available in C). The function checks if the passed pointer is NULL, and skips any "reporting" in that case (so as not to cause a segfault - this may allow the function to do less work if needed). If it is not NULL, it will modify the passed-in thing via the pointer.

In the case of this SDL function, the modification is to write to the pointed-at integer value, writing the size of the array being returned (rather, the size of the allocated memory pointed at by the pointer being returned).

Here is how we may construct a std::vector instance which contains the same data as what was returned (and then the vector instance tracks the size internally, and is also capable of resizing itself when/if needed, and will also do all the rest of its own memory management - BTW, do you know whether or not you're supposed to free() that returned pointer when you're done with it? Or perhaps delete[] it?):

#include <vector>// ...int size;bool* tmp = SDL_GetKeyState(&size);vector<bool> copiedKeyState(tmp, tmp + size);

The vector constructor takes two pointers, representing a "range" of memory - from the beginning of the returned data (where the pointer is) to the end (the beginning plus the length - that points one past the end, but that's how these ranges are defined to work - Trust Us(TM), in the long run it's much more convenient that way). It will automatically copy the data over into a new memory allocation, which it "owns" (it will automatically be delete[]d when the vector falls out of scope).