ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
> Either it should not create a memory location for the constant - meaning that attempting to set a pointer to the address of the constant gives an error message
Only variables of 'register' storage class have no address, so it would be an error if the compiler gave an error
> It creates a memory location to store the value of the constant but any attempt to change the contents of that memory location would result in an error message.
Implementation detail. The compiler could have put the constant into a read-only segment, but it didn't. Maybe another version of it will do so.
Only variables of 'register' storage class have no address, so it would be an error if the compiler gave an error
But it is not a variable, it is a constant.
As min86 pointed out:
Quote:
Originally Posted by mina86
As you can see, the “i” variable is *never* created. The compiler simply knows that “i == 10” therefore whenever “i” is used, it simply passes literal “10”.
Either it should not create a memory location for the constant - meaning that attempting to set a pointer to the address of the constant gives an error message
This makes no sense. Taking address of a const object is valid operation thus compiler must provide a valid code for it.
Quote:
Originally Posted by psionl0
It creates a memory location to store the value of the constant but any attempt to change the contents of that memory location would result in an error message.
It will result in compile-time error unless you explicitly cast to pointer to non-const type, but at that point you have only yourself to blame.
I tested the "explicitly cast to pointer to non-const type" with the following code snippet:
Code:
#include <iostream>
using namespace std;
void changeConstant(int *p) {
*p = 20;
cout << "within function i = " << *p << endl;
}
int main(int argc, char **argv) {
const int i = 10;
cout << "i = " << i << endl;
changeConstant(&i);
cout << "now i = " << i << endl;
}
and got the expected error message: error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
but change the function call to changeConstant((int*)&i);
and suddenly all is forgiven and the program gives the output:
Code:
i = 10
within function i = 20
now i = 10
It shouldn't be so.
ETA Interestingly, the compiler won't allow the following:
Code:
#include <iostream>
using namespace std;
void changeConstant(int &p) {
p = 20;
cout << "within function i = " << p << endl;
}
int main(int argc, char **argv) {
const int i = 10;
cout << "i = " << i << endl;
changeConstant((int)i);
cout << "now i = " << i << endl;
}
error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
Quite honestly, friend, you need to "mash the Pause button" on your premature attempts to write C++, with or without the helpful-advice of your compadre, and spend some time learningabout the language! There are plenty of books, online and otherwise, which talk about the various language features, and you need to stop and read them!
I'm trying to say that in the very nicest possible way, and with the purpose of being helpful. (This is not "RTFM.™") Because, what you're doing right now is banging your head against things that you barely (if at all) understand, when the ability to fully understand them will only take a little more of the time that you are wasting right now. Quit stumbling around in the dark: turn on the lights, get a map, and stop and read it. You have a well-defined goal to reach ("understanding"), and what you're doing right now is a hopelessly-inefficient non-method of getting there.
I know the inline function ,but I really do not know the meanning of "using a variable inlined"
You are right,there is so much konwledge that I need to learn.
Kindly use [code] and [/code] tags, it cannot be that difficult.
Also you could add some explanation to your printf's, eg:
[code]
Code:
printf("after setting i to 20, i=%d, *pi=%d, j=%d\n",i, *pi, j);
[/code]
PS: Yes, it is a kind of optimization: specifying 'const' you give permission the compiler to use it as a constant... It's 'const volatile' that prevents this.
PS: Yes, it is a kind of optimization: specifying 'const' you give permission the compiler to use it as a constant... It's 'const volatile' that prevents this.
you are right. Changing the declaration to const volatile i = 10; leads to the following output:
Code:
i = 10
within function i = 20
now i = 20
So you can override a 'const' variable by casting its address to a 'non-const' pointer. Doesn't seem like a good idea to me.
I think it is a kind of compiler optimization of C++
changing a variable that was declared constant in C++ results in undefined behavior,
this is in the standard
so nothing can happen, or you CPU melts, or anything else
-
please use code tags!
but change the function call to changeConstant((int*)&i);
and suddenly all is forgiven and the program gives the output: […]
It shouldn't be so.
Like I've said. You have yourself to blame. Your code has a bug, not the compiler.
Quote:
Originally Posted by psionl0
ETA Interestingly, the compiler won't allow the following:
Code:
[…]
changeConstant((int)i);
[…]
error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
Because this is not how you cast to a non-const reference. Change to “(int&)” and it'll work.
Quote:
Originally Posted by psionl0
I wasn't aware that C recognized the 'const' modifier but I have been out of date on developments in the language before.
Out of date since 1989? I'm sorry if I sound condescending, but that's an interesting confession from someone who at the same time claims they found a bug in a compiler (or a language).
Quote:
Originally Posted by NevemTeve
PS: Yes, it is a kind of optimization: specifying 'const' you give permission the compiler to use it as a constant... It's 'const volatile' that prevents this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.