Re: Portability regarding sizeof() function
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Mon, 13 Apr 2009 18:59:23 -0400
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.
.
- Follow-Ups:
- Re: Portability regarding sizeof() function
- From: Richard Heathfield
- Re: Portability regarding sizeof() function
- References:
- Portability regarding sizeof() function
- From: Behzad
- Re: Portability regarding sizeof() function
- From: James Kuyper
- Re: Portability regarding sizeof() function
- From: CBFalconer
- Re: Portability regarding sizeof() function
- From: Richard Heathfield
- Portability regarding sizeof() function
- Prev by Date: Strange problem with fmemopen() and fgetwc()
- Next by Date: Re: for loop problem
- Previous by thread: Re: Portability regarding sizeof() function
- Next by thread: Re: Portability regarding sizeof() function
- Index(es):
Relevant Pages
|