On Tue, 13 Apr 2004, Shachar Shemesh wrote:
[...]
> >are declared both static and const. Is the static necessary in that
> >context?
> >
> The static is even more necessary in that context. Outside of functions,
> all static does is limit the scope. Inside functions, static prevents
> the var from taking stack space.


I wrote a teeny test app to check this out and you're right. The static
is crucial. If it's not specified the string is copied to the stack so
that the const essentially has no effect whatsoever (no compiler warning
and no runtime error). With 'static const' we still don't get a compiler
warning (tested with gcc version 3.3.3 (Debian 20040321)) but we get a
runtime crash, i.e. the string is really const this time.


#include <stdio.h>

void foo()
{
    /*static*/ /*const*/ char buf[]={'a',':','b',':','c',':','d',0};
    char* c;

    c=strchr(buf,':');
    if (c)
        *c=' ';
    printf("buf=%s\n",buf);
}

int main()
{
    foo();
    foo();
    foo();
    return 0;
}


-- 
Francois Gouget         [EMAIL PROTECTED]        http://fgouget.free.fr/
                     Linux: the choice of a GNU generation

Reply via email to