Re: Technical reason why sizeof() is an operator and not a function?



In article <1137503380.079569.41020@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
robert.thorpe@xxxxxxxxxxxx says...
> Gerry Quinn wrote:
> > In article <DNadnUCaEIpbKVbeRVnyrg@xxxxxxxxxxxxxx>, me@xxxxxxxxxxx
> > says...
> > > "Gerry Quinn" <gerryq@xxxxxxxxxxxxxxxxxxx> wrote in message
> > > news:MPG.1e35b6ab4dfab6fd98a9a5@xxxxxxxxxxxxxxxxxxx
> > > > In article <isednWzN_-QSeFfeRVn-vg@xxxxxxxxxxx>,
> > > > gershwin@xxxxxxxxxxxxxxx says...
> > > >
> > > > > Note that it's proper to write sizeof without the parenthesis both when
> > > > > referring to it and when using it on some expression.
> > > > >
> > > > > int* ptr = malloc(100 * sizeof *ptr);
> > > >
> > > > Looks ugly. Why would anyone want to do that?
> > >
> > > It doesn't look ugly to me (although I'd swap the first asterisk and space).
> >
> > That's a C thing.
> >
> > I would write:
> >
> > int* ptr = malloc( 100 * sizeof( int ) );
> >
> > The brackets indicate that sizeof is not an integer,
>
> But how could malloc( 100 * sizeof int ); indicate that sizeof is an
> integer?

The original has an extra asterisk , giving 100 * sizeof * int, which
looks rather like multiplication.

I guess it's a C idiom, though. As somebody pointed out, the use of
the unnatural construction protects against changes in the type of ptr.
But whether that's a win over writing easily readable code is hard to
know.

- Gerry Quinn







.



Relevant Pages

  • Re: Getting to a number from a sequence
    ... the brackets and precedence rules are ... collectively a herring, because the brackets exist only for the purpose ... Gerry Quinn ...
    (comp.programming)
  • Re: #define....
    ... the brackets are not necessary as sizeof is not a ... Uli ... Prev by Date: ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Ellipses to show hesitation
    ... Indeed, he had been christened Baldric, but due to his endless ... mumblings, everyone called him Er-ic. ... - Gerry Quinn ... Prev by Date: ...
    (rec.arts.sf.composition)
  • Re: No Exit & Sartre
    ... > calculator or pencil and paper handy, ... - Gerry Quinn ... Prev by Date: ...
    (rec.arts.sf.composition)
  • Re: Size of any Data Type?
    ... >>> i want to find the size of any data type with out using the sizeof() ... > over-write uninitialized memory, causing a buffer overflow. ... - Gerry Quinn ...
    (comp.programming)