Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

pointer memory

This topic is 4843 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 would i be able to find out how much memory a pointer takes up? Such as a "char" is 1 byte, a "short" is 2 bytes, etc... is there one amount for pointer or is it a certain fraction of the type it points to or what?

Share this post


Link to post
Share on other sites
Advertisement
A pointer is just storage for a memory address, assuming your compiling for a 32 bit machine it will be 4 bytes, 64 bit machine it will be 8 bytes. This is why 32 bit windows has a 4gb memory address limitation.

Share this post


Link to post
Share on other sites
The pointer itself holds the address of the other variable. The size of that piece of information is completely independent of the type of the data you're pointing to. The size of a char* is the same as the size of a long* this is why you can define pointers to incomplete types.

You can get that size by simply doing sizeof(long*) or long* ptr; ... sizeof(my_ptr)

The only exception are pointers-to-members, which are not pointers. Their size is implementation-defined and may change depending on the kind of class the thing you are pointing at is a member of.

If you want to know the size of the data itself, well, you can only rely on the type of the pointer itself: except for polymorphic types, the data you point to doesn't keep any identifying information, and even polymorphic types do not keep track of their size in a way that is accessible to the programmer. If you use a char* to point at double data, the computer will treat the byte patterns as if they were char, not double. So all you can really do is either sizeof(long) or sizeof(*my_ptr).

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
ok, well, how does the memory work when say you do something like

cMyType* ptr = 0;
ptr = new cMyType();

and say cMyType is 10 bytes in size


the size of ptr would still be 4 bytes (assuming a 32 bit environment) because all it contains is a memory location, not data.

The size of (*ptr) would be ten bytes, because the data structure that ptr points to is ten bytes.

Share this post


Link to post
Share on other sites
Because you estimate the size of a POINTER to the CMyType class, not the size of the class. Those two things are different:

CMyClass *pMyClass = new CMyClass;
int iSize1 = sizeof(pMyClass); // Returns the same as the size of a pointer - 4 bytes
int iSize2 = sizeof(CMyClass); // Returns the size of the class, not the pointer

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
ptr = new cMyType[5];

how big woudl the pointer be then?


Still the same, the pointer would be (on most machines) 4 bytes, and would contain the address of a memory block that is big enough to hold 5 cMyType objects.

Share this post


Link to post
Share on other sites
ptr[0] is a reference to an object of cMyType. As such if you did sizeof(ptr[0]) it should return the same value as sizeof(cMyType).

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!