DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: const variable

  1. #1
    raid168 Guest

    const variable


    I am working on an embedded project using C++. Since ROM space is limited,
    we try to reduce code size.

    One question was raised that if "const" qualifier should be used at all.
    If it does not occupy code space then there is no problem. But if it does,
    we should avoid it.

    I did some experiments. It appears that if it is declared inside a C++ module
    (*.cpp), it does not occupy any space, but it does not generate symbol either,
    this implies that the const is local to the file.

    If I change this file to C (*.c), then there is a symbol in the final map,
    but it does occupy space.

    Is this documented behavior or just quirk of a certain compiler?

    Thanks

    Raid168

  2. #2
    ralph Guest

    Re: const variable


    "raid168" <raid168@email.com> wrote:
    >
    >I am working on an embedded project using C++. Since ROM space is limited,
    >we try to reduce code size.
    >
    >One question was raised that if "const" qualifier should be used at all.
    > If it does not occupy code space then there is no problem. But if it does,
    >we should avoid it.
    >
    >I did some experiments. It appears that if it is declared inside a C++

    module
    >(*.cpp), it does not occupy any space, but it does not generate symbol either,
    >this implies that the const is local to the file.
    >
    >If I change this file to C (*.c), then there is a symbol in the final map,
    >but it does occupy space.
    >
    >Is this documented behavior or just quirk of a certain compiler?
    >
    >Thanks
    >
    >Raid168


    Documented behavior as per ANSI/ISO C++ standard.


  3. #3
    Danny Kalev Guest

    Re: const variable

    the exact details are implementation defined but note that if you try to
    taken address of a const variable, the compiler must allocate space for
    that variable. Also, if it's a global symbol it may
    occupy space because the linker must resolve references to it in
    different modules. Turn on all optimizations to enable the linker to
    remove the symbol from the executable. As a last resort, use #define
    instead.

    Danny

    raid168 wrote:
    >
    > I am working on an embedded project using C++. Since ROM space is limited,
    > we try to reduce code size.
    >
    > One question was raised that if "const" qualifier should be used at all.
    > If it does not occupy code space then there is no problem. But if it does,
    > we should avoid it.
    >
    > I did some experiments. It appears that if it is declared inside a C++ module
    > (*.cpp), it does not occupy any space, but it does not generate symbol either,
    > this implies that the const is local to the file.
    >
    > If I change this file to C (*.c), then there is a symbol in the final map,
    > but it does occupy space.
    >
    > Is this documented behavior or just quirk of a certain compiler?
    >
    > Thanks
    >
    > Raid168


  4. #4
    ch0rlt0n Guest

    Re: const variable


    "raid168" <raid168@email.com> wrote:
    >
    >I am working on an embedded project using C++. Since ROM space is limited,
    >we try to reduce code size.
    >
    >One question was raised that if "const" qualifier should be used at all.
    > If it does not occupy code space then there is no problem. But if it does,
    >we should avoid it.
    >
    >I did some experiments. It appears that if it is declared inside a C++

    module
    >(*.cpp), it does not occupy any space, but it does not generate symbol either,
    >this implies that the const is local to the file.
    >
    >If I change this file to C (*.c), then there is a symbol in the final map,
    >but it does occupy space.
    >
    >Is this documented behavior or just quirk of a certain compiler?
    >
    >Thanks
    >
    >Raid168


    I recently had a similar issue with this. As far as we could figure, const
    variables defined within a C++ module have static scope by default. This
    means that they can't be used by other modules and if there are no references
    within the file then the compiler has enough info to know not to create a
    symbol.
    In C, consts have extern scope by default and hence it's possible that another
    module might refer to them, so a symbol IS created.

    The thread is on this group somewhere, maybe a week-fortnight ago.

    If anyone knows of a publicly available web resource where this behaviour
    is documented I'd be very grateful.

  5. #5
    ralph Guest

    Re: const variable


    "ch0rlt0n" <ch0rlt0n@btinternet.com> wrote:
    >
    >"raid168" <raid168@email.com> wrote:
    >>
    >>I am working on an embedded project using C++. Since ROM space is limited,
    >>we try to reduce code size.
    >>
    >>One question was raised that if "const" qualifier should be used at all.
    >> If it does not occupy code space then there is no problem. But if it

    does,
    >>we should avoid it.
    >>
    >>I did some experiments. It appears that if it is declared inside a C++

    >module
    >>(*.cpp), it does not occupy any space, but it does not generate symbol

    either,
    >>this implies that the const is local to the file.
    >>
    >>If I change this file to C (*.c), then there is a symbol in the final map,
    >>but it does occupy space.
    >>
    >>Is this documented behavior or just quirk of a certain compiler?
    >>
    >>Thanks
    >>
    >>Raid168

    >
    >I recently had a similar issue with this. As far as we could figure, const
    >variables defined within a C++ module have static scope by default. This
    >means that they can't be used by other modules and if there are no references
    >within the file then the compiler has enough info to know not to create

    a
    >symbol.
    >In C, consts have extern scope by default and hence it's possible that another
    >module might refer to them, so a symbol IS created.
    >
    >The thread is on this group somewhere, maybe a week-fortnight ago.
    >
    >If anyone knows of a publicly available web resource where this behaviour
    >is documented I'd be very grateful.


    http://www.codeguru.com/cpp/tic/tic0091.shtml


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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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