This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C++ const vs #define - How to avoid memory allocation for constants

In my experience in C++ I have seen it said many times that:

1) C constants are not true constants but C++ constants are (can't use them as size of an array etc.)

2) In C++ const int is preferable to #define for its type safety, scoping and other benefits.

I also thought that I had seen in my previous work that const int data or static const int class data would be "compiled out", especially when using optimization settings, so that it would not use memory to allocate those constants (unless possibly the address of the variable was used).

Recently I am trying to demonstrate this as we move to C++ and I have not found a way to achieve this. It seems that all variables that are switched from #define to const int seem to consume memory. I have tried changing the optimization settings on the files where they are used and I have not seen any change in behavior.

Is there a way to avoid memory allocation for these? It seems unnecessary..

Thanks
Chris

  • Are you using "static const"?  Could we see an example where you expect the compiler to get rid of the actual variable?

  • In our existing project I tried using "const int" to replace #defines in global and namespace scope. I found that memory was consumed when this change was made no matter which file I made the change in or which #define I changed.
    Note: since C++ implicitly makes const variables outside of classes static it doesn't matter if I use the static keyword. I did try it though and it made no difference. 

    I have, since creating this post, found that changing "static const int" class members to #define DOES NOT CHANGE MEMORY USAGE. So I have concluded that the static const int members are being "compiled out". I thought that I had seen this in the past but I could not remember where or how.

    What I am asking is:

    1) Does anyone have any experience with when this occurs and when it does not (optimization options or anything like that).

    2) Can a TI employee tell me what is actually being done by the compiler so I know what to expect and how it decides to do what it does.

    I am working with some weary C-programmers and they don't want to see memory increase if we use these scoped variables to replace #defines in the C++ code.

    Thanks

    Chris

  • c0l0jar0 said:
    2) Can a TI employee tell me what is actually being done by the compiler so I know what to expect and how it decides to do what it does.

    You can see what the compiler does by generating the assembly code for a module and looking for a definition of a .const variable with the name of your constant; if that exists, the compiler has consumed memory for it.  There's more to it than that; the compiler does some tricky things, and the C++ standard has some weird requirements, so I'd have to see an actual example to tell you what the compiler is actually doing.

    c0l0jar0 said:
    I am working with some weary C-programmers and they don't want to see memory increase if we use these scoped variables to replace #defines in the C++ code.

    If all else fails, you can still use #define, it's just a style issue.