Results 1 to 6 of 6

Thread: [C++] Memory/executable size question

  1. #1
    Join Date
    Aug 2012
    Beans
    185

    [C++] Memory/executable size question

    Hi,
    I have a "enums.h" file with common utility enums and consts declared like:

    Code:
    enum ViewMode {
        VIEW_MODE_NONE = 0,
        VIEW_MODE_LIST = 1,
        VIEW_MODE_GRID = 2
    };
    Including it in a .cpp file makes the compilation unit larger (and hence the final executable and memory size) only by the enums/consts actually used in the .cpp file or by all the enums/consts defined in the "enums.h"?

  2. #2
    Join Date
    Sep 2009
    Beans
    207
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [C++] Memory/executable size question

    Generally speaking the compiler is smart enough to know that you didn't reference the variable anywhere in the .cpp and the memory usage of your application should not be any larger. The binary will slightly larger as that code will be in there.

  3. #3
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: [C++] Memory/executable size question

    I could be wrong, but if I understand the C++ compile process correctly, the enum won't make any difference whatsoever unless you have debugging symbols set. The C preprocessor will just substitute instances of the enum to its appropriate value, e.g.:

    Code:
    int i = VIEW_MODE_NONE;
    would become:

    Code:
    int i = 0;

  4. #4
    Join Date
    Feb 2009
    Beans
    1,469

    Re: [C++] Memory/executable size question

    I think enums are type-checked, so it's not done by the preprocessor. But you're right that it should make no difference whatsoever to the generated code. Enumeration constants are just that, constants -- they don't incur any overhead over an integer literal.

  5. #5
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: [C++] Memory/executable size question

    Quote Originally Posted by trent.josephsen View Post
    I think enums are type-checked, so it's not done by the preprocessor.
    Huh. I thought it was just a neat way of #define 'ing multiple constants. Thanks for the hint!

  6. #6
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: [C++] Memory/executable size question

    it is correct that enums are not preprocessed, they only represent integers which are (in x86 at least) directly representable in machine code, so no explicit load from memory is required and they should not increase the executable size by much (depending on the size of the number)

    e.g.:
    this:
    Code:
    enum {
        VAR = 431 
    } my_enum;
    
    int f(int a)
    {
        if (a == VAR)
            return 1;
        return 0;
    }
    essentially results in this:
    Code:
    81 ff af 01 00 00    	cmp    $0x1af,%edi
    Last edited by MadCow108; March 14th, 2013 at 12:52 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •