473,765 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

when is typecasting (unsigned char*) to (char*) dangerous?

When are they not consistent?
Nov 14 '05 #1
1 2835
b83503104 wrote:
When are they not consistent?
(In the future, please make sure your entire question
appears in the body of your message. Use the Subject: header
as a synopsis of your question, but do not rely on it to
carry your message unaided.)

Putting the Subject: and the body together, we have
this question:
when is typecasting (unsigned char*) to (char*) dangerous?
When are they not consistent?


The conversion itself is not dangerous. However, it
could be dangerous to use the resulting `char*' to access
the pointed-to characters. In theory, at least, some bit
patterns that are valid as `unsigned char' might be invalid
when considered as `char' -- that is, there might be no
actual `char' value corresponding to the bit pattern.

This concern is mostly theoretical, and would apply to
"exotic" architectures that use signed magnitude or ones'
complement representation for negative numbers and choose to
treat `char' as a signed type. On such systems there are two
distinct representations of zero (100...0 in binary notation
for S.M, or 111...1 for O.C.). When viewed as `unsigned char'
these bit patterns are easily distinguished from 000...0 --
but when viewed as `char', the "minus zero" is indistinguishab le
from "positive zero." Even worse, the alternate forms could be
treated as "trap representations " and could cause your program
to misbehave.

Such concerns do not arise on the two's complement machines
that are prevalent nowadays. Nothing bad will happen when you
convert the `unsigned char*' to `char*', and nothing bad will
happen when you use the `char*' to inspect the bytes. (If an
expert disputes this assertion and mentions "padding bits" or
"trap representations ," pay him no heed until and unless he
can exhibit a system whose `char' representation has such things.
If he says the word "DeathStar" or the abbreviation "DS," he's
just trying to scare you.)

Nonetheless, you must still be vigilant: `char' is unsigned
on some two's complement machines and signed on others. If you
use the `char*' to fetch a `char' value and then index an array
with the fetched value, you may find yourself trying to access
`crc_table[-128]', and this is not likely to be good for your
program's prospects of forward progress. Fetch a `char' value
and start right-shifting it until all the 1-bits "fall off the
end," and you may find yourself in an infinite loop. Beware!

There are some situations where type-punning is guaranteed
to be safe. Given a pointer to any data object, you can safely
convert that pointer to `unsigned char*' and then inspect the
individual bytes of the object. You can safely convert between
a struct pointer and a pointer to its first element, or between
a union pointer and a pointer to any of its elements, or between
any data pointer at all and a `void*'. Sometimes, conversions
of this kind are essential -- but if you find yourself writing
"a lot" of them, it's probably a sign that your data structures
are not well-designed.

--
Er*********@sun .com

Nov 14 '05 #2

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

Similar topics

19
16813
by: Vincent | last post by:
Hi all, I want to convert a char (binary) to an unsigned long. How can I do this? Thanks, Vincent
13
2423
by: brian | last post by:
Quick question: if I have a structure: struct foo { unsigned char *packet; unsigned char *ip_src; };
14
9327
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
89
6069
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
26
11751
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
26
12564
by: Nishu | last post by:
Hi All, Is it valid in C to typecast a pointer? eg. code snippet... considering int as 16 bit and long as 32 bit. int *variable, value; *((long*)variable)++ = value; *((long*)variable)++ = value;
2
3358
by: aki | last post by:
Hi all, i am receiving a buffer from network... there are n number of options in buffer type char* each option contain thr fields of fixed size. 1)type 2)length 3)value example->
27
2790
by: parag_paul | last post by:
Can this be done float a; (int) a = getc();
11
1564
by: venkat | last post by:
Hi, I am new to c++ programming language. I have written small program , where i pass the char string then , i find out length. find_len( char *str); is the proto type for the function. I did type casting to give new name to char , typedef signed char sint;
0
9398
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
10156
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...
1
9951
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
9832
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
7375
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
6649
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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
2805
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.