gcc: What's illegal about this ? | | |
I need a clue. I'm getting an "invalid lvalue in increment" from gcc
4.1.2 on this line...
sum += *((unsigned short *)iphp)++ ;
....but I don't see what the problem is. gcc 3.4.3 isn't bother by
it, either.
Machine specifics:
$ uname -a
Linux tmxnw-fc6 2.6.22.9-61.fc6 #1 SMP Thu Sep 27 18:48:03 EDT 2007
i686 i686 i386 GNU/Linux
$ gcc --version
gcc (GCC) 4.1.2 20070626 (Red Hat 4.1.2-13) | | | | re: gcc: What's illegal about this ?
On Fri, 14 Mar 2008 17:49:16 +0000, Richard Eich wrote: Quote:
I need a clue. I'm getting an "invalid lvalue in increment" from gcc
4.1.2 on this line...
>
sum += *((unsigned short *)iphp)++ ;
>
...but I don't see what the problem is. gcc 3.4.3 isn't bother by it,
either.
(unsigned short *)iphp is not an lvalue. Where do you want to store
(unsigned short *)iphp + 1? Presumably, you want to store it in iphp. If
you want that, write that.
sum += *((unsigned short *)iphp);
iphp = ((unsigned short *)iphp) + 1;
You can consider it similar to
int i;
(i + 1) = -1;
(double) i = 1.5; | | | | re: gcc: What's illegal about this ?
Richard Eich <richard.eich@domain.invalidwrites: Quote:
I need a clue. I'm getting an "invalid lvalue in increment" from gcc
4.1.2 on this line...
>
sum += *((unsigned short *)iphp)++ ;
The part that GCC is complaining about is this:
((unsigned short *)iphp)++
which attempts to increment the result of a cast. The result of
a cast is never an lvalue, so you can't increment it. (GCC used
to have an extension that permits this, but it was removed.)
Perhaps you really mean:
sum += (*((unsigned short *)iphp))++ ;
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}} | | | | re: gcc: What's illegal about this ?
On Mar 14, 12:49 pm, Richard Eich <richard.e...@domain.invalidwrote: Quote:
I need a clue. I'm getting an "invalid lvalue in increment" from gcc
4.1.2 on this line...
>
sum += *((unsigned short *)iphp)++ ;
>
...but I don't see what the problem is. gcc 3.4.3 isn't bother by
it, either.
Your code is invalid, result of a cast is not an lvalue
(and hence not something you can apply ++ to). Quoting http://gcc.gnu.org/gcc-3.4/changes.html
"""
The cast-as-lvalue extension has been removed for C++ and
deprecated for C and Objective-C. In particular, code like this:
int i;
(char) i = 5;
or this:
char *p;
((int *) p)++;
is no longer accepted for C++ and will not be accepted for C
and Objective-C in a future version.
"""
And "future version" was 4.something.
Yevgen | | | | re: gcc: What's illegal about this ?
Richard Eich <richard.eich@domain.invalidwrites: Quote:
>I need a clue. I'm getting an "invalid lvalue in increment" from gcc
>4.1.2 on this line...
Quote:
> sum += *((unsigned short *)iphp)++ ;
Quote:
>...but I don't see what the problem is. gcc 3.4.3 isn't bother by
>it, either.
I think it would help if you could give us just a bit of context, at least.
--
Aaron Hsu <arcfide@sacrideo.us| Jabber: arcfide@jabber.org
``Government is the great fiction through which everybody endeavors to
live at the expense of everybody else.'' - Frederic Bastiat | | | | re: gcc: What's illegal about this ?
In article <MPG.224484a7110ac91798b037@news.verizon.net>,
Richard Eich <richard.eich@domain.invalidwrote: Quote:
>I need a clue. I'm getting an "invalid lvalue in increment" from gcc
>4.1.2 on this line...
>
> sum += *((unsigned short *)iphp)++ ;
>
>...but I don't see what the problem is. gcc 3.4.3 isn't bother by
>it, either.
It's an extension in earlier versions of gcc, was accepted by
some other C compilers before the ANSI standard, and was allowed in
early drafts of the ANSI standard, but it not allowed now.
-- Richard
--
:wq | | | | re: gcc: What's illegal about this ?
Aaron Hsu wrote: Quote:
Richard Eich <richard.eich@domain.invalidwrites:
> Quote:
>I need a clue. I'm getting an "invalid lvalue in increment"
>from gcc 4.1.2 on this line...
> Quote:
> sum += *((unsigned short *)iphp)++ ;
> Quote:
>...but I don't see what the problem is. gcc 3.4.3 isn't bother
>by it, either.
>
I think it would help if you could give us just a bit of context,
at least.
In this case at least, why? That line of code is obviously wrong.
Evem before the very queasy integer to pointer cast.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com | | | | re: gcc: What's illegal about this ?
On Mar 14, 4:48 pm, CBFalconer <cbfalco...@yahoo.comwrote: Quote:
Aaron Hsu wrote: Quote:
Richard Eich <richard.e...@domain.invalidwrites:
> Quote: Quote:
I need a clue. I'm getting an "invalid lvalue in increment"
from gcc 4.1.2 on this line...
> Quote: Quote:
sum += *((unsigned short *)iphp)++ ;
> Quote: Quote:
...but I don't see what the problem is. gcc 3.4.3 isn't bother
by it, either.
> Quote:
I think it would help if you could give us just a bit of context,
at least.
>
In this case at least, why? That line of code is obviously wrong.
Evem before the very queasy integer to pointer cast.
So context indeed would be helpful, because there is no
integer to pointer cast ;)
Yevgen | | | | re: gcc: What's illegal about this ?
In article <frf1qq$1of2$1@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <richard@cogsci.ed.ac.ukwrote: Quote:
>In article <MPG.224484a7110ac91798b037@news.verizon.net>,
>Richard Eich <richard.eich@domain.invalidwrote: Quote:
>>I need a clue. I'm getting an "invalid lvalue in increment" from gcc
>>4.1.2 on this line...
>>
>> sum += *((unsigned short *)iphp)++ ;
>>
>>...but I don't see what the problem is. gcc 3.4.3 isn't bother by
>>it, either.
>
>It's an extension in earlier versions of gcc, was accepted by
>some other C compilers before the ANSI standard, and was allowed in
>early drafts of the ANSI standard, but it not allowed now.
>
>-- Richard
>--
>:wq
What did it do?
As has been pointed out, it doesn't make any sense, so I'm having a hard
time imagining what it did in prior versions of gcc? | | | | re: gcc: What's illegal about this ?
On Sat, 15 Mar 2008 15:23:06 +0000, Kenny McCormack wrote: Quote:
In article <frf1qq$1of2$1@pc-news.cogsci.ed.ac.uk>, Richard Tobin
<richard@cogsci.ed.ac.ukwrote: Quote:
>>In article <MPG.224484a7110ac91798b037@news.verizon.net>, Richard Eich
>><richard.eich@domain.invalidwrote: Quote:
>>>I need a clue. I'm getting an "invalid lvalue in increment" from gcc
>>>4.1.2 on this line...
>>>
>>> sum += *((unsigned short *)iphp)++ ;
>>>
>>>...but I don't see what the problem is. gcc 3.4.3 isn't bother by it,
>>>either.
>>
>>It's an extension in earlier versions of gcc, was accepted by some other
>>C compilers before the ANSI standard, and was allowed in early drafts of
>>the ANSI standard, but it not allowed now.
>
What did it do?
>
As has been pointed out, it doesn't make any sense, so I'm having a hard
time imagining what it did in prior versions of gcc?
In those versions, (ptrtype) ptrlvalue was treated in some (but not all)
contexts as *(ptrtype *) &ptrlvalue, except without aliasing problems (if
those versions understood aliasing at all). | | | | re: gcc: What's illegal about this ?
In article <e518f$47dbebd1$541dfcd3$16772@cache6.tilbu1.nb.ho me.nl>,
Harald van Dijk <truedfx@gmail.comwrote: Quote: Quote:
>As has been pointed out, it doesn't make any sense, so I'm having a hard
>time imagining what it did in prior versions of gcc?
Quote:
>In those versions, (ptrtype) ptrlvalue was treated in some (but not all)
>contexts as *(ptrtype *) &ptrlvalue, except without aliasing problems (if
>those versions understood aliasing at all).
Though it's similar in effect to that expression, I would have hoped
it didn't rely on the pointer types having to the same representation,
which that does.
I would expect *(ptrtype *)ptrlvalue++ to be interpreted as
*(ptrtype *)ptrlvalue for the purpose of retrieving the value,
and as ptrlvalue = (type_of_ptrlvalue)(((ptrtype *)ptrlvalue) + 1)
for the side effect.
-- Richard
--
:wq | | | | re: gcc: What's illegal about this ?
On Sat, 15 Mar 2008 16:53:35 +0000, Richard Tobin wrote: Quote:
In article <e518f$47dbebd1$541dfcd3$16772@cache6.tilbu1.nb.ho me.nl>,
Harald van Dijk <truedfx@gmail.comwrote: Quote: Quote:
>>As has been pointed out, it doesn't make any sense, so I'm having a
>>hard time imagining what it did in prior versions of gcc?
> Quote:
>>In those versions, (ptrtype) ptrlvalue was treated in some (but not all)
>>contexts as *(ptrtype *) &ptrlvalue, except without aliasing problems
>>(if those versions understood aliasing at all).
>
Though it's similar in effect to that expression, I would have hoped it
didn't rely on the pointer types having to the same representation,
which that does.
Correct. I don't believe gcc has been ported to systems where different
pointer types have different representations (though I would be pleased
if proved wrong on this). | | | | re: gcc: What's illegal about this ? ymuntyan@gmail.com wrote: Quote:
CBFalconer <cbfalco...@yahoo.comwrote: Quote:
>Aaron Hsu wrote: Quote:
>>Richard Eich <richard.e...@domain.invalidwrites:
>> Quote:
>>>I need a clue. I'm getting an "invalid lvalue in increment"
>>>from gcc 4.1.2 on this line...
>>>>
>>> sum += *((unsigned short *)iphp)++ ;
>>>>
>>>...but I don't see what the problem is. gcc 3.4.3 isn't bother
>>>by it, either.
>>>
>>I think it would help if you could give us just a bit of context,
>>at least.
>>
>In this case at least, why? That line of code is obviously wrong.
>Evem before the very queasy integer to pointer cast.
>
So context indeed would be helpful, because there is no
integer to pointer cast ;)
iphp is cast to a pointer. Besides which the ++ operates on the
cast value, which is illegal. If iphp isn't integral things are
even worse.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com | | | | re: gcc: What's illegal about this ?
On Mar 15, 6:30 pm, CBFalconer <cbfalco...@yahoo.comwrote: Quote:
ymunt...@gmail.com wrote: Quote:
CBFalconer <cbfalco...@yahoo.comwrote: Quote:
Aaron Hsu wrote:
>Richard Eich <richard.e...@domain.invalidwrites:
> Quote: Quote:
>>I need a clue. I'm getting an "invalid lvalue in increment"
>>from gcc 4.1.2 on this line...
> Quote: Quote:
>> sum += *((unsigned short *)iphp)++ ;
> Quote: Quote:
>>...but I don't see what the problem is. gcc 3.4.3 isn't bother
>>by it, either.
> Quote: Quote:
>I think it would help if you could give us just a bit of context,
>at least.
> Quote: Quote:
In this case at least, why? That line of code is obviously wrong.
Evem before the very queasy integer to pointer cast.
> Quote:
So context indeed would be helpful, because there is no
integer to pointer cast ;)
>
iphp is cast to a pointer.
It is, but it's a pointer from the very beginning.
char *iphp; // guessed
sum += *((unsigned short *)iphp)++ ; Quote:
Besides which the ++ operates on the
cast value, which is illegal.
Yep. Quote:
If iphp isn't integral things are
even worse.
Not really.
Yevgen | | | | re: gcc: What's illegal about this ? ymuntyan@gmail.com wrote: Quote:
CBFalconer <cbfalco...@yahoo.comwrote: Quote:
>ymunt...@gmail.com wrote: Quote:
>>CBFalconer <cbfalco...@yahoo.comwrote:
>>>Aaron Hsu wrote:
>>>>Richard Eich <richard.e...@domain.invalidwrites:
>>>>>
>>>>>I need a clue. I'm getting an "invalid lvalue in increment"
>>>>>from gcc 4.1.2 on this line...
>>>>>>
>>>>> sum += *((unsigned short *)iphp)++ ;
>>>>>>
>>>>>...but I don't see what the problem is. gcc 3.4.3 isn't
>>>>>bother by it, either.
>>>>>
>>>>I think it would help if you could give us just a bit of
>>>>context, at least.
>>>>
>>>In this case at least, why? That line of code is obviously
>>>wrong. Evem before the very queasy integer to pointer cast.
>>>
>>So context indeed would be helpful, because there is no
>>integer to pointer cast ;)
>>
>iphp is cast to a pointer.
>
It is, but it's a pointer from the very beginning.
So what? The only conversion available is to a void*, and from a
void* back to the original form. All others are illegal. Quote:
>
char *iphp; // guessed
sum += *((unsigned short *)iphp)++ ;
> Quote:
>Besides which the ++ operates on the cast value, which is illegal.
>
Yep.
> Quote:
>If iphp isn't integral things are even worse.
>
Not really.
Yes, really. Read the C standard.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com | | | | re: gcc: What's illegal about this ?
On Mar 16, 5:25 pm, CBFalconer <cbfalco...@yahoo.comwrote: Quote:
ymunt...@gmail.com wrote: Quote:
CBFalconer <cbfalco...@yahoo.comwrote: Quote:
ymunt...@gmail.com wrote:
>CBFalconer <cbfalco...@yahoo.comwrote:
>>Aaron Hsu wrote:
>>>Richard Eich <richard.e...@domain.invalidwrites:
> Quote: Quote:
>>>>I need a clue. I'm getting an "invalid lvalue in increment"
>>>>from gcc 4.1.2 on this line...
> Quote: Quote:
>>>> sum += *((unsigned short *)iphp)++ ;
> Quote: Quote:
>>>>...but I don't see what the problem is. gcc 3.4.3 isn't
>>>>bother by it, either.
> Quote: Quote:
>>>I think it would help if you could give us just a bit of
>>>context, at least.
> Quote: Quote:
>>In this case at least, why? That line of code is obviously
>>wrong. Evem before the very queasy integer to pointer cast.
> Quote: Quote:
>So context indeed would be helpful, because there is no
>integer to pointer cast ;)
> Quote: Quote:
iphp is cast to a pointer.
> Quote:
It is, but it's a pointer from the very beginning.
>
So what? The only conversion available is to a void*, and from a
void* back to the original form. All others are illegal.
As people around here say, Wrong. Quote: Quote:
char *iphp; // guessed
sum += *((unsigned short *)iphp)++ ;
> Quote: Quote:
Besides which the ++ operates on the cast value, which is illegal.
> > Quote: Quote:
If iphp isn't integral things are even worse.
> >
Yes, really. Read the C standard.
As far as the standard is concerned, both cases would
be wrong. So if you were to compare them, then it's
not using the standard but using the meaning of
((type-name)expression)++ in given implementation (GNU
C). And in that case you don't care whether something
was an integer or a pointer, both can be converted to
each other. Not that it makes sense to say which is
worse or better of course.
Yevgen | | | | re: gcc: What's illegal about this ? arcfide@sacrideo.us wrote... Quote:
Richard Eich <richard.eich@domain.invalidwrites:
> Quote:
I need a clue. I'm getting an "invalid lvalue in increment" from gcc
4.1.2 on this line...
> Quote:
sum += *((unsigned short *)iphp)++ ;
> Quote:
...but I don't see what the problem is. gcc 3.4.3 isn't bother by
it, either.
>
I think it would help if you could give us just a bit of context, at least.
Of course. Sorry.
iphp is a pointer to an IPv4 header. Specifically, it holds the
address of a populated IPv4 header ('iph' below).
sum is a long. It holds checksum calculation for the IPv4 header.
....
struct ip_hdr * iphp = &iph ;
int i, hlc = sizeof(iph) ;
long sum ;
for ( i = 0, sum = 0; hlc 1; i++ )
{
sum += *((unsigned short *)iphp)++ ;
if ( sum & 0x80000000 )
sum = (sum & 0xFFFF) + (sum >>16) ;
hlc -= 2 ;
}
iph.ip_sum = htons( (short)~sum ) ;
....
I've been out sick, and am just now reading the responses to this
thread (which I thank the responders for).
Seems like the problem is the cast. I'm sure at least one of the
responses explains why. To me, it's done to make sure I'm adding to
sum only short-sized values and I'm utterly clueless as to why the
compiler wouldn't allow it.
Let me get through the responses though. | | | | re: gcc: What's illegal about this ?
Richard Eich wrote: .... snip ... Quote:
>
Seems like the problem is the cast. I'm sure at least one of the
responses explains why. To me, it's done to make sure I'm adding
to sum only short-sized values and I'm utterly clueless as to why
the compiler wouldn't allow it.
Because the result of the cast is an expression, not an object.
There is no place to keep the incremented value of that expression.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com | | | | re: gcc: What's illegal about this ?
Richard Eich <richard.eich@domain.invalidwrites: Quote: arcfide@sacrideo.us wrote... Quote:
>Richard Eich <richard.eich@domain.invalidwrites:
>> Quote:
>I need a clue. I'm getting an "invalid lvalue in increment" from gcc
>4.1.2 on this line...
>> Quote:
> sum += *((unsigned short *)iphp)++ ;
>> Quote:
>...but I don't see what the problem is. gcc 3.4.3 isn't bother by
>it, either.
>>
>I think it would help if you could give us just a bit of context, at least.
>
Of course. Sorry.
>
iphp is a pointer to an IPv4 header. Specifically, it holds the
address of a populated IPv4 header ('iph' below).
>
sum is a long. It holds checksum calculation for the IPv4 header.
>
...
struct ip_hdr * iphp = &iph ;
There is really no need use this hack here. If it is safe and correct
to access iph in "short" sized chunks, then just convert the pointer
once rather than every time:
unsigned short *iphp = (unsigned short)&iph; Quote:
int i, hlc = sizeof(iph) ;
long sum ;
>
for ( i = 0, sum = 0; hlc 1; i++ )
{
sum += *((unsigned short *)iphp)++ ;
No cast now needed, just 'sum += *iphp++;'
--
Ben. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|