![]() |
|
#1
|
|||
|
|||
An interesting question about CONSTCPP / C++ / C Code:
Why the result is "125 256" ? Last edited by LuciWiz : 06-Apr-2007 at 10:43.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: An interesting question about CONSTQuote:
Note to moderator: I posted a response to this on the C board (before I saw it here), I have subsequently pasted the response here and deleted the substance of the discussion from the C board. Note to Poster: Why would you post the same question to the C board and the C++ board? That's not likely to be more productive. I understand why a Poster might ask the question on two (or more) different forum sites, but not to two different sections of the same site. Quote:
My guess would be that the compiler "hard-coded" the value of i into the code. That is: since i is declared to be constant, its value is known at compile time, so the print statement would actually pass "125" to the printf function, and not the current value of the contents of that memory location. By using the const_cast<>, you made the compiler accept what it would not normally allow. C++ won't let you convert a non-void pointer to a non-void pointer of any type without some kind of cast, and this is converting a const pointer to int to a non-const pointer to int. But even though you think you fooled it, it still won't generate code that allows you to assign a new value to a declared constant. The value of i will always be 125 wherever it is in scope in this program. Since j was declared as pointer to int (non-constant), the code allows the contents of the location pointed to by j to be changed. Note that this is undefined behavior, and the results are not always guaranteed to be what you observe. From the C++ standard: "Creating a new object at the storage location that a const object with static or automatic storage duration occupies ... results in undefined behavior." Whenever I see any kind of cast in a program, I wonder about two things: 1. Is it is really necessary? Sometimes people use casts explicitly in places where they aren't needed, just because they don't understand (much) about data types and language-defined conversions. I look very carefully at their data types and conversions between them. 2. Why is it used? In cases like this, someone is trying to do something that is "illegal". If the only way that the programmer can get the program to compile is by using a cast, is it really trying to do something that is questionable? Or: Maybe they haven't included some header file that would have defined a prototype function. Using a cast might eliminate the pesky warning message (or error message), but this will not necessarily guarantee proper linkage to the function. There are certainly places in programs where casts are required to get proper behavior as well as to get the blooming compiler to accept the code. This ain't one of them (since the attempted behavior is, in fact, undefined). The language lets you do lots of things that result in undefined behavior. That doesn't mean that the language should be changed; it means that you really have to know what you are doing and you can't depend on the language to defend itself against user stupidity (or maliciousness). On the other hand, it makes a reasonably provocative homework problem, right? I agree with your title: It is an interesting question. Regards, Dave Last edited by davekw7x : 06-Apr-2007 at 10:56.
|
Recent GIDBlog
2nd Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Morse Code Program Using Switch Statement | glacier | CPP / C++ Forum | 10 | 24-Sep-2006 22:31 |
| Handling keyup events. | harroldm | FLTK Forum | 3 | 24-Aug-2006 08:48 |
| need help with a console menu system | BullBuchanan | CPP / C++ Forum | 6 | 20-Aug-2006 14:46 |
| help me to do this program!!! | mrdell_06 | CPP / C++ Forum | 3 | 21-Feb-2006 21:12 |
| Error - unresolved external symbol _WinMain | ap6118 | CPP / C++ Forum | 6 | 23-Mar-2005 22:46 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The