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 3076
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)
Regards,
Jm
user wrote: 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
Thomas Stegen wrote: Frane Roje wrote:
I'm not sure about this but I think that when you write int *p=20; the compiler will handle the memmory allocation of the int which has '20' value.
No, it will not. Unless You make p point somewhere decent first the above is wrong.
int *p=20;
is an initialization, so you can't make p point somewhere decent first.
Also, a cast is required to assign an integer value to a pointer,
and the result might not be defined.
--
pete
tinybyte wrote: On Thu, 08 Jan 2004 12:21:13 +0000, Dik T. Winter wrote:
In what way is that compiler broken and gcc not? A compiler is allowed, when it sees a declaration like const int num = 100; to assume that when you use "num" later in the program, your intention is that the value 100 should be used.
But it returns the same address. That's inconsistent. It should be not permitted if not according to the standard, but it is. This means: compilers implementations are broken, not strictly following the standard.
What is proper execution with a piece of code to which the standard assigns no meaning?
There is no proper execution, now I know.
I hope you understand that the compiler can't be wrong
when there is no proper execution.
--
pete
On Thu, 08 Jan 2004 13:20:48 +0000, pete wrote: I hope you understand that the compiler can't be wrong when there is no proper execution.
Compilers simply must not permit such a thing, if it gives an
undefined behavior.
Daniele
"tinybyte" <ne******@box.i t> wrote in message
news:pa******** *************** *****@box.it... On Thu, 08 Jan 2004 13:20:48 +0000, pete wrote: I hope you understand that the compiler can't be wrong when there is no proper execution.
Compilers simply must not permit such a thing, if it gives an undefined behavior.
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?
In article <pa************ *************** *@box.it> tinybyte <ne******@box.i t> writes: On Thu, 08 Jan 2004 12:21:13 +0000, Dik T. Winter wrote:
In what way is that compiler broken and gcc not? A compiler is allowed, when it sees a declaration like const int num = 100; to assume that when you use "num" later in the program, your intention is that the value 100 should be used.
But it returns the same address. That's inconsistent.
No that is not inconsistent. The address *must* remain the same.
The question is: "must the compiler look at that address to see what
is there?" The answer to that question is *no* if the variable is
declared to be const, because the compiler is allowed to assume that
the value is not changed.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
tinybyte wrote: On Thu, 08 Jan 2004 13:20:48 +0000, pete wrote:
I hope you understand that the compiler can't be wrong when there is no proper execution.
Compilers simply must not permit such a thing, if it gives an undefined behavior.
Is that how you think C is,
or is that your opinion on how you think C should be ?
--
pete
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.
Daniele
On Thu, 08 Jan 2004 14:54:22 +0000, pete wrote: tinybyte wrote: or is that your opinion on how you think C should be ?
Is an opinion on how C compilers should be.
Daniele
On Thu, 08 Jan 2004 14:31:51 +0000, Dik T. Winter wrote: No that is not inconsistent. The address *must* remain the same. The question is: "must the compiler look at that address to see what is there?" The answer to that question is *no* if the variable is declared to be const, because the compiler is allowed to assume that the value is not changed.
The compiler is allowed to assume that the value it's not changed, but
sometimes it is changed, sometimes not, depends on optimization.
This is inconsistent behavior for me, and in C programming this should
not be ALLOWED. That is all I'm talking about.
I'm trying to say that compilers aren't perfect. And that all those
standards everyone here care so much about are not of any help in this case.
A value that cannot be changed has been changed, under certain conditions.
That means something is broken.
Is the same as you have an application that normally gives perfect output,
but by using it in a different way it was written for, you can screw up
the whole thing. You should not be allowed to use it that way.
If you can, there is a bug.
Bye
Daniele This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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++
|
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)
{
|
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...
|
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;
|
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:
| |
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;
|
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*/
}
|
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
{
|
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__
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |