473,322 Members | 1,614 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,322 software developers and data experts.

conversion from const type* to type* allowed?

The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Jan 4 '07 #1
13 1455
Michal Nazarewicz wrote:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
Const is broken in C, that's why one shouldn't ignore compiler warnings!

--
Ian Collins.
Jan 4 '07 #2
Michal Nazarewicz wrote On 01/04/07 15:55,:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
From back to front: It is not allowed, GCC is not buggy
(not in this regard, anyway), and you are in fact missing
something. What you're missing is that the C Standard makes
no distinction between error messages, warning messages,
informative messages, and spirit messages from the Great
Beyond: They are all merely "diagnostics." Once the compiler
has emitted a "diagnostic" for the erroneous construct, it
is under no obligation to halt compilation or reject the
program[*], it may (if it feels like it) emit the diagnostic
and translate the program anyhow.

There's a GCC option ("-Werror"?) that turns "warnings"
into "fatal errors" if you prefer.
[*] Exception: The compiler is required to reject a
module containing a non-suppressed #error directive.
--
Er*********@sun.com
Jan 4 '07 #3
Michal Nazarewicz <mi****@tlen.plwrites:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
The implicit conversion is a constraint violation. A conforming
compiler's only obligation in the presence of a constraint violation
is to issue a diagnostic. gcc meets this requirement by printing a
warning.

The only case where a compiler is actually required to reject a
translation unit is when it contains a "#error" directive.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 4 '07 #4
On Thu, 04 Jan 2007 21:55:28 +0100, Michal Nazarewicz <mi****@tlen.pl>
wrote:
>The following code compiles fine (with warnings though)
If it had warnings, it did not compile fine. Just remember that the
compiler is actually too dumb to know the difference between a serious
error and a trivial one, though it like to pretend it does.

Strive to have all your programs compile cleanly without errors or
warnings. There are cases where warnings can be ignored*, but only if
you understand exactly what caused them and know for a fact that it
won't be a problem.

*One which comes to mind is an HP-UX warning that is issued whenever
you use "const". It just tells you that using const improperly is bad,
without knowing whether your usage was bad or not <g>.
under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
--
Al Balmer
Sun City, AZ
Jan 4 '07 #5
Al Balmer said:

<snip>
Strive to have all your programs compile cleanly without errors or
warnings. There are cases where warnings can be ignored*, but only if
you understand exactly what caused them and know for a fact that it
won't be a problem.
Perhaps "overruled" would be a better word than "ignored". When gcc tells me
that I've only partly initialised an aggregate object - e.g. mystruct s =
{0}; - I take due note of the warning, and check that the initialisation is
correct. Actually, this is such a frequent occurrence that I suppose
"ignored" is pretty accurate in that case, so let me find a different
example (taken from real code this time):

Dest->n[i] = in & 0xff;

Dest->n[i] has type unsigned char. in is a long int (which, incidentally, is
known to be non-negative by this stage in the proceedings). By the rules of
C, in & 0xff must must must must must be in the range of unsigned char, so
I know for sure that it will fit into Dest->n[i] - and yet one of my
compilers issues a diagnostic message saying that information might be lost
in this assignment. It can't possibly be - but the compiler isn't quite
bright enough to work this out, so it warns me.

And it's a reasonable warning, on the whole. So I let the compiler warn me,
and I look at the warning, and I check that it really is okay. If it is, I
may add a comment to the code to save me a little time next time through,
but by paying attention to the warning at least the first time it appears,
I have given myself the opportunity to check out a potential bug.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 4 '07 #6
On Thu, 04 Jan 2007 21:57:24 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>Al Balmer said:

<snip>
>Strive to have all your programs compile cleanly without errors or
warnings. There are cases where warnings can be ignored*, but only if
you understand exactly what caused them and know for a fact that it
won't be a problem.

Perhaps "overruled" would be a better word than "ignored".
I agree. I'll remember that the next time I give that speech :-)

--
Al Balmer
Sun City, AZ
Jan 4 '07 #7
Eric Sosman wrote:
Michal Nazarewicz wrote On 01/04/07 15:55,:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:
<snip>
... The compiler is required to reject a
module containing a non-suppressed #error directive.
That is true of C99, but rejection under #error is not a requirement of
C90
implementations. However, the C90 implementation the OP is using will.

--
Peter

Jan 5 '07 #8
Al Balmer <al******@att.netwrites:
On Thu, 04 Jan 2007 21:55:28 +0100, Michal Nazarewicz <mi****@tlen.pl>
wrote:
>>The following code compiles fine (with warnings though)

If it had warnings, it did not compile fine.
True and I always check all the warnings compiler gives me but at this
point I was shocked that compiler generated an executable where
implicit conversion from pointer to const type to pointer to type was
done.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Jan 5 '07 #9
Eric Sosman <Er*********@sun.comwrites:
Michal Nazarewicz wrote On 01/04/07 15:55,:
>The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?

From back to front: It is not allowed, GCC is not buggy
(not in this regard, anyway), and you are in fact missing
something. What you're missing is that the C Standard makes
no distinction between error messages, warning messages,
informative messages, and spirit messages from the Great
Beyond: They are all merely "diagnostics." Once the compiler
has emitted a "diagnostic" for the erroneous construct, it
is under no obligation to halt compilation or reject the
program[*], it may (if it feels like it) emit the diagnostic
and translate the program anyhow.
Thanks for explanation, I wasn't aware of such behaviour even though
it sounds rather strange to me.
There's a GCC option ("-Werror"?) that turns "warnings"
into "fatal errors" if you prefer.
That'd be too much. I use a -Wpadded switch which produces warnings
that some of them are to be ignored.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Jan 5 '07 #10
On Thu, 04 Jan 2007 21:57:24 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>Al Balmer said:

<snip>
>Strive to have all your programs compile cleanly without errors or
warnings. There are cases where warnings can be ignored*, but only if
you understand exactly what caused them and know for a fact that it
won't be a problem.

Perhaps "overruled" would be a better word than "ignored". When gcc tells me
that I've only partly initialised an aggregate object - e.g. mystruct s =
{0}; - I take due note of the warning, and check that the initialisation is
correct. Actually, this is such a frequent occurrence that I suppose
"ignored" is pretty accurate in that case, so let me find a different
example (taken from real code this time):

Dest->n[i] = in & 0xff;

Dest->n[i] has type unsigned char. in is a long int (which, incidentally, is
known to be non-negative by this stage in the proceedings). By the rules of
C, in & 0xff must must must must must be in the range of unsigned char, so
I know for sure that it will fit into Dest->n[i] - and yet one of my
compilers issues a diagnostic message saying that information might be lost
in this assignment. It can't possibly be - but the compiler isn't quite
bright enough to work this out, so it warns me.

And it's a reasonable warning, on the whole. So I let the compiler warn me,
and I look at the warning, and I check that it really is okay. If it is, I
may add a comment to the code to save me a little time next time through,
but by paying attention to the warning at least the first time it appears,
I have given myself the opportunity to check out a potential bug.
I'd take charge of the matter and get rid of the compiler warning
altogether. You could get rid of it this way:

Dest->n[i] = (unsigned char)(in & 0xff);

You could also get rid of it using compiler-specific pragmas, for
example:

#ifdef _MSC_VER
#pragma warning(disable:4244)
#endif
Dest->n[i] = in & 0xff;
#ifdef _MSC_VER
#pragma warning(default:4244)
#endif

I'd choose the former, since it is much easier to implement, and I
know what I'm doing, and a cast in this case is worth getting rid of
the bogus compiler warning.

--
jay

Jan 5 '07 #11
Richard Heathfield <rj*@see.sig.invalidwrote:
Perhaps "overruled" would be a better word than "ignored".
int *foo=42;

"Objection, your honor! This construct makes a pointer from an
integer without a cast!"

Sometimes overruling the objections of the General Compiler Council
can be cause for disbarment :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jan 5 '07 #12
jaysome said:

<snip>
I'd take charge of the matter and get rid of the compiler warning
altogether. You could get rid of it this way:

Dest->n[i] = (unsigned char)(in & 0xff);
True, but adding spurious casts to code to silence diagnostic messages does
not add value to the code. It doesn't do anything good, and it doesn't stop
anything bad happening. All it does is turn your eyes off.
You could also get rid of it using compiler-specific pragmas,
....and have gcc play nethack with you. Yeah, right. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 5 '07 #13
Peter Nilsson wrote:
Eric Sosman wrote:
>Michal Nazarewicz wrote On 01/04/07 15:55,:
>>The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:
<snip>
> ... The compiler is required to reject a
module containing a non-suppressed #error directive.

That is true of C99, but rejection under #error is not a requirement of
C90
implementations. However, the C90 implementation the OP is using will.
If C99 were a human child she would be almost eight years
old and partway through the second grade. She would know how
to add and subtract, and would be on the verge of learning
multiplication. She would be able to read. She might be
taking violin lessons (with wide strings, no doubt). She would
be computer-literate. She would be an accepted, albeit junior,
member of society.

Alas! that over this bright beginning should hang the grim
shadow of noxious doom! For though she seems healthy and happy
today, her family is congenitally short-lived: her elder brother
ANSI succumbed before he reached his teens, and a similar fate
awaits poor C99. Another year or two, say the grave and sorrowful
doctors, or three at the most, and >sob< she will be gone.

When the grim day arrives, will it be said that she was never
a "real" child? Will she go to her grave unmourned, unloved,
always passed over in favor of her long-gone brother's ghost?
Don't be so heartless: it is in your power to give her a brief
moment of acknowledgment before she's gone. Make a child happy.

"Be not the first by whom the new is tried,
Nor yet the last to lay the old aside."
-- Alexander Pope

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 5 '07 #14

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

Similar topics

2
by: Tao Lu | last post by:
class A { pub: A(const char*); //1 A(char *); //2 } ; if i do A a("blah"); which constructor should be called?
14
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
15
by: Alexander Stippler | last post by:
hi, the following does not work. I do not understand why, since it works if I replace line marked by (1) with the line below ((2)). Compiling with gcc4.0 results in "error: conversion from...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
3
by: Alexander Stippler | last post by:
Given the following code snippet we get some unexpected behaviour: //-------------------------------------------------------------------- #include <iostream> using namespace std; struct A {...
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
6
by: Rahul | last post by:
Hi Everyone, I have the following code, class B; class A { public : operator B();
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...

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.