Advertisement

How can I check if a pointer...

Started by December 13, 1999 05:52 PM
6 comments, last by Gandalf 25 years ago
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

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

Gandalf the Black
Advertisement
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

Thanks! That was probably the answer to my question (mayby I should consider using a linked list to all my monsters).

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

Gandalf the Black
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

Alexander Stockinger
Programmer
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.

Advertisement
...have any elements? I have allocated memory for the struct dynamicly.

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

Gandalf the Black
I think most implementations of new or malloc() already give you memory pooling... and if they don't, I think the OS does too...

Mason McCuskey
Spin Studios
www.spin-studios.com

Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!

This topic is closed to new replies.

Advertisement