• Advertisement
Sign in to follow this  

Few Questions About Pointers

This topic is 4855 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 much memory does a NULL pointer take up? I'm going to have to have a copy of a class in every single one of my tiles in my game, and that object isn't always going to exist in that tile. With that said, I would like to allocate memory for that object only if it should exist there, and that's not always the case. So, if I have a class, CFoobar, and I initialize with this statement: CFoobar* foobar = NULL; Would this allocate sizeof(CFoobar) bytes and set them all to 0, or would this allocate 0 bytes, and once it's initialized, it would allocate the number of required bytes? Depending on your answer, I may have some follow-up questions. [Edit: Use <tt> for inline fixed-font text rather than the <font> tag. It's easier. [smile]] [Edited by - Oluseyi on January 5, 2005 10:09:33 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Ludi83
hi,

correct me if I'm wrong, but a pointer is alwys 4 byte on a x86 machine.

yes if the machine has a 32-bit processor (1 byte = 8 bits).

Share this post


Link to post
Share on other sites
My goal is to implement this system as efficiently as possible. It would be wasteful to copy CFoobar into every tile on the map, especially if it doesn't always exist.

Now, how much memory would this be worth?
void* foobar = NULL;

Share this post


Link to post
Share on other sites
That uses the exact same amount of memory as the first version.

EDIT: Which is 4 bytes for the pointer itself and 0 bytes for the CFoobar object.

Share this post


Link to post
Share on other sites
Which means, then, that 4 bytes wasted per tile is unavoidable?

This shouldn't be a problem, obviously, since 4 bytes isn't a lot, but nevertheless, it would be even nicer not to allocate anything for something that shouldn't exist.

Share this post


Link to post
Share on other sites
Quote:
Original post by v0dKA
This shouldn't be a problem, obviously, since 4 bytes isn't a lot, but nevertheless, it would be even nicer not to allocate anything for something that shouldn't exist.
But it might, and since it might, take the 4-byte usage as unavoidable.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement