[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: RFS: ustr (updated package)



Vaclav Ovsik <vaclav.ovsik@i.cz> writes:

> I have setup gcc-4.3 from experimental on a sid xen guest.
> The warning is emitted for example on following (simplified) code:
>
>     extern inline char func( int arg )
>     {
>       static const char foomap[4] = {2, 4, 8, 16};
>
>       return foomap[arg & 3];
>     }

 Yes, from the link Bernd provided the warning is emitted basically
for anything that uses the static keyword. The above is correct
though, IMO, as is:

     extern inline char func( int arg )
     {
       static const char foomap[] = "abc";

       return foomap[arg & 3];
     }

> I have red several times the ISO paragraph :). It seems to me, that
> paragraph applies to this situation.
> IMHO foomap is `reference to an identifier with internal linkage'.
> Or no? :)

 From ISO 9899:1999 6.2.2 Linkages of identifiers:

 #1
An identifier declared in different scopes or in the same scope more
than once can be made to refer to the same object or function by a
process called linkage. There are three kinds of linkage: external,
internal, and none.

 #2
If the declaration of a file scope identifier for an object or a
function contains the storage-class specifier static, the identifier
has internal linkage.
[...]
 #6
The following identifiers have no linkage: an identifier declared to
be anything other than an object or a function; an identifier declared
to be a function parameter; a block scope identifier for an object
declared without the storage-class specifier extern.

...from that #2 doesn't apply because the identifier isn't at file
scope, and so the last part of #6 does apply.

 Also it's "well known" that in:

void foobar(void)
{
  const char *const foo   = "abcd";
  static const char bar[] = "abcd";
}

...foo and bar do the same thing, point to some constant data, but bar
is the better version.
 As with foo you have a allocated two objects (the pointer being the
second object), and have to use/manage them both, but with bar you
have just "named" a single object.

-- 
James Antill -- james@and.org
C String APIs use too much memory? ustr: length, ref count, size and
read-only/fixed. Ave. 44% overhead over strdup(), for 0-20B strings
http://www.and.org/ustr/



Reply to: