Hi,
Is any one knows what's wrong with the following code, I was told that it
will conpile and run but in some point it will crash.
char* c;
long* lg;
c = (char*) lg;
lg = (long*) c;
Ramesh 10 1510
Ramesh Tharma wrote: Is any one knows what's wrong with the following code, I was told that it will conpile and run but in some point it will crash.
char* c; long* lg;
c = (char*) lg; lg = (long*) c;
C++ only guarantees that you can cast between pointers of unrelated
types only to cast it back and have it unchanged. In your case, 'lg'
should survive the "round-trip" to 'c' and back. This type of cast
is equivalent to 'reinterpret_cast' in C++ terms, IIRC.
What you shouldn't try is to do anything to 'c' itself (like ++ or
-- or +=) and then cast back. That may cause alignment problems on
some hardware (although C++ does not specify it that way, it just says
that the behaviour in such case is undefined).
V
> Is any one knows what's wrong with the following code, I was told that it will conpile and run but in some point it will crash.
char* c; long* lg;
c = (char*) lg; lg = (long*) c;
This snippet looks fine to me other than (I'm guessing) you're reading
from an uninitialized "lg".
"Me" <an*****************@yahoo.com> wrote in message Is any one knows what's wrong with the following code, I was told that it will conpile and run but in some point it will crash.
char* c; long* lg;
c = (char*) lg; lg = (long*) c;
This snippet looks fine to me other than (I'm guessing) you're reading from an uninitialized "lg".
He's assigning just pointers.
Ramesh Tharma wrote: Hi,
Is any one knows what's wrong with the following code, I was told that it will conpile and run but in some point it will crash.
char* c; long* lg;
c = (char*) lg; lg = (long*) c;
Ramesh
That code isn't crash (on my machine)
Can you show me a full sourcecode?
> > > char* c; long* lg;
c = (char*) lg; lg = (long*) c;
This snippet looks fine to me other than (I'm guessing) you're reading from an uninitialized "lg".
He's assigning just pointers.
Which is undefined behavior if lg is uninitialized:
4.1/1 "If the object to which the lvalue refers is not an object of
type T and is not an object of a type derived from T, or if the object
is uninitialized, a program that necessitates this conversion has
undefined behavior."
Victor Bazarov wrote: C++ only guarantees that you can cast between pointers of unrelated types only to cast it back and have it unchanged.
C++ makes no such guarantee for pointers in general. It should work
for void* and char* though (there is an escape cluase that says if
you convert to a pointer of a more strict alignment type you may
lose information).
Hi,
Actullay this question was asked on an interview,the code snippets is just
like it is.
And asume that the values are initilazed.
The person who interview me told me that this will crash for some values not
for all.....
Thanks,
Ramesh
"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Ramesh Tharma wrote: Hi,
Is any one knows what's wrong with the following code, I was told that it will conpile and run but in some point it will crash.
char* c; long* lg;
c = (char*) lg; lg = (long*) c;
Ramesh
That code isn't crash (on my machine) Can you show me a full sourcecode?
Ramesh Tharma wrote: The person who interview me told me that this will crash for some values not for all.....
On which?
BTW, interviewers are often not as good as they think ;-)
Prawit Chaivong schreef: char* c; long* lg;
c = (char*) lg; lg = (long*) c;
Ramesh
That code isn't crash (on my machine)
Some bad code crashes after a delay, or only in special conditions,
or only on some CPUs, or only in release mode, ...
Don't assume that no crash==good code.
HTH,
Michiel Salters
msalters wrote: c = (char*) lg; lg = (long*) c;
Ramesh
That code isn't crash (on my machine)
Some bad code crashes after a delay, or only in special conditions, or only on some CPUs, or only in release mode, ... Don't assume that no crash==good code.
I agree with that, but as several people have pointed out, assuming
that lg starts out with some valid value, the conversion to char* and
back to long* has well-defined behavior and yields the original value. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven T. Hatton |
last post by:
The code shown below is an example from the Coin3D documentation. I believe
the use of the C-style cast is safe under the circumstances, but from what
I've been exposed to (TC++PL(SE)), I would...
|
by: Christopher Benson-Manica |
last post by:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use
const myClass* a=reinterpret_cast<const myClass*>(my_val);
or
const myClass* a=(const myClass*)myVal;
...
|
by: Howard |
last post by:
Hi,
I am maintaining a lot of code that is rife with C-style casts. I've
seen a lot of comments that one should not use C-style casts at all. But
I'm wondering what harm there could be in...
|
by: Earl Higgins |
last post by:
The company where I work as a Senior Software Engineer is currently
revamping their (1991 era) "Programming and Style Guidelines", and I'm
on the committee. The company, in business for over 20...
|
by: Ramesh Tharma |
last post by:
Hi,
Is any one knows what's wrong with the following code, I was told that it
will compile and run but it will crash for some values.
Assume that variables are initilized.
char* c;
long*...
|
by: Chuck Bowling |
last post by:
Maybe I'm doing something wrong or just don't understand the concept, but
i'm having a problem with default properties.
My impression of how a default property should act is this;
MyClass c =...
|
by: 0to60 |
last post by:
I'm coding in MC++ and I'm using the System::Collections data structures to
store my own objects. When I get something out of a hashmap, should I be
using dynamic_cast or old C-style casting? In...
|
by: Kobe |
last post by:
Hi,
if I need to convert a size_t to an int, in "older" C++ I'd write the
following code (using C-like "casting"):
<CODE>
std::vector<...> v;
int count = (int) v.size();
// v.size() returns...
|
by: Noah Roberts |
last post by:
I have a class, that inherits from a class that inherits virtually from
another class. I have a breakdown occuring and it is not wrt the
virtually inherited class but one of the other MIed pure...
|
by: brekehan |
last post by:
I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting. I am looking to clean up the
following block by using C++ casting instead of the C style...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
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...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
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)...
|
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...
|
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
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |