November 13, 2005

How const is const?

Here is some straightforward C++ code:

int a[5];

The variable "a" is declared as being an array of 5 integers. This code is equivalent:

const int n = 5;
int a[n];

The variable "n" is a compile-time constant and can be used interchangeably with the constant 5. The address of a global variable is also a compile-time constant. Therefore, this is valid C++ as well:

int b;
const int n = reinterpret_cast<int>(&b);

And it does indeed compile, at least under Visual C++ 2005. The variable "n" no longer has the value 5. It has the value of the address of "b" reinterpreted as an integer.

Now the interesting part (to me anyway!). What if I combine the two examples so that the constant "n" is initialized with the address of "b" and then "n" is used as the size of the array "a"?

int b;
const int n = reinterpret_cast<int>(&b);
int a[n];

Although "n" has the same datatype as it did previously (constant integer), this code results in a compile time error under Visual C++. I can understand why this is the case. Consider:

// foo.cpp
extern int b = 0;

// bar.cpp
extern int b;
const int n = reinterpret_cast<int>(&b);
int a[n];

The compiler has no way of knowing the value of "n" at compile time. Only the linker has sufficient information to determine its value. But this is too late because the compiler is responsible for allocating the storage for the array "a".

My conclusion is that the type of "n" seems to go beyond its declared type of constant integer. Behind the scenes, the compiler must track whether "n" can be resolved at compile time or not. Sort of like, is "n" constant or really constant.

The morale of the story? Never fool yourself into thinking you fully understand C++!

Comments:
Hi,
I’m going to ask a non-computer question if i may.. Simple maths i think :-)
The family tree has grown significantly, Alastair. If i ever thought, though, that one day i'd have all our forebears nicely charted.. right down to their favourite breakfast, i was very wrong. Every discovery produces a new set of parents and of course, all the siblings! Mathematical Progression has nothing on Ancestry!
One individual may have only 2 biological parents, but go back 4 generations and you have 32 biological ggggrandparents. Simple mathematics you'd say. Well, yes, on the face of it, YES! But, each generation is producing a reverse multiplication of its own. So, as the numbers of parents increase going back through the generations, so the numbers increase coming down again with all the marrying, breeding and births and.... so on and so forth! :-) and you know me, i like to get them all ... don't want anyone feeling left out ;-)
Now, my question is: is there a mathematical term for this kind of explosion in numbers? ... Increasing in both directions at once.. or near enough .. although not necessarily in equal proportion!
xxxx
:-)
PlatMum
 
here;'s a good one as a consolation prize when you can't see the wood for the trees:
"The blindingly obvious is seldom immediately apparent"

maybe it could apply to compilation hiccups too :-)

Platinum Mum
 
Good one keep uploading such type of things will return to read your blog...
 
This is a mega event don't miss it. CodeFest @ ITBHU - Register now at http://www.itbhu.ac.in/codefest
Win exiting prizes.
you can contact me personally @ vipul.neo@gmail.com
 
please see my blog and giev me suggestion for blogging

It's useful

Http://codeanswer.blogspot.com
http://codeanswer.wordpress.com
 
Really Nice Information Provided and its helpful.

http://www.drcsystems.com/
http://www.drcsystems.com/software_development_company_india.php
 
For programmers, using the initializer list functionality that C++ offers is very important for better performance.
 
I am enyoing reading your blog! Hope you will continue your steps on your new blog!

http://androidprogrammingblog.blogspot.com
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?