Sign in to follow this  

pointer length

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites

  • 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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this