Jump to content
  • Advertisement
Sign in to follow this  
WhatEver

How many bytes does a void pointer take up?

This topic is 4812 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 many bytes does a void pointer variable take up? Are all pointer variables the same size? For example, how many bytes would this structure be: struct sizeme { char* Name; short DataID; void* Data; sizeme* Next; sizeme* Prev; }; My guess is that this struct is worth 18 bytes; 4 bytes each for the pointers, and 2 bytes for DataID.

Share this post


Link to post
Share on other sites
Advertisement
Yep, typically a 4-byte pointer. Although this is dependent on the architecture of the CPU.

ace

Share this post


Link to post
Share on other sites
Not all pointers are the same size. In particular, pointers to member functions can be much bigger than pointers to primitive types. On 32 bit platforms pointers tend to be 32 bits in size, on 64 bit platforms pointers tend to be 64 bits in size.

Share this post


Link to post
Share on other sites
The size of that struct is 20 bytes, because the struct size is padded to the natural alignment of its biggest member, as well as each member being put on its natural alignment (in general). In this case, there will be two unused bytes after the short. (At least on all platforms you're likely to use, with typical compiler settings)

SiCrane is also right: member function pointers may be up to 5x as large as other pointer kinds. In fact, depending on the class involved, the size may vary, and you can even get silent pointer slicing bugs in your code if you're not careful! So, don't use pointer-to-member-function too much :-)

Share this post


Link to post
Share on other sites
Why would a large structure need a larger pointer size than a primitive variable? I thought a pointer was just a variable that contained an address instead of a value.

I think I'm having a revelation here. Ok, so if 32 bit machines use 4-byte pointers, that means it can address up to 0x00000000+4294967295, which means the maximum amount of memory you can have of RAM is 4096 megs?

Share this post


Link to post
Share on other sites
No pointer-to-member-functions for me. The syntax is insane to look at, and I personally haven't found a use for them yet, so I stick with standard function pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by WhatEver
Why would a large structure need a larger pointer size than a primitive variable? I thought a pointer was just a variable that contained an address instead of a value.

I think I'm having a revelation here. Ok, so if 32 bit machines use 4-byte pointers, that means it can address up to 0x00000000+4294967295, which means the maximum amount of memory you can have of RAM is 4096 megs?


Which is 4 GB. Most mobo's cant handle mre than that anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by WhatEver
Why would a large structure need a larger pointer size than a primitive variable? I thought a pointer was just a variable that contained an address instead of a value.

Not large structures, but complex ones. Classes, for instance, have inheritance that complicates things. Pointers to member functions need to account for this, so the size of their pointers may well increase to hold the extra information. I didn't know this until just now, but a google of 'sizeof pointer to member functions' results in a few pages that each mention the problem. Fascinating stuff.
Quote:
Original post by WhatEver
I think I'm having a revelation here. Ok, so if 32 bit machines use 4-byte pointers, that means it can address up to 0x00000000+4294967295, which means the maximum amount of memory you can have of RAM is 4096 megs?

Exactly. In actual RAM, at least. With virtual memory you can get considerably more than that if needed.

CM

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!