Archived

This topic is now archived and is closed to further replies.

How can I check if a pointer...

This topic is 6572 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

i'm not sure i understand the question...but my only guess is you mean testing for a NULL pointer...which is done like this:

if(myPoiner != NULL)
{
// i should have a valid pointer in here
// so it's safe to use
}
else
{
// I DO NOT HAVE A VALID POINTER
// probably return an error code or throw exception....
}

if this isn't what you mean... please post a more detailed question

Share this post


Link to post
Share on other sites
Thanks for your quick anwser!

In fact I want to check how many elements a pointer to a field have.

To check how many elements a field have, you use this gimmick: int nr = sizeof(vect) / sizeof(vect[1]); But if you try to use this metod on a pointer to a field, I alwalys get the answer 0.

I would be very glad if you could help me here!

------------------
Gandalf the White

Share this post


Link to post
Share on other sites
quite simply, you cannot get the size of a dynaimcally allocated array using sizeof(). It will always return 4 (or whatever size a pointer is on the platform). To get the size at runtime, you either need to store it in a separate variable (like num_elements) or use static arrays, but I recommend using a separate variable, because it gives you more flexibility.

But it all depends on what you're using the array for. If it can be static without wasting too much memory and doesn't need to grow dynamically, then use a static array, like this:

code:

float static_array[20]; // 20 fixed elements


sizeof(static_array) will give you 80 (eighty bytes), so yuo need to divide by the size of one element to get the total number of elements in the array. It looks like this:
code:

num_static_elements = sizeof(static_array) / sizeof(static_array[0]);

Hope this answers your question

Share this post


Link to post
Share on other sites
Linked lists are the better chioce for your purpose. Imagine you want to delete one creature as it is killed and add it to some other list like a simple object list to represent the dead body you would have to reallocate the memory for your object array, create a copy of the creature in there, overwrite the creature in the array with the last creature and, again, reallocate the memory for your creature array. But much work isn't it? While by using the linked list you'd just have to remove it from the creature list (1 call) and then add it to the object list (again 1 call) or the other way around. Of course there will be some memory allocated and released, but it's faster as there is no need to allocate memory for the whole object as it is already allocated and it's much more comfortable to use a nice class like good written linked list.
I use it for most cases as much more flexible than arrays and way easier to use. And then, there's the little advantage of sorted linked lists or the fact that you don't need counters in for-loops. Simply a pretty nice invention...

Alexander Stockinger
Programmer

------------------
Alexander Stockinger
Programmer

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I find linked lists very useful as well. Just a question that sort of been nagging me. Does allocating memory take much CPU time? It seems to me having a memory pool of objects, and implementing a linked list within that is cleaner than asking the OS for memory every time something is added.

For the most part, I've been using 'new' and 'delete' to deal with allocating new linked list objects. At one point I had a class to handle linked lists because I didn't really like the new syntax it created for accessing the list.. sure it creates lots of repeated code, but it's all the same and it all works so I'm not terribly worried about it.

Share this post


Link to post
Share on other sites