473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Doubt regarding const......

Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.
Nov 14 '05 #1
7 1617

"Rajat" <my**********@yahoo.com> wrote in message
news:b4**************************@posting.google.c om...
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

C89: 3.5.3 -

If an attempt is made to modify an object defined with a
const-qualified type through use of an lvalue with non-const-qualified
type, the behavior is *undefined*.

Undefined means its not defined as to what the output could be. Could be
anything.
On my box i get c=4 *p=4

- Ravi
I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.

Nov 14 '05 #2
Rajat wrote:
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.

The error is to declare a pointer to a non-const data.

Using the lcc-win32 compiler you get the warning:

Warning tconst.c: 4 Different const qualifiers

The compiler warns you about this. Maybe you should also get this
warning using gcc if you increase the warning level.

jacob
Nov 14 '05 #3
"Ravi Uday" <ra******@gmail.com> wrote in message news:<1097474590.442588@sj-nntpcache-3>...
"Rajat" <my**********@yahoo.com> wrote in message
news:b4**************************@posting.google.c om...
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}


C89: 3.5.3 -

If an attempt is made to modify an object defined with a
const-qualified type through use of an lvalue with non-const-qualified
type, the behavior is *undefined*.

Undefined means its not defined as to what the output could be. Could be
anything.
On my box i get c=4 *p=4

- Ravi
I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.


Even I have met with this problem and there were times in which I got
out put c=16 and *p=4 and there were times I got output c=4 and *p=4.
I have tried this one with Microsoft C++ compiler.

Its a defect in the compiler which will supress the usage of
const_cast in c++ ( consct_cast in C++ is used to remove the
constaness of a variable)
But Even I am not sure of the behiour of compiler on this.
Nov 14 '05 #4


Rajat wrote:
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.


As someone else said, this is undefined behaviour, so you're lucky you
didn't get monkeys flying out of your nose and other interesting results
of such practices.

Tried it myself but I got 4 for both c and *p, using MinGW 3.2.3. Best
bet if you want to work out what it's doing is to generate the assembler
(gcc -S) and look at that. My guess is that space for c is generated,
then the value 16 optimised into a register, or simply replaced
throughout the function by the literal value 16, either of which would
be reasonable interpretations of the code. Either way c would then
appear to contain one of two values depending how you reference it.

Dave.
Nov 14 '05 #5
Rajat wrote:

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I
think it should give error while changing the read only memory
content i.e. c, or if it allow changes then it should reflect
for bot c and *p.


The following shows the result after cleaning up the source and
making it legal by adding the #include statement. On my system
"gcc" exercises gcc through an alias, which supplies the parameters
to make it into a standard C compiler with a respectable warning
level. That includes -W -Wall -ansi -pedantic.

Without that #include the source invokes undefined behaviour, so
you have no reason to complain.

c:\c\junk>gcc junk.c
junk.c: In function `main':
junk.c:5: warning: initialization discards qualifiers from pointer
target type

c:\c\junk>a
c=4, *p=4
c:\c\junk>type junk.c
#include <stdio.h>
int main(void)
{
const int c = 16;
int *p = &c;

*p = 4;
printf("c=%d, *p=%d", c, *p);
return 0;
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
In article <b4**************************@posting.google.com >,
Rajat <my**********@yahoo.com> wrote:
Hi all,

See the below code. Can u tell me what will be the output and why??

int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}


If you're going to lie to the compiler, why should you trust anything
it says to you?

Gary
--
Any attempt to brew coffee with a teapot should result in the error code
"418 I'm a teapot". The resulting entity body MAY be short and stout.
-- RFC 2324, Hyper Text Coffee Pot Control Protocol (HTCPCP)/1.0
Nov 14 '05 #7
my**********@yahoo.com (Rajat) wrote in message news:<b4**************************@posting.google. com>...

See the below code. Can u tell me what will be the output
No.
and why??
Because the code causes undefined behaviour.
int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it
should give error while changing the read only memory content i.e. c,
or if it allow changes then it should reflect for bot c and *p.


I'm very surprised that gcc didn't give a warning for this while
compiling. Even the default warning levels (which are way too low)
give me a warning with gcc. It's a good idea to pay attention to
warnings, and ask for as many as the compiler will give you.
Nov 14 '05 #8

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

Similar topics

1
by: SK | last post by:
Hi all, I have a doubt in C++ Templates by Nicolai M. Josuttis. On Page 17 there is a line "In general, it is a good idea not to change more than necessary when overloading function templates....
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
3
by: darkstorm | last post by:
Consider this: template<typename T_fp>class TRigidBody : public BaseObject { public:
9
by: kiran.agashe | last post by:
Hi, Please refer program below: #include <string> #include <cstdio> using namespace std; const char* f(); main() {
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
5
by: VSP | last post by:
Hi, I have a doubt regarding using references. Please look at the below code. I am using VC++ 6.0 int &i = 10; // Compilation error error C2440: 'initializing' : cannot convert from...
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
5
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.