Archived

This topic is now archived and is closed to further replies.

d000hg

Operator overloading : sizeof() ??

Recommended Posts

OK, I see. That isn't the actual size of the object, though. You can just make your own method that calculates the cumulative associated "size" and returns a std::size_t.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 26, 2003 5:47:39 AM]

Share this post


Link to post
Share on other sites
class myclass
{
int *pointer;
.
.
.
};

Say I have something like this, maybe as a wrapper for n image/sound file or whatever. It would be handy to be able to get sizeof() to act on the memory allocated. Though like you say it''s not hard to write a SizeOf() method or maybe even _sizeof(). It''s just a niggle that irritates me!

Share this post


Link to post
Share on other sites
it would be easy enough to have the class keep track of how much memory it allocates within itself, add that to the sizeof for itself, and you have (should be??) the total memory usage of an instance of that class.

I say should be because I''ve heard the actual memory hit of a class is not always accurately represented by the sizeof operator. Anyone have more details on this?

Share this post


Link to post
Share on other sites
this one is in stroustrops faq. He says it''s because sizeof is used internally by the compiler. When you have a pointer, and you do ptr++, it will add sizeof(type) to ptr, instead of just increasing the pointer with one. If you could overload the sizeof operator you would fuck up these things.

My Site

Share this post


Link to post
Share on other sites
Do member functions increase the size of a class? Ie
struct struct FVF_VERTEX1
{
float x,y,z;
DWORD diffuse;
};
class FVF_VERTEX2
{
float x,y,z;
DWORD diffuse;
public:
SetPos(float x,float y,float z);
};
Are these identical in terms of memory alignment? I assume so as some people use D3DX classes for xyz vectors in a FVF structure without screwing it up.

Share this post


Link to post
Share on other sites
quote:
Original post by quasar3d
When you have a pointer, and you do ptr++, it will add sizeof(type) to ptr, instead of just increasing the pointer with one.

It's not instead of , it's how you "increase the pointer with one". It's called pointer arithmetic. For example, that's why you need to cast a void pointer before you can perform pointer arithmetic on it.

Something along the lines of this:

template <class T>
class CTest
{
public:
CTest() : _array(0), _size(0) {}
explicit CTest( std::size_t size ) : _array( new T[size] ), _size(size) {}
~CTest()
{
delete [] _array; // note: delete checks for null

}

...

std::size_t GetSize() const;
...
private:
T* _array;
std::size_t _size;
....
};

template <class T>
std::size_t CTest<T>::GetSize() const
{
return sizeof (T) * _size + sizeof *this;
}

or something along the lines of a member that keeps track of how much memory has been allocated by the object, and then a method to return this value plus the size of the object (sizeof *this).

quote:
Original post by d000hg
Do member functions increase the size of a class?


No, member functions do not increase the size of the class's objects.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 26, 2003 10:46:42 AM]

Share this post


Link to post
Share on other sites
quote:

I thought that sizeof() was resolved into a const size_T at compile time and not at run-time so this would mean that the function cannot be overloaded.


Yes.
quote:

I say should be because I''ve heard the actual memory hit of a class is not always accurately represented by the sizeof operator.


No, the sizeof gives the exact size. what you''re thinking of is when you add up the sizes of the members but the class is actually bigger then the individual sizes. This is because of padding to 32bit (well,on 32 bit systems) boundaries.

Share this post


Link to post
Share on other sites