473,402 Members | 2,061 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,402 software developers and data experts.

Pointer casting in GCC 4

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
7 1738
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

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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
26
by: Alfonso Morra | last post by:
Hi, I'm getting a compiler error of "pointer truncation from 'void *' to 'int'" because I'm not explicitly casting from void* to (datatype*). Pointers are stored as ints (is that right?), so as...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
10
by: lovecreatesbeauty | last post by:
Why (type*)pointer isn't equal to *(type**)pointer, In the code snippet, it shows that: (int *) == (int **) , (int *) != (*(int **)) . Does type-casting change the address? or...
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
5
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.