Sign in to follow this  

Enginuity blob-size question

This topic is 4554 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

Hi. Guess most of you guys know (and hopefully understand) the Enginuity series. I think (or hope) I'm getting there. But what I don't get right now is why dataSize+sizeof(this); is supposed to show the size of the whole dynamic buffer. Why doesn't the Constructor assign
size*sizeof(T)
or
sizeof(buffer)
(after creating the object) to dataSize? Right now it looks to me as if it only returns the correct size if T has a size of 1 Byte. What am I missing? Here the code I'm talking about:
template<class T>
class CMMDynamicBlob : public IMMObject
{
   protected:
      unsigned long dataSize;
      T *buffer;
   public:
      inline T& operator [](int index)
      {
         assert(index<dataSize && "Bad index on CMMDynamicBlob::[]");
         return buffer[index];
      }
      inline operator T*()
      {
         return buffer;
      }
      CMMDynamicBlob(unsigned long size)
      {
         dataSize=size;
         buffer=new T[size];
         assert(buffer!=0 &&
           "DynamicBlob buffer could not be created - out of memory?");
      }
      ~CMMDynamicBlob()
      {
         if(buffer)delete[] buffer;
      }
      unsigned long size()
      {
         return dataSize+sizeof(this);
      }
      inline unsigned long blobSize()
      {
         return dataSize;
      }
};

Share this post


Link to post
Share on other sites
dataSize is a silly name, IMHO, because its meaning isn't clear. It appears to be a dataCount in which case the constructor and [] operator are consistent and correct, but the size function is, as you said, wrong.

I think it should be returning dataSize * sizeof(T) + sizeof(this);

However, I can't actually think of any time I would call this size function, so perhaps this has never shown up because noone has ever used it?

Where is this thing being used? Is it really justified having it atall? I've not read enginuity...

Share this post


Link to post
Share on other sites
size() is virtual in the base class IMMObject. There is a static list of all objects derived from it.
So it is possible to keep track of the currently needed memory.
Probably not bad for debugging.

But yeah you are right ... the array size is needed for the assort macro ...
so returning the actual data size would make more sense than assigning it to dataSize in the constructor.

Thanks for your reply.

Share this post


Link to post
Share on other sites

This topic is 4554 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.

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