473,883 Members | 1,669 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 1539
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
2573
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
1744
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
1479
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
2012
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
1761
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
10233
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
1892
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
6005
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
1387
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
5942
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
9940
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11141
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10742
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
10847
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
10415
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...
0
9573
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7126
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();...
1
4611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3232
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.