By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 2,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

Pointer casting in GCC 4

P: n/a
Why does this code not compile in GCC 4.x:

int
main()
{
int a[100];
void *pa = (void *)a;

((char*)pa) += 2;

return 0;
}

In GCC 3.3 it still worked.

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Arne Schmitz wrote:
Why does this code not compile in GCC 4.x:


Of course I mean g++ 4.x ...

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #2

P: n/a

Arne Schmitz wrote:
Why does this code not compile in GCC 4.x:
And what is the compiler output?
int
main()
{
int a[100];
void *pa = (void *)a;

((char*)pa) += 2;

return 0;
}


Note that the expression

((char*)pa) += 2;

does not produce any effect, since the cast returns an r-value (a
temporary object) which is then added with 2 and destroyed.

If you need to add 2 to pa proper ways of doing so include:

pa = (char*)pa + 2;
((char*&)pa) += 2;
(*(char**)&pa) += 2;

Nov 22 '05 #3

P: n/a
Maxim Yegorushkin wrote:

Arne Schmitz wrote:
Why does this code not compile in GCC 4.x:

Invalid lvalue in assignment.
And what is the compiler output?
Note that the expression

((char*)pa) += 2;

does not produce any effect, since the cast returns an r-value (a
temporary object) which is then added with 2 and destroyed.
That does make sense. Although it is interesting, that g++ 3.3 did not even
warn about this.
If you need to add 2 to pa proper ways of doing so include:

pa = (char*)pa + 2;


No, that also does not work, one has to explicitly convert:

pa = (int *)((char *)pa + 2);

Evil, evil stuff. Thank god this is not my code. :)

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #4

P: n/a

Arne Schmitz wrote:

[]
If you need to add 2 to pa proper ways of doing so include:

pa = (char*)pa + 2;


No, that also does not work, one has to explicitly convert:


If pa is void* this should work.

Nov 22 '05 #5

P: n/a
Arne Schmitz wrote:
Maxim Yegorushkin wrote:
Arne Schmitz wrote:
Why does this code not compile in GCC 4.x:
int main()
{
int a[100];
void *pa = (void *)a;
Useless cast, remove it.
((char*)pa) += 2;
return 0;
} Note that the expression

((char*)pa) += 2;

does not produce any effect, since the cast returns an r-value (a
temporary object) which is then added with 2 and destroyed.


That does make sense. Although it is interesting, that g++ 3.3 did
not even warn about this.


GCC used to have "cast-as-lvalue" as an extension. If you compiled
with warnings on (-Wall -Wextra -ansi -pedantic), it should tell you.
If you need to add 2 to pa proper ways of doing so include:

pa = (char*)pa + 2;


No, that also does not work, one has to explicitly convert:


Explicit conversions aren't necessary when converting to (void *).
pa = (int *)((char *)pa + 2);


This is likely to have alignment problems (if int has 4-byte alignment
then it can't be aligned correctly both before and after this
operation).
((char*&)pa) += 2;
(*(char**)&pa) += 2;


These solutions rely on 'pa' being a 'void *'. If it were an int *,
then we would have undefined behaviour because int* might
be a different size/representation to char* .

Nov 22 '05 #6

P: n/a
Arne Schmitz wrote:
((char*)pa) += 2;

The result of the cast does not yield an lvalue. You
can't apply += to it.

Why not either cast to a char* outright if you're going
to try to do byte math (which isn't safe in general).

You could also try
((char*&)pa) += 2;
Nov 22 '05 #7

P: n/a
Maxim Yegorushkin schrieb:
Arne Schmitz wrote:

[]
> If you need to add 2 to pa proper ways of doing so include:
>
> pa = (char*)pa + 2;


No, that also does not work, one has to explicitly convert:


If pa is void* this should work.


No, it throws an error. ISO C++ forbids implicit void* conversion, right? (I
said gcc, although I meant g++...)

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.