c style casting

R

Ramesh Tharma

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
 
V

Victor Bazarov

Ramesh said:
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
 
M

Me

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".
 
P

Prawit Chaivong

Ramesh said:
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?
 
M

Me

char* c;
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."
 
R

Ron Natalie

Victor said:
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).
 
R

Ramesh Tharma

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
 
R

Rapscallion

Ramesh said:
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 ;-)
 
M

msalters

Prawit Chaivong schreef:
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
 
R

Ron Natalie

msalters said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top