Review: My C FAQ Page

  • Thread starter Vijay Kumar R Zanvar
  • Start date
J

Jeremy Yallop

Vijay said:
[I wrote:]
[Regarding http://www.geocities.com/vijoeyz/faq/c/sizeof_impl.txt,
where the macro
#define sizeof_type( type ) (size_t)((type*)0 + 1 )
appears]

This invokes undefined behaviour: pointer arithmetic on null pointers
is not defined. Even if the arithmetic were guaranteed to work, the
conversion from pointer to integer is implementation-defined and may
not give the results you expect.

How can this be implemented, then?

It can't, in general, I think. It can't be done in the way you
suggest because there's no way to form type names representing
'pointer to T' from type names representing type T (hence the
restrictions on the type argument to va_arg). It /can/ be implemented
in GNU C using statement expressions, but there's no need to implement
it at all given that sizeof is already part of the language.

Jeremy.
 

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

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top