DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2007

    Ruined Reputation: static const int...

    I put my rep on the line was completely embarrassed.
    First, I used a static const int in a function to size an array:
    int function()
        static const int SIZE(10);
        char buf[SIZE];
    This works on some versions of g++, but a guy running g++ (version 4. something) on a Lynux box had a error for it. The error indicated that char buf was being sized with a non-constant integral expression.
    The compiler, for some reason, was not able to determine that SIZE should be replaced with 10.

    Furthermore, C++ style teaches to use "static const int..." instead of #define. At least I was taught that.

    I thought that not only did this enforce type safety, but any good compiler would optimize it away and you wouldn't waste space.

    A guy here told me he put a "static const int ..." in a .h file and a separate copy of the const was created in each compilation unit that included the .h.

    I think these are compiler errors. What say ye?

  2. #2
    Join Date
    Nov 2003
    Actually, your reputation is untarnished -- something I can't say about your compiler, though.
    The code is fine and should compile under any C++ compiler, including a C++98 compiler. I know that GNU have had some bugs with static const data, and I guess this bug is probably discussed on one of their forums (I'll have to check it) but for the time being, you can rest assure that this is perfectly valid C++ code. Try to replace the parentheses with a = sign. Perhaps this will convince the compiler that SIZE is a constant expression...
    Last edited by Danny; 07-15-2009 at 05:10 PM.
    Danny Kalev

  3. #3
    Join Date
    Oct 2007
    Actually, I should have put in the initial post that when I substituted = for the constructor form for initializing the constant, the Compiler was happy.

    On the second issue, I found out that the guy used "no optimization" when compiling. No wonder the objects were not optimized away.

  4. #4
    Join Date
    Nov 2003
    Even if the object weren't optimized away, they still make a const expression so the code should compiler in either case (i.e., optimized and unoptimized).
    Danny Kalev

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++
    Replies: 1
    Last Post: 02-15-2010, 10:21 AM
  2. Screen Capture Bug
    By Ranger in forum VB Classic
    Replies: 3
    Last Post: 05-29-2006, 09:43 PM
  3. KeyListener not responding
    By lordanki in forum Java
    Replies: 3
    Last Post: 04-04-2006, 10:13 AM
  4. Screen Capture Bug
    By Ranger in forum VB Classic
    Replies: 0
    Last Post: 12-09-2000, 12:21 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.