Re: Portability regarding sizeof() function



Richard Heathfield wrote:
CBFalconer said:
James Kuyper wrote:

... snip ...

Appropriate use of sizeof is essential to writing portable code.
Example:
struct tm *t = malloc(sizeof *t);

Would you care to suggest a more portable way of doing that
which doesn't involve use of the sizeof operator?

Inappropriate use of sizeof can be dangerous, but then so can
inappropriate use of just about any other feature of C.

Nit. I don't think that works. When sizeof is executed t has
not yet been defined. I think it awaits the final semi.

How do you reconcile your opinion with what the Standard says in
3.1.2.1 of C89 (or 6.2.1(4) of C99), given that the Standard's
take on the matter appears to be diametrically opposite to yours?

That seems (to me) to define the end of the scope. I am worrying
about the start. I don't see how that can precede reading the
statement involved.

[#4] Every other identifier has scope determined by the
placement of its declaration (in a declarator or type
specifier). If the declarator or type specifier that
declares the identifier appears outside of any block or list
of parameters, the identifier has file scope, which
terminates at the end of the translation unit. If the
declarator or type specifier that declares the identifier
appears inside a block or within the list of parameter
declarations in a function definition, the identifier has
block scope, which terminates at the end of the associated
block. If the declarator or type specifier that declares
the identifier appears within the list of parameter
declarations in a function prototype (not part of a function
definition), the identifier has function prototype scope,
which terminates at the end of the function declarator. If
an identifier designates two different entities in the same
name space, the scopes might overlap. If so, the scope of
one entity (the inner scope) will be a strict subset of the
scope of the other entity (the outer scope). Within the
inner scope, the identifier designates the entity declared
in the inner scope; the entity declared in the outer scope
is hidden (and not visible) within the inner scope.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


.



Relevant Pages

  • Re: types, variable names and fields
    ... No, assuming the same scope, no two typedefs, objects, or functions ... determine what declaration an identifier refers to. ... For structure/union tags and members, ... declarator or type specifier). ...
    (comp.lang.c)
  • Re: Accessing local variable
    ... char * fun ... "4 Every other identifier has scope determined by the placement of its ... declaration (in a declarator or type specifier). ...
    (comp.lang.c)
  • Re: declaration shadows a parameter
    ... be a mistake to do so, but the only who knows that it is a mistake is the one who wrote the code. ... declared before the opening '{' of the scope. ... A label name is the only kind of identifier that has function scope. ... declaration (in a declarator or type specifier). ...
    (comp.lang.c)
  • Re: scratch memory
    ... have function scope. ... "If the declarator or type specifier that declares ... the identifier appears inside a block or within the list ... of parameter declarations in a function definition, ...
    (comp.lang.c)
  • Re: Array definition guarantees
    ... linkage tells you where, ... identifier that is spelled sufficiently similarly. ... I think it is closer to say that scope tells you where the name ... The second glitch in this is the "extern" keyword. ...
    (comp.lang.c)