Sign in to follow this  
zyrolasting

[C++] Pointer safety on virtual stack

Recommended Posts

zyrolasting    280
I store addresses on a stack in a virtual machine project, which I use to dereference data to work with for a scripting language. I've heard people get very funny about pointers by making working with them sound like brain surgery. I ask solely from the fear induced by several online articles: If I never store function or member pointers on my stack (I know they vary), can I always expect a pointer size to be sizeof(int)? Are there other details on what affects the size of an arbitrary pointer?

Share this post


Link to post
Share on other sites
SiCrane    11839
In theory, the only guarantees you have are sizeof(void *) and sizeof(char *) are the same, that a pointer to cv-qualified T and a pointer to T are the same size and that when you convert a pointer to T to void *, when you convert it back you get the original pointer. Otherwise all bets are off. The size of a pointer to a derived class may not even be the same as a size of a pointer to its base class.

In practice, pretty much every implementation has every T * the same size. Only about 75% of the time will it be the same size as int though. However, it will almost always be the same size as size_t.

Share this post


Link to post
Share on other sites
zyrolasting    280
Quote:
The size of a pointer to a derived class may not even be the same as a size of a pointer to its base class.

In practice, pretty much every implementation has every T * the same size.


Can I get more details on this? From the way you put it, these two points sound like they are contradicting. (A derived class is still a type T, which as you said would still have T* be the same size across most implementations.)

Let me show you what I'm doing: My stack works with char*, and I use something like the following to refer to a pointer of type T off of it. Function pointers aside, what limitations did I put myself in and why? I don't even know where you guys get the information for the darker side of pointers, so forgive my prodding.

/* Pointer to value on stack. (is really a STL vector)
The value itself is another address, so p can be considered T**. */


char* p = &stack[offset];

T* t = *((T**)p);

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by zyrolasting
Can I get more details on this? From the way you put it, these two points sound like they are contradicting.


One sentence comes from a paragraph that begins with the words "in theory". The other sentence begins with the words "in practice". When you have two paragraphs like that, where one begins with the words "in theory" and the other begins with the words "in practice", you can generally assume that "in theory" applies to every sentence in the first paragraph. So, in theory, Base * and Derived * could have different sizes, but, in practice, they won't.

Share this post


Link to post
Share on other sites
zyrolasting    280
I appreciate the precision, but I won't be the only one to be confused by that.

Anyway, I figured I could remedy my problem by pushing and popping pointers onto the stack where both methods to do so are templates. This is just so they always catch varying values for sizeof(T*).

Share this post


Link to post
Share on other sites

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