473,807 Members | 2,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on const

Hello,

Here is the program

#include stdio

int main(void)
{
const int num = 100;
int *ip;

ip = (int *)#
*ip = 200;
printf("value of num is %d(%d) \n", num, *ip);
}
Output:
value of num is 100(200)

The output says that *ip is changed and 'num' is unchanged. How is this
possible when both of them point to the same memory location? My wild guess
says that this trick is handled at the compiler level. Am I correct?

Even when the memory location is accessable and the contents changed the
'const integer' is unaffected.

Thanks
Nov 14 '05
83 3077
begin tinybyte:
The compiler is allowed to assume that the value it's not changed,
but sometimes it is changed, sometimes not, depends on optimization.
It is not changed by magic but by your request.
Using an explicit cast. You asked for trouble, you got trouble.
If you fix this line

ip = (int *) #

to

ip = #

you should get a diagnostic similar to

warning: assignment discards qualifiers from pointer target type

If you change the declaration to

const int *ip;

then the diagnostic will change to

warning: assignment of read-only location
This is inconsistent behavior for me, and in C programming this
should not be ALLOWED.
C gives you enough rope to hang yourself.
And then a couple of more feet, just to be sure.
Always has been, always will be.
[...] A value that cannot be changed has been changed, under
certain conditions. That means something is broken.


There are indeed languages out there that protect the code from
hostile programmers, e.g. Smalltalk, Java, C#, Python, Ruby.
If you want that, go for it.

--
Für Google, Tux und GPL!
Nov 14 '05 #41
tinybyte wrote:
On Thu, 08 Jan 2004 14:20:15 +0000, Alex wrote:
Are you trying to say that all possible undefined behaviour
must be detected at compile time, resulting in an error? If
not, what are you trying to say?


Yes, they should be detected, and result in an error.


#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
int a, b, c;

a = INT_MAX;
b = rand();
(void)printf("% d\n", c = a + b);
return 0;
}

has a fairly high probability of generating undefined behavior.
Yet very few systems will complain at run-time whether or not UB
occurs, and none that I know of will complain at compile time.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #42
*** Please do NOT top-post - corrected ***
Jean-Michel Collard wrote:
user wrote:
Here is the program

#include stdio

int main(void)
{
const int num = 100;
int *ip;

ip = (int *)&num;
*ip = 200;
printf("value of num is %d(%d) \n", num, *ip);
}
Output:
value of num is 100(200)
.... snip ...
It seems to be compiler dependent and even changes with optimisations.

<jm@paris>13:42 :33~$gcc -v
Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.0/specs
Configured with: ../gcc/configure --prefix=/usr --enable-shared
--enable-static --enable-debug --enable-profile --verbose --enable-interpreter
--enable-haifa --enable-long-long --enable-languages=c,c++ --with-system-zlib
--enable-__cxa_atexit --enable-threads=posix
Thread model: posix
gcc version 3.4.0

<jm@paris>13:43 :51~$gcc ess.c
<jm@paris>13:44 :26~$./a.out
value of num is 200(200)

<jm@paris>13:44 :28~$gcc -Wall -W -O2 ess.c
ess.c: In function `main':
ess.c:11: warning: control reaches end of non-void function
<jm@paris>13:44 :41~$./a.out
value of num is 100(200)


With the silly top-posting fixed this may make some sense.

Something is seriously wrong with your gcc installation:

c:\c\junk>gcc -W -Wall -ansi -pedantic -O1 junk.c
junk.c:1:10: #include expects "FILENAME" or <FILENAME>
junk.c: In function `main':
junk.c:9: warning: implicit declaration of function `printf'
junk.c:10: warning: control reaches end of non-void function

After correcting the source and removing the foolish cast:

#include <stdio.h> /* corrected */
int main(void)
{
const int num = 100;
int *ip;

ip = &num; /* silly cast removed */
*ip = 200;
printf("value of num is %d(%d) \n", num, *ip);
return 0; /* line added */
}

c:\c\junk>gcc -W -Wall -ansi -pedantic -O1 junk.c
junk.c: In function `main':
junk.c:7: warning: assignment discards qualifiers from pointer
target type

Moral: CASTS ARE EVIL.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #43
tinybyte wrote:

On Thu, 08 Jan 2004 14:20:15 +0000, Alex wrote:
Are you trying to say that all possible undefined behaviour must be detected
at compile time, resulting in an error? If not, what are you trying to say?


Yes, they should be detected, and result in an error.

Then it wouldn't be undefined behavior.

Brian Rodenborn
Nov 14 '05 #44
On Thu, 08 Jan 2004 17:36:30 +0100, Alexander Bartolich wrote:
begin tinybyte:
It is not changed by magic but by your request.
Using an explicit cast. You asked for trouble, you got trouble.
I asked for that, I've got it, I can manage it, now I'm happy :)
C gives you enough rope to hang yourself.
And then a couple of more feet, just to be sure.
Always has been, always will be.
Nowadays too many programmers hangs themselves using C.
Knowing every inch of that rope is the only way to stay away from
hanging...
There are indeed languages out there that protect the code from
hostile programmers, e.g. Smalltalk, Java, C#, Python, Ruby.
If you want that, go for it.


I don't want that. I will keep programming in C for my whole life, because
I love it. Knowing many languages is indeed useful, even if you actually
program only in C. I don't want another language, I want the C language
the best it can be. This is the whole point of my polemic

....

Am I too much polemic? ;)

Bye
Daniele
Nov 14 '05 #45
The original post is missing on my server, so I am replying to
a followup.
int main(void)
{
const int num = 100;
int *ip;

ip = (int *)&num;
*ip = 200;
The effect at this point is officially undefined by the C standard,
so *anything* *could* happen. What likely *does* happen is just
what is shown below:
printf("value of num is %d(%d) \n", num, *ip);
}

In article <3F************ ***@france-paris.org>
Jean-Michel Collard <jm@france-paris.org> writes:It seems to be compiler dependent and even changes with optimisations. [without optimization]value of num is 200(200) [with optimization]value of num is 100(200)


This is highly expect-able. :-)

First, remember that the "const" keyword in C is peculiarly named,
like so many C keywords: "struct" means "define a type"; "typedef"
means "do NOT define a new type"; "static" has nothing to do with
electricity :-) ; and "const" means "read-only".

Thus:

const int num = 100;

means: "Allocate a plain old ordinary variable that could change
like any plain old ordinary variable, but please Mr Compiler, if
you are able and willing, please put it into memory that is in fact
read-only. Oh and by the way I also promise never, ever to change
this variable -- you may count on me sir, I never make misteaks!" (sic)

Now, as it happens, gcc is not able or willing to place this variable
in read-only memory (because gcc assumes there is a conventional
stack, and puts all its automatic variables on that stack, and does
not try to protect any sub-parts of it against writing). If you
compile without optimization, gcc also forgets about your promise
not to change the variable.

On the other hand, if you turn on optimization, gcc remembers your
promise. You promised the variable "num" would always be 100 --
so the printf() call can substitute in the promised value for the
first %d.

The pointer "ip", of course, points to the actual variable (which
the compiler did not place in read-only memory after all) that
was initially 100. You achieved this by using a cast to tell the
compiler: "Hey Mr Compiler, I am smarter than you: this pointer
value from &num, that has type `pointer to read-only int', really
points to a read/write int. So even though this assignment is
suspicious and dodgy and you would normally tell me I am doing
something foolish, I invoke the All-Powerful-Cast syntax to shut
you up!" And -- because gcc was unwilling or unable to put "num"
in really, truly read-only memory, this, too, is actually correct.
The variable "num" *is* an ordinary int variable when gcc is done
compiling, because -- despite your asking gcc to please put it in
read-only memory -- it went into read/write memory.

Then, when you said "*ip = 200;" you told the compiler to change
the value of the memory from its original 100 to the new 200.
The compiler no doubt generated code to do this, and since "num"
was -- despite your request -- in read/write memory, the assignment
changed it.

Thus, you broke your promise never to change "num".

You lied to the compiler. It got its revenge, by producing different
code under optimization (where it assumed you kept your promise)
than without optimization (where it did not).

Note that it requires a pointer cast to achieve this lie. Avoid
pointer casts and you will avoid many mistakes. This is part of
the reason comp.lang.c wisdom says "do not cast malloc()".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #46
On Thu, 08 Jan 2004 18:55:08 +0000, Chris Torek wrote:
The original post is missing on my server, so I am replying to
a followup. [snip] Note that it requires a pointer cast to achieve this lie. Avoid
pointer casts and you will avoid many mistakes. This is part of
the reason comp.lang.c wisdom says "do not cast malloc()".


Ah, finally an exaustive explanation that isn't "according to the standard
there is no reason to speak about it" ... thanks a real lot Chris!
If we ever met in the future, remember that I owe you a beer! :D

Bye,
Daniele "tinybyte" Milan
Nov 14 '05 #47
[Cross-posted and followups set to gnu.gcc.help.]

[Note: Discussion arose from a question about strange output when a
const object is modified through a pointer with the const attribute cast
away. The OP observed that the same address appeared to contain 2
different values, depending on whether it was accessed by the const
object name or by dereferencing the pointer - typical newbie stuff
dealing with undefined behavior. Others observed that gcc doesn't seem
to do the optimization that causes the apparently strange result. OP's
code (slightly adapted) appears in my example session below.]

Dik T. Winter wrote:
In article <3f************ ***@news.indivi dual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
> No, the compiler used by the OP is quite as logical as gcc, and possibly
> better at optimising - it seems to have diked out all references to the
> const int and replaced them with the constant value, which is both a
> sensible optimisation, and potentially quite a valuable one.


What gcc probably does do (and the warning message you get suggests it)
is discarding the word const from the declaration of num. Normally gcc
performs the above optimisation.


gcc doesn't even seem to warn about it with -W -Wall. It looks like
-Wcast-qual (which is one of several additional warning options I
usually enable) enables the warning. But I don't think this warning has
anything to do with the results.

gcc *should* do that optimization, if you turn optimizations on. And it
does... but I found something strange:

$ cat fun.c
#include <stdio.h>

int main(void)
{
const int num = 100;
int *ip;

ip = (int *)&num;
*ip = 200;
printf("value of num is %d(%d) \n", num, *ip);

return 0;
}
$ gcc fun.c -o fun.exe
$ ./fun
value of num is 200(200)
$ gcc -O fun.c -o fun.exe
$ ./fun
value of num is 100(200)
$ gcc -pedantic -O fun.c -o fun.exe
$ ./fun
value of num is 200(200)
Note that I compiled 3 different ways: first, with no special options,
second with basic optimizations turned on, and finally with basic
optimizations and -pedantic turned on. With optimizations (and no
-pedantic) it did the expected optimization. For some reason, turning on
-pedantic changed the results. I checked the docs and this doesn't look
right. -pedantic should only affect what diagnostics are issued, I think.

If -pedantic reduces optimization effectiveness, I think a lot of us are
going to have to change the way we use gcc.

For reference, here's my version info:

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.3.1/specs
Configured with: /GCC/gcc-3.3.1-3/configure --with-gcc --with-gnu-ld
--with-gnu-as --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc
--libdir=/usr/lib --libexecdir=/usr/sbin --mandir=/usr/share/man
--infodir=/usr/share/info
--enable-languages=c,ada ,c++,f77,pascal ,java,objc --enable-libgcj
--enable-threads=posix --with-system-zlib --enable-nls
--without-included-gettext --enable-interpreter --enable-sjlj-exceptions
--disable-version-specific-runtime-libs --enable-shared
--disable-win32-registry --enable-java-gc=boehm
--disable-hash-synchronization --verbose --target=i686-pc-cygwin
--host=i686-pc-cygwin --build=i686-pc-cygwin
Thread model: posix
gcc version 3.3.1 (cygming special)

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #48
tinybyte <ne******@box.i t> spoke thus:
If we ever met in the future, remember that I owe you a beer! :D


There are a number of people here I owe beers to ;D

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #49
tinybyte wrote:
On Thu, 08 Jan 2004 10:39:30 +0000, Kevin Goodsell wrote:

These last two statements invoke undefined behavior. %p is for void
pointers only. It's also a little silly to add your own 0x prefix. On

I admit it's silly :)
Undefined behavior? then cast that pointers to void and you're done.

...
printf ("address of ip is %p\n", (void *)ip);
printf ("address of num is %p\n", (void *)&num);
...


That is the correct way to handle it, but you might want to cast to
const void * in the latter case, otherwise you may elicit a warning from
the compiler, since num is a const object.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3695
by: Jessica | last post by:
Hi, I have a design question. I am making a time series analysis tool. Since I already use STL vector to represent time series, is there a need to implement a class for the time series object? Right now I just use typedef vector<double>TS; Is that enough? I feel that I am not taking advantage of the C++
7
5077
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone offered me a kind explanation. TIA Function template and specialization below works fine: template <typename T> T maximum (T a, T b) {
2
6384
by: fb | last post by:
Hi everyone. I have the following code. It was a question out of C++ how to program. I get a warning about "possible unreachable code" in the RunCode() function, but I don't see any problem with it... I was hoping for a better way of exiting the case statements below. I used exit() for "divide by zero" and "invalid instruction". I can't get a try/catch to work. Probably from a lack of experience. I have a fairly strong C...
2
2184
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const Debug_Level ddl;
2
1938
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix. I have a problem, however, with /calling/ foo. If I call foo as foo(a,m,n), my compiler (gcc) complains about:
4
1748
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good design to have large objects with several has-a relationship with other objects. Second, I want my unit to have a coord struct. struct coord{ int x;
14
1348
by: streamkid | last post by:
i'm a learning newbie at c++... and i have the following question... reading some source code, i saw this: int function(const void * one, const void * two) { int var1, var2; var1 = *((int*)one); var2 = *((int*)two); /* sm other code here*/ }
8
2241
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address, port number, or both. How should we design the classes to represent these filters? My answer was: class FilterRule {
14
2125
by: Alexander Dong Back Kim | last post by:
Dear all, I used to use C++ programming language at all time but moved to C# and Java. Few days ago, I restarted studying about C++ with a very beginner's mind. I wrote a simple class and gcc couldn't compile the class. Any hints that I'm missing? Header File: #ifndef __Calc_h__
8
1672
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n), and the information to assign (a const pointer to a class object Vector<TYPE>. According to the arrow below, I put a const keyword in the function. To my knowledge, this means that all private variables in this object
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.