Why 'sizof (CBuffer::m_pData)' doesn't work ?

M

Martin Ambuhl

Timothy said:
Hello

I've noticed that 'sizeof (CBuffer::m_pData)' doesn't compile.

Is this behavior present by chance or there is a good reason for it ?

There is a good reason: 'CBuffer::m_pData' is a syntax error. Perhaps
you wanted where such silliness is tolerated.
 
T

Timothy Madden

Hello

I've noticed that 'sizeof (CBuffer::m_pData)' doesn't compile.

Is this behavior present by chance or there is a good reason for it ?
Is there a better alternative other than

sizeof ((CBuffer *)NULL)->m_pData

wich I currently use ?

Thank you
Timothy Madden
Romania
 
M

Merrill & Michele

Perhaps this is because :: is an invalid token in C?

That would be the whammy. The double whammy is quoting anything by
Aerosmith. You left out "Dude looks like a lady" and "you can't catch me
cuz the rabbit done died."
 
D

Default User

Timothy said:
Hello

I've noticed that 'sizeof (CBuffer::m_pData)' doesn't compile.


This looks an awful lot like C++, not C. If so, try comp.lang.c++.




Brian Rodenborn
 
K

Keith Thompson

Timothy Madden said:
Ok ok ok ...
I knew I might overlook something when I posted to c.l.c.
However if I have this declaration:

struct CBuffer
{
unsigned char *m_pData;
size_t m_nSize;
};

how should I get the sizeof (*(struct CBuffer *)main).m_pData in a way that
many could say it is nicer ?
(this code really compiles in C, I've tested)

Casting the value of main is, to say the least, bizarre, but you're
taking advantage of the fact that the argument to sizeof isn't
evaluated (unless it's a C99 VLA).

Similarly, this should work without being quite so ugly:

#include <stdio.h>

struct CBuffer
{
unsigned char *m_pData;
size_t m_nSize;
};

#define MEMBER_SIZE(type, member) (sizeof ((type*)0)->member)

int main(void)
{
printf("MEMBER_SIZE(struct CBuffer, m_pData) = %d\n",
(int)MEMBER_SIZE(struct CBuffer, m_pData));
printf("MEMBER_SIZE(struct CBuffer, m_nSize) = %d\n",
(int)MEMBER_SIZE(struct CBuffer, m_nSize));
return 0;
}

(If the argument to sizeof were evaluted, this would dereference a
null pointer, but it isn't so it it doesn't.)
 
T

Timothy Madden

Joona I Palaste said:
Perhaps this is because :: is an invalid token in C?

Ok ok ok ...
I knew I might overlook something when I posted to c.l.c.
However if I have this declaration:

struct CBuffer
{
unsigned char *m_pData;
size_t m_nSize;
};

how should I get the sizeof (*(struct CBuffer *)main).m_pData in a way that
many could say it is nicer ?
(this code really compiles in C, I've tested)

P. S. How come c.l.c has far more messages than c.l.c++ ?

Thank you
Timothy Madden
Romania
 
C

CBFalconer

Keith said:
.... snip ...

#include <stdio.h>

struct CBuffer
{
unsigned char *m_pData;
size_t m_nSize;
};

#define MEMBER_SIZE(type, member) (sizeof ((type*)0)->member)

int main(void)
{
printf("MEMBER_SIZE(struct CBuffer, m_pData) = %d\n",
(int)MEMBER_SIZE(struct CBuffer, m_pData));
printf("MEMBER_SIZE(struct CBuffer, m_nSize) = %d\n",
(int)MEMBER_SIZE(struct CBuffer, m_nSize));
return 0;
}

Er - did you ever try that macro expansion?
 
K

Keith Thompson

CBFalconer said:
Er - did you ever try that macro expansion?

Yes, I did. I just tried the program again with several different
compilers, and it works properly with no warnings on all of them. I
also ran "gcc -E" (after commenting out the "#include <stdio.h>"), and
the results looked reasonable. It looks like it dereferences a null
pointer, but it doesn't really because the operand of sizeof is not
evaluated (unless it's a VLA, but that's not the case here).

Do you see a problem that I'm missing?
 
C

CBFalconer

Keith said:
Yes, I did. I just tried the program again with several different
compilers, and it works properly with no warnings on all of them. I
also ran "gcc -E" (after commenting out the "#include <stdio.h>"), and
the results looked reasonable. It looks like it dereferences a null
pointer, but it doesn't really because the operand of sizeof is not
evaluated (unless it's a VLA, but that's not the case here).

Do you see a problem that I'm missing?

I was thinking that "struct" replaced the first param, "CBuffer"
the second, and the third "m_pData" supplied param was left in the
air.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top