Thread: Static storage duration question.

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    Static storage duration question.

    Hi,
    I'm reading C Primer Plus by Prata, and he mentioned that if you initilize a variable with static storage duration, then you have to use constant values. I don't understand what he means by 'constant values'. Did he mean that I have to declare my variable with the qualifier "const"?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by nimitzhunter View Post
    Hi,
    I'm reading C Primer Plus by Prata, and he mentioned that if you initilize a variable with static storage duration, then you have to use constant values. I don't understand what he means by 'constant values'. Did he mean that I have to declare my variable with the qualifier "const"?
    Actually, there is no such requirement.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nimitzhunter
    I don't understand what he means by 'constant values'. Did he mean that I have to declare my variable with the qualifier "const"?
    No, it means that you should say, use a literal, or an expression that can be resolved as if it were a literal, to initialise an object with static storage duration. It does not mean that all variables with static storage duration must be declared const.

    Quote Originally Posted by Sebastiani
    Actually, there is no such requirement.
    Actually, there is:
    Quote Originally Posted by C99 Section 6.7.8 Paragraph 4
    All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    Actually, there is:
    Heheh. I think this is the second time you've pointed that out to me, too.

    Why then does GCC not complain with this code?

    Code:
    int bar( void )
    {    }
    
    int main( void )
    {
        static int foo = bar( );
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sebastiani
    Why then does GCC not complain with this code?
    But it does...
    Code:
    test.c: In function `main':
    test.c:6: error: initializer element is not constant
    at least for the MinGW port of gcc 3.4.5 with the -pedantic option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    But it does...
    Code:
    test.c: In function `main':
    test.c:6: error: initializer element is not constant
    at least for the MinGW port of gcc 3.4.5 with the -pedantic option.
    I see. Yeah, I haven't got the "pedantic" switch enabled, apparently (fixed). I wonder why it's required, anyway? Related to initialization order not being guaranteed WRT different translation units or some such?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  3. Automatic vs. Static Duration
    By JMB in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 07:16 PM
  4. Simple storage question
    By zipfur in forum C Programming
    Replies: 2
    Last Post: 10-05-2002, 12:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM