The correct cast

Discussion in 'C++' started by persres@googlemail.com, Jan 13, 2011.

  1. Guest

    Hi,
    I want to know if reinterpret_cast is the correct one here. I want
    to know what are the dangers or issues of doing this.

    unsigned Len=0;
    unsigned char buf[] = { 65, 66, 67, 0 };
    Len = strlen(reinterpret_cast<char *>(buf));

    Why should the compiler give a warning at all. Cant it just do the
    conversion?. Seems harmless. Your thoughts please.
    Thanks
    , Jan 13, 2011
    #1
    1. Advertising

  2. On 1/13/2011 10:52 AM, wrote:
    > Hi,
    > I want to know if reinterpret_cast is the correct one here. I want
    > to know what are the dangers or issues of doing this.
    >
    > unsigned Len=0;
    > unsigned char buf[] = { 65, 66, 67, 0 };
    > Len = strlen(reinterpret_cast<char *>(buf));
    >
    > Why should the compiler give a warning at all. Cant it just do the
    > conversion?. Seems harmless. Your thoughts please.


    I think a 'static_cast' is better in this case. Also, doesn't 'strlen'
    return an int? Assigning it to an unsigned int might also cause a warning.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 13, 2011
    #2
    1. Advertising

  3. On 1/13/2011 11:08 AM, Leigh Johnston wrote:
    > On 13/01/2011 15:59, Victor Bazarov wrote:
    >> On 1/13/2011 10:52 AM, wrote:
    >>> Hi,
    >>> I want to know if reinterpret_cast is the correct one here. I want
    >>> to know what are the dangers or issues of doing this.
    >>>
    >>> unsigned Len=0;
    >>> unsigned char buf[] = { 65, 66, 67, 0 };
    >>> Len = strlen(reinterpret_cast<char *>(buf));
    >>>
    >>> Why should the compiler give a warning at all. Cant it just do the
    >>> conversion?. Seems harmless. Your thoughts please.

    >>
    >> I think a 'static_cast' is better in this case. Also, doesn't 'strlen'
    >> return an int? Assigning it to an unsigned int might also cause a
    >> warning.
    >>

    >
    > static_cast would be incorrect (types are unrelated) so the warning is
    > probably down to "Len".


    Well, shows how much I've used casts in the recent years... You're
    right, reinterpret_cast is the one, shouldn't be any warning. Of
    course, compilers are free to issue any warning at any time, they are
    not mandated, nor is the absence of them.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 13, 2011
    #3
  4. Victor Bazarov wrote:
    > Also, doesn't 'strlen' return an int?


    I'd say it returns a size_t instead.

    > Assigning it to an unsigned int might also cause a warning.


    Yes, but because an unsigned int might be smaller, not because it is
    signed.

    Uli
    Ulrich Eckhardt, Jan 13, 2011
    #4
  5. wrote:
    > I want to know if reinterpret_cast is the correct one here. I want
    > to know what are the dangers or issues of doing this.


    Issues are only with correctness, there are no actual dangers.

    > unsigned char buf[] = { 65, 66, 67, 0 };
    > reinterpret_cast<char *>(buf)


    What you can do is use a static_cast to "void*" and then to "char*" (or
    their const equivalents), which would be fully defined and correct.
    Another alternative would be to use a C-style cast instead. I'd not use
    the reinterpret_cast, because it suggests something implementation-
    defined.

    Or, what you can do in this case is to simply use "sizeof buf - 1" or use
    std::vector instead, but that won't work for general unsigned char
    pointers.

    Uli
    Ulrich Eckhardt, Jan 13, 2011
    #5
  6. Ulrich Eckhardt wrote:

    > Victor Bazarov wrote:
    >> Also, doesn't 'strlen' return an int?

    >
    > I'd say it returns a size_t instead.
    >
    >> Assigning it to an unsigned int might also cause a warning.

    >
    > Yes, but because an unsigned int might be smaller, not because it is
    > signed.


    Unless you tell the compiler to:

    $ cat x.C
    int main()
    {
    int i = 5;
    unsigned int j = i;
    return j;
    }
    $ g++ -Wsign-conversion -O3 x.C
    x.C: In function ‘int main()’:
    x.C:4: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign
    of the result
    x.C:5: warning: conversion to ‘int’ from ‘unsigned int’ may change the sign
    of the result
    Paul Brettschneider, Jan 13, 2011
    #6
  7. Paul Brettschneider wrote:
    > Ulrich Eckhardt wrote:
    >> Victor Bazarov wrote:
    >>> Also, doesn't 'strlen' return an int?

    >>
    >> I'd say it returns a size_t instead.
    >>
    >>> Assigning it to an unsigned int might also cause a warning.

    >>
    >> Yes, but because an unsigned int might be smaller, not because it is
    >> signed.

    >
    > Unless you tell the compiler to:
    >
    > $ cat x.C
    > int main()
    > {
    > int i = 5;
    > unsigned int j = i;
    > return j;
    > }
    > $ g++ -Wsign-conversion -O3 x.C
    > x.C: In function ‘int main()’:
    > x.C:4: warning: conversion to ‘unsigned int’ from ‘int’ may change the
    > sign of the result
    > x.C:5: warning: conversion to ‘int’ from ‘unsigned int’ may change the
    > sign of the result


    Let me fix what I mean: Assigning the result of strlen() to an "unsigned"
    can not yield a warning due to an assignment with signed/unsigned
    conversion. It can only yield a warning because "unsigned" might have a
    different size than "size_t". Sorry for the confusion.

    Uli
    Ulrich Eckhardt, Jan 13, 2011
    #7
  8. Ulrich Eckhardt wrote:

    > Paul Brettschneider wrote:
    >> Ulrich Eckhardt wrote:
    >>> Victor Bazarov wrote:
    >>>> Also, doesn't 'strlen' return an int?
    >>>
    >>> I'd say it returns a size_t instead.
    >>>
    >>>> Assigning it to an unsigned int might also cause a warning.
    >>>
    >>> Yes, but because an unsigned int might be smaller, not because it is
    >>> signed.

    >>
    >> Unless you tell the compiler to:
    >>
    >> $ cat x.C
    >> int main()
    >> {
    >> int i = 5;
    >> unsigned int j = i;
    >> return j;
    >> }
    >> $ g++ -Wsign-conversion -O3 x.C
    >> x.C: In function ‘int main()’:
    >> x.C:4: warning: conversion to ‘unsigned int’ from ‘int’ may change the
    >> sign of the result
    >> x.C:5: warning: conversion to ‘int’ from ‘unsigned int’ may change the
    >> sign of the result

    >
    > Let me fix what I mean: Assigning the result of strlen() to an "unsigned"
    > can not yield a warning due to an assignment with signed/unsigned
    > conversion. It can only yield a warning because "unsigned" might have a
    > different size than "size_t". Sorry for the confusion.


    You are right - because size_t _is_ unsigned.
    Paul Brettschneider, Jan 13, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    515
    Roedy Green
    Jul 8, 2003
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,077
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    640
    Martin Dickopp
    Feb 14, 2004
  4. Joe

    Correct way to cast

    Joe, Mar 14, 2007, in forum: C Programming
    Replies:
    9
    Views:
    346
    Richard Heathfield
    Mar 15, 2007
  5. Pavel
    Replies:
    7
    Views:
    527
    Pavel
    Sep 19, 2010
Loading...

Share This Page