help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Invalid cast


From: Alf P. Steinbach
Subject: Re: Invalid cast
Date: Tue, 06 Feb 2007 17:56:36 +0100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051201 Thunderbird/1.5 Mnenhy/0.7.3.0

* Frederic Mayot:
Hi,
Can someone give me an explanation why the following code produces a
bug. I think it's related to the compiler and that the first line is
not OK. I have no pb with gcc 3.4 but the bug appeared with gcc 4.1.1
A* p = ...;
int l;
(char*&)p += l;
The right way is
p = reinterpret_cast<A*>(reinterpret_cast<char*>(p) + l);

reinterpret_cast, or a C-style cast that resolves to reinterpret_cast, is a sure way to introduce a bug.

Remove the casts (you'll probably have to fix other code) and chances are you're removing the bug too.

Hope this helps.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]