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

Strange casting problem w/ GCC 3.3

Hi all,

I just upgrade my workstation to Gcc 3.3, and I got a very strange
probleme with casting : I got an error if I try to get the adress of a
"casted" pointer.
---- dumy example source code ------

class tata {
int a;
};

int main(int, char **){
void *toto;
tata **tst_ptr = &((tata *)toto);
}

-------------------------------

[faillie]/home/faillie/src $ g++ tst.cxx
tst.cxx: In function `int main(int, char**)':
tst.cxx:7: error: non-lvalue in unary `&'

I duno if it's normal or not (I don't see why the compiler think this
casted stuff is not a "lvalue"), but do you know how to compile such
thing ?

If it's a GCC bug, I'm very bad as it's the only version I have on my
HPUX workstation :-(

Thanks for your help

Laurent
Jul 19 '05 #1
4 2889
sb******@cs.uic.edu (Shane Beasley) wrote in message news:<2f*************************@posting.google.c om>...
Write the result of the conversion into an own variable and take a
pointer to that, and try to avoid old-style casts:

void *toto;
tata *converted_toto = reinterpret_cast<tata *>(toto);
tata **tst_ptr = &converted_toto;


Alternatively, you could use a reference, which is an lvalue and thus
has an address you can take:

void *toto;
tata **tst_ptr = &reinterpret_cast<tata *&>(toto);


Hmm... My code was meant to do what the OP asked to do, which is not
the same as what the code above does. Oops. :)

However, since conversion from tata * to void * is implicit, the
reverse can (and should) be done with static_cast, not
reinterpret_cast:

tata *converted_toto = static_cast<tata *>(toto);

I would agree that this is the sanest route, all things considered.

- Shane
Jul 19 '05 #2
Rolf Magnus <ra******@t-online.de> wrote in message news:<bd*************@news.t-online.com>...

Hello,
casting toto into a tata* creates a nameless temporary (the "casted
stuff") that contains the converted value. And this temporary is not an
lvalue. You cannot take the address of it.


For me it's a "surprise" for me, because doing such cast, I said to
the compiler "ok, even if toto is a void *, please treat it as a tata
*" ... as it done w/ normal C or w/ older version of the compiler. If
I want realy a modification of my object, I said dynamic_cast<> or
something like that.

Thanks for your light and code.

Bye

Laurent
Jul 19 '05 #3
Rolf Magnus <ra******@t-online.de> wrote in message news:<bd*************@news.t-online.com>...
destroyedlolo wrote:
Hi all,

I just upgrade my workstation to Gcc 3.3, and I got a very strange
probleme with casting : I got an error if I try to get the adress of a
"casted" pointer.

---- dumy example source code ------

class tata {
int a;
};

int main(int, char **){
void *toto;
tata **tst_ptr = &((tata *)toto);
}

-------------------------------
casting toto into a tata* creates a nameless temporary (the "casted
stuff") that contains the converted value. And this temporary is not an
lvalue. You cannot take the address of it.
but do you know how to compile such thing?


Write the result of the conversion into an own variable and take a
pointer to that, and try to avoid old-style casts:

void *toto;
tata *converted_toto = reinterpret_cast<tata *>(toto);
tata **tst_ptr = &converted_toto;


That compiles but won't work as expected. You'd want
(*tst_ptr) = new tata;
to set the void* to the new tata. After all, tst_ptr should
point to toto.

Regards,
--
Michiel Salters
Jul 19 '05 #4
destroyedlolo wrote:
Rolf Magnus <ra******@t-online.de> wrote in message
news:<bd*************@news.t-online.com>...

Hello,
casting toto into a tata* creates a nameless temporary (the "casted
stuff") that contains the converted value. And this temporary is not
an lvalue. You cannot take the address of it.
For me it's a "surprise" for me, because doing such cast, I said to
the compiler "ok, even if toto is a void *, please treat it as a tata
*"


That's not what a cast is doing. A cast converts the specified value to
the destination type and returns the result of that conversion.
... as it done w/ normal C or w/ older version of the compiler.


Which compiler? Btw, "normal C" behaves the same as C++ wrt this.

Jul 19 '05 #5

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

Similar topics

2
by: J-P | last post by:
Hi all, I'm getting a strange result with the following IF statement: $bar = ($foo == 'last') ? true : false; In my script $foo normaly has a integer value greater than 0, but can have last...
24
by: David | last post by:
hello. when doing the simple following computation, the value put into the variable numMinusOne is NOT the same as what the computation is showed to be in the Watch window!! here is the code:...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
13
by: Patricia | last post by:
First, I know the following code is bad, but it's from a library I have to use, and I can't change it. class A { // some primitive members }; class B { // some primitive members };
3
by: Sameh Halabi | last post by:
Hi I have a strange problem in C , I hope someone may help with it . the problem is as follows : I have an existing big structure (that I can't change) which contains in it groups of...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
6
by: Andrew Ducker | last post by:
I have a class "Validator" which can be cast to a Control. The code: ValidTextBox t = (ValidTextBox)v; works just fine. However, because v doesn't descend from t, I can't use "is" or "as". I...
10
by: vigacmoe | last post by:
I was trying to cast some strings to integers with stringstream, then this strange problem poped up. Here is my test code: stringstream conv; string from; int to; from = "1"; conv << from;
18
by: seenutn | last post by:
Hi All, I found a piece of code in linux/kernel/signal.c: int send_sig(int sig, struct task_struct *p, int priv) { return send_sig_info(sig, (void*)(long)(priv != 0), p); }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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: 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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.