473,915 Members | 4,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c style casting

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
Jul 23 '05 #1
10 1540
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_ca st' 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
Jul 23 '05 #2
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".

Jul 23 '05 #3
"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.

Jul 23 '05 #4


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?

Jul 23 '05 #5
Me
> > > 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."

Jul 23 '05 #6
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).
Jul 23 '05 #7
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.goo glegroups.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?

Jul 23 '05 #8
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 ;-)

Jul 23 '05 #9
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

Jul 23 '05 #10

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

Similar topics

26
2580
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 favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void foo(SoNode * node) { if (node->getTypeId() == SoFile::getClassTypeId()) {
15
1746
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; ?
3
1481
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 doing so with the built-in types. For example, if you have a long which needs to be passed to a function as an unsigned long, or a char* that needs to be passed as an unsigned char*, or an unsigned int that needs to be passed as a long, isn't it...
15
2017
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 years, writes medical software, which must ultimately pass FDA approval and ISO 9000 certification. The product the document applies to is a large system, (just over 3 million lines of code) running on a variety of platforms (all Unix-ish), and over...
19
1763
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* lg;
5
10234
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 = new MyClass(); c = "my text"; In the line above, the string is assigned to a field in the class instance.
4
1893
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 other words, should it be Namespace::ObjectName* someObject = (Namespace::ObjectName*)hashMap->Item(key); or Namespace::ObjectName* someObject =
3
6010
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 size_t </CODE>
2
1389
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 virtual classes. I get this in my call stack at the point of explosion: Flo.exe!DDocument::Dispatch(DFZone * ptr=0x0298d260) Line 94 + 0x32 bytes C++ Flo.exe!DDocument::Dispatch(DFZone * __A0=0x0298d260) + 0x2f
5
5946
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 casting. from what I am reading, I should use reinterpret cast in this situation, is that correct? Why does static and dynamic casting fail me? // please excuse the windows types, it is necessary in this code, // but the question remains C++ related
0
9881
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10542
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8100
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.