473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Length Issue in Char while type casting

Dear All

Please check the below code:

UINT8 MsgLength = 0;

MsgLength = strlen((char *)msg);

if ( MsgLength == 0 || MsgLength 64) {

//Flow A
} else {

//Flow B
}
do you find any issue with the typecasting of the message, will it
create any serious
problem ? As if now I can say that when the message length is more then
256 then it will
fail, apart form this, do any compiler may fail ? looking forward for
the disscussion on this.

Thanks In Advance
Ranjeet Gupta

Jul 17 '06 #1
4 2082
ra***********@gmail.com writes:
Dear All

Please check the below code:

UINT8 MsgLength = 0;
UINT8 does not exist in standard C. I will assume it is an 8 bit
unsigned int, although I may be wrong. Your type will probably handle
numbers between 0 and 255.
>
MsgLength = strlen((char *)msg);
strlen returns a size_t. In your case it will be converted to
UINT8, which may not be able to hold the number. If your string has
3000 characters size_t can hold it but UINT8 will not and the value of
the conversion is unknown.

strlen requires a const char *. If you need to cast msg then there's
something probably wrong with your argument.
>
if ( MsgLength == 0 || MsgLength 64) {

//Flow A
} else {

//Flow B
}
do you find any issue with the typecasting of the message, will it
create any serious
problem ?
Who knows, depends on how msg is declared.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 17 '06 #2


ra***********@gmail.com wrote On 07/17/06 11:45,:
Dear All

Please check the below code:

UINT8 MsgLength = 0;

MsgLength = strlen((char *)msg);

if ( MsgLength == 0 || MsgLength 64) {

//Flow A
} else {

//Flow B
}
do you find any issue with the typecasting of the message, will it
create any serious
problem ?
I don't think it could create a problem, but it
could hide a problem. For instance, if msg is an int
then the compiler must complain about strlen(msg) but
need not complain about strlen((char*)msg).

In short, the cast is almost certainly useless.
As if now I can say that when the message length is more then
256 then it will
fail, apart form this, do any compiler may fail ? looking forward for
the disscussion on this.
If UINT8 in an unsigned eight-bit type, the code
will misbehave for messages longer than 255 characters,
not 256. Why not use size_t? Or if all you care about
is the branching and you don't really need the length
stored separately, you could use

if (strlen(msg) - 1 64 - 1)

.... relying on the "wrap around" properties of unsigned
integer arithmetic and on the fact that (size_t)-1 is
larger than 63.

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

Jul 17 '06 #3
In article <b1*************@ukato.freeshell.org>,
Nelu <sp*******@gmail.comwrote:
>ra***********@gmail.com writes:
> UINT8 MsgLength = 0;
>UINT8 does not exist in standard C. I will assume it is an 8 bit
unsigned int,
> MsgLength = strlen((char *)msg);
>strlen returns a size_t. In your case it will be converted to
UINT8, which may not be able to hold the number. If your string has
3000 characters size_t can hold it but UINT8 will not and the value of
the conversion is unknown.
Earlier, though, you assumed that UINT8 was an unsigned int (of some
particular size.) That being the case, for any particular value
integral returned by strlen, the conversion to UINT8 is well defined,
rater than the result of the conversion being "unknown".

"When a value with integral type is demoted to an unsigned
integer with smaller size, the result is the nonnegative remainder
on division by the number one greater than the largest unsigned
number that can be represented in the type with smaller size."
It happens that in a 2s complement system that this corresponds to
throwing away the upper bits and keeping only the bits that fit within
the smaller unsigned integer, but this value-based definition indicates
the value result for other representation systems as well.

In the example you gave, of 3000, then if UINT8 is 8 bits, the result
would be well defined as being 184 (3000 = 256 * 11 + 184).
Now, of course, if the strlen of msg happened to come out as 2816,
the result of the conversion would be 0 (2816 = 256 * 11), so this
property that the conversion result is well defined does not happen
to be a -useful- property for this code snippet...
--
Prototypes are supertypes of their clones. -- maplesoft
Jul 17 '06 #4
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <b1*************@ukato.freeshell.org>,
Nelu <sp*******@gmail.comwrote:
ra***********@gmail.com writes:
UINT8 MsgLength = 0;
UINT8 does not exist in standard C. I will assume it is an 8 bit
unsigned int,
MsgLength = strlen((char *)msg);
strlen returns a size_t. In your case it will be converted to
UINT8, which may not be able to hold the number. If your string has
3000 characters size_t can hold it but UINT8 will not and the value of
the conversion is unknown.

Earlier, though, you assumed that UINT8 was an unsigned int (of some
particular size.) That being the case, for any particular value
integral returned by strlen, the conversion to UINT8 is well defined,
rater than the result of the conversion being "unknown".

"When a value with integral type is demoted to an unsigned
integer with smaller size, the result is the nonnegative remainder
on division by the number one greater than the largest unsigned
number that can be represented in the type with smaller size."
It happens that in a 2s complement system that this corresponds to
throwing away the upper bits and keeping only the bits that fit within
the smaller unsigned integer, but this value-based definition indicates
the value result for other representation systems as well.

In the example you gave, of 3000, then if UINT8 is 8 bits, the result
would be well defined as being 184 (3000 = 256 * 11 + 184).
Now, of course, if the strlen of msg happened to come out as 2816,
the result of the conversion would be 0 (2816 = 256 * 11), so this
property that the conversion result is well defined does not happen
to be a -useful- property for this code snippet...
I should've said useless, but I didn't diferentiate between the words
when I posted. My mistake. Thank you for pointing that out.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 17 '06 #5

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

Similar topics

3
by: srinivas reddy | last post by:
Hi, I have following questions. 1. Does va_arg allow one to read user defined types. My compiler allows but I am wondering whether it is true for all. 2. I wrote the following code. Pardon my...
13
by: Matthew Jakeman | last post by:
If i have a pointer, char *. That i have checked is only 1 character long, is just casting it to a char using (char) the best way or is there another way ?
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
24
by: Francine.Neary | last post by:
I've read that you should always cast the argument you pass to isupper(), isalnum(), etc. to unsigned char, even though their signature is int is...(int). This confuses me, for the following...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.