pointer memory

Started by
9 comments, last by Zahlman 18 years, 8 months ago
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?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
-------------Become part of developing the specifications of a new language. Visit CodeBASIC.org
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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
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 bytesint iSize2 = sizeof(CMyClass); // Returns the size of the class, not the pointer
now what about:

ptr = new cMyType[5];

how big woudl the pointer be then?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
but how big would
ptr[0] be? still 4 bytes?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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).

This topic is closed to new replies.

Advertisement