Welcome, Guest. Please login or register.

Author Topic: sizeof (*VarPtr) possible ?  (Read 5336 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline AJCopland

Re: sizeof (*VarPtr) possible ?
« on: January 30, 2007, 07:05:18 PM »
I don't get what the problem is.

Dereference the pointer and do sizeof as you seem to be doing.




#define GETMETHESIZEOFTHINGATENDOFPOINTER(ptr) sizeof(*ptr)

void dummyFunc( ThisThingIsbiggerThan4bytes *ptr )
{

   assert( NULL!=ptr );
   cout << sizeof(*ptr);   // prints sizeof ThisThingIsbiggerThan4bytes
   cout << sizeof(ptr);    // prints 4
   cout << GETMETHESIZEOFTHINGATENDOFPOINTER(ptr); // prints sizeof ThisThingIsbiggerThan4bytes
}




The macro will just be replaced by the call to sizeof. But it's not type safe, and it's a very bad idea.

Why exactly are you trying to do this? What are you hoping to achieve? Finally, NEVER do anything like what i just showed you above :-D

I just threw together a small test app and it did exactly what I'd expect.

Andy
Be Positive towards the Amiga community!
 

Offline AJCopland

Re: sizeof (*VarPtr) possible ?
« Reply #1 on: January 30, 2007, 07:15:41 PM »
You might also want to take a look at something like this page.
Writing CPP macros

Then throw away all of your macros :-D

Writing good code generally means there won't be a lot of macros in it, not if I can avoid it anyway. I'll inline functions (type safety checking) if I want a function and I'll "const int blah = 1;" in a namespace if I want constants.

Just my opinion of course.

Andy
Be Positive towards the Amiga community!