LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2014, 12:35 PM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870

> 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.
 
Old 08-26-2014, 01:28 PM   #17
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by NevemTeve View Post
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 View Post
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”.
 
Old 08-26-2014, 01:34 PM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
There's no constants in C, only variables with 'const' modifier. Pascal, for example, does have constants.
 
Old 08-26-2014, 05:15 PM   #19
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by psionl0 View Post
That looks like a compiler bug to me.
It is a bug… in the code.

Quote:
Originally Posted by psionl0 View Post
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 View Post
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.
 
Old 08-26-2014, 11:55 PM   #20
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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'

Last edited by psionl0; 08-27-2014 at 12:11 AM.
 
Old 08-27-2014, 12:03 AM   #21
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
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 learning about 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.
 
Old 08-27-2014, 12:10 AM   #22
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Just forget the 'inlined variable', and try 'const volatile'. When it is done, remember never doing this again.

(You may have heard Ted Ts'o saying: "...handing C++ to the average programmer seems roughly comparable to handing a loaded .45 to a chimpanzee.")
 
Old 08-27-2014, 12:14 AM   #23
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
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.



In c and C++,there is difference

here is my testing in c:



#include <stdio.h>

const int j=100;
int main(int argc, char* argv[])
{


const int i=10;
int *pi=(int *)&i;


int *pj=(int *)&j;
*pi = 111;

printf("%d\n",i);
printf("%d\n",*pi);
printf("%d",j);


printf("%d",*pj);

return 0;
}



the result is

111
111
100
100

when i try to do this "*pj=10000",the program is terminated

but in c++, the "i" doesn't change !


I think it is a kind of compiler optimization of C++
 
Old 08-27-2014, 12:20 AM   #24
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
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.

Last edited by NevemTeve; 08-27-2014 at 12:33 AM.
 
Old 08-27-2014, 12:31 AM   #25
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by dy20082250 View Post
In c and C++,there is difference
I wasn't aware that C recognized the 'const' modifier but I have been out of date on developments in the language before.
 
Old 08-27-2014, 01:19 AM   #26
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 08-27-2014, 01:22 AM   #27
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by dy20082250 View Post
but in c++, the "i" doesn't change !

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!
 
1 members found this post helpful.
Old 08-27-2014, 07:28 AM   #28
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by psionl0 View Post
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 View Post
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 View Post
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 View Post
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.
But of course, you should almost never use volatile. Definitely not in any user-space programs.

Last edited by mina86; 08-27-2014 at 07:34 AM.
 
Old 08-27-2014, 07:45 AM   #29
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
An example for volatile usage in user-land program (own experience):

Code:
static int debug= 0;
...
if (debug) {
    fprintf ("This line won't ever be printed, even if you do 'set var debug= 1' in gdb\n");
}
If no part of the source modifies 'debug', compiler will know that debug is alway zero, so setting it from debugger results nothing!

Solution: static volatile int debug= 0;

Last edited by NevemTeve; 08-27-2014 at 07:47 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: "const" Keyword Explained LXer Syndicated Linux News 0 02-07-2009 01:02 AM
c++ problem setting "const char *str" from function returning a string ocularb0b Programming 3 03-04-2008 09:36 PM
any difference between "const myClass &obj" and "myClass const &obj"? parv Programming 9 01-09-2008 08:58 AM
"int const" working problem. gpagedar Linux - General 0 10-08-2004 01:04 AM
Are there "const correctness" rules for Java with final? johnMG Programming 1 07-11-2004 10:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration