pointer length

Started by
8 comments, last by Zahlman 18 years, 4 months ago
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
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement

  • 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

You don't.* Keep track of it or use something like std::vector.

*without some horrible platform dependent hacks.
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
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
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


The size of the array returned by SDL_GetKeyState is stored in it's argument.
then y can i pass NULL into the arguement and an array, of an unknown size of which i need to know, is returned?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
then 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.
but for what i'm doing i want to know the size
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
but 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.
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).

This topic is closed to new replies.

Advertisement