Re: conversion to char*

Discussion in 'C++' started by Victor Bazarov, Jul 31, 2003.

  1. "Marc Schellens" <> wrote...
    > I want to convert, an eg. long int* to a char*
    > But using a static_cast<char*>(...) the compiler complains.


    How?

    > Why?


    Without knowing how it's impossible to tell.

    > So I am using a reinterpret_cast<char*>(...)
    > I know that the long int* points to a valid memeory location.
    > Is the reinterpret cast then dangerous, ie.
    > can I assume that pointers to differnt types have the same length,
    > and all other properties?


    What are you going to do with the char*?

    Victor
    Victor Bazarov, Jul 31, 2003
    #1
    1. Advertising

  2. >>I want to convert, an eg. long int* to a char*
    >>But using a static_cast<char*>(...) the compiler complains.

    >
    >
    > How?


    Edited the program in between...
    something like:
    cannot static_cast from MyType* to char*


    >>Why?

    >
    >
    > Without knowing how it's impossible to tell.
    >
    >
    >>So I am using a reinterpret_cast<char*>(...)
    >>I know that the long int* points to a valid memeory location.
    >>Is the reinterpret cast then dangerous, ie.
    >>can I assume that pointers to differnt types have the same length,
    >>and all other properties?

    >
    >
    > What are you going to do with the char*?


    char *src,*dest;
    size_t byt_length;
    copy( src, src+byt_length, dest);

    I cannot use the original types,
    because the task is to copy an array of some type to another
    type, bytewise.


    > Victor

    thanks,
    marc
    Marc Schellens, Jul 31, 2003
    #2
    1. Advertising

  3. >>I want to convert, an eg. long int* to a char*
    >>But using a static_cast<char*>(...) the compiler complains.

    >
    >
    > How?


    Edited the program in between...
    something like:
    cannot static_cast from MyType* to char*


    >>Why?

    >
    >
    > Without knowing how it's impossible to tell.
    >
    >
    >>So I am using a reinterpret_cast<char*>(...)
    >>I know that the long int* points to a valid memeory location.
    >>Is the reinterpret cast then dangerous, ie.
    >>can I assume that pointers to differnt types have the same length,
    >>and all other properties?

    >
    >
    > What are you going to do with the char*?


    char *src,*dest;
    size_t byt_length;
    copy( src, src+byt_length, dest);

    I cannot use the original types,
    because the task is to copy an array of some type to another
    type, bytewise.


    > Victor

    thanks,
    marc
    Marc Schellens, Jul 31, 2003
    #3
  4. Victor Bazarov wrote:
    > "Marc Schellens" <> wrote...
    >
    >>I want to convert, an eg. long int* to a char*
    >>But using a static_cast<char*>(...) the compiler complains.

    >
    >
    > How?


    ok, got it back:

    basic_fun.cpp:481: invalid static_cast from type `DByte*' to type `char*'

    But I think ES Kim already gave the answer to this.
    Thanks ES Kim.

    >
    >>Why?

    >
    >
    > Without knowing how it's impossible to tell.
    >
    >
    >>So I am using a reinterpret_cast<char*>(...)
    >>I know that the long int* points to a valid memeory location.
    >>Is the reinterpret cast then dangerous, ie.
    >>can I assume that pointers to differnt types have the same length,
    >>and all other properties?

    >
    >
    > What are you going to do with the char*?


    Please see my post in the other thread (somehow it went there...)

    > Victor
    Marc Schellens, Jul 31, 2003
    #4
  5. "Marc Schellens" <> wrote...
    > >>So I am using a reinterpret_cast<char*>(...)
    > >>I know that the long int* points to a valid memeory location.
    > >>Is the reinterpret cast then dangerous, ie.
    > >>can I assume that pointers to differnt types have the same length,
    > >>and all other properties?

    > >
    > >
    > > What are you going to do with the char*?

    >
    > char *src,*dest;
    > size_t byt_length;
    > copy( src, src+byt_length, dest);
    >
    > I cannot use the original types,
    > because the task is to copy an array of some type to another
    > type, bytewise.


    Strange task. Not portable. Use memcpy(), it will convert the
    pointers to 'void*'.

    Victor
    Victor Bazarov, Jul 31, 2003
    #5
  6. Victor Bazarov wrote:
    > "Marc Schellens" <> wrote...
    >
    >>>>So I am using a reinterpret_cast<char*>(...)
    >>>>I know that the long int* points to a valid memeory location.
    >>>>Is the reinterpret cast then dangerous, ie.
    >>>>can I assume that pointers to differnt types have the same length,
    >>>>and all other properties?
    >>>
    >>>
    >>>What are you going to do with the char*?

    >>
    >>char *src,*dest;
    >>size_t byt_length;
    >>copy( src, src+byt_length, dest);
    >>
    >>I cannot use the original types,
    >>because the task is to copy an array of some type to another
    >>type, bytewise.

    >
    >
    > Strange task. Not portable. Use memcpy(), it will convert the
    > pointers to 'void*'.


    Its an interpreter and the language provides this kind of copy...
    Yes, memcpy(...) is more appropriate here I think.
    But why is this not portable?
    I can static_cast to void*

    And: If c++ does not guarntee that pointers to differnt tyes
    have the same properties, why one can static_cast to void* then?

    And stop!
    Actually I copy valarrays.
    Eg. valarray<long int> to valarray< char>
    so
    char* src (and dest) is set to &v[0] (v being a valarray -
    of course different valarrays for src and dest, size is checked before).

    Might there be something evil with the alignment?

    thanks,
    marc
    Marc Schellens, Jul 31, 2003
    #6
  7. "Marc Schellens" <> wrote...
    >
    >
    > Victor Bazarov wrote:
    > > "Marc Schellens" <> wrote...
    > >
    > >>>>So I am using a reinterpret_cast<char*>(...)
    > >>>>I know that the long int* points to a valid memeory location.
    > >>>>Is the reinterpret cast then dangerous, ie.
    > >>>>can I assume that pointers to differnt types have the same length,
    > >>>>and all other properties?
    > >>>
    > >>>
    > >>>What are you going to do with the char*?
    > >>
    > >>char *src,*dest;
    > >>size_t byt_length;
    > >>copy( src, src+byt_length, dest);
    > >>
    > >>I cannot use the original types,
    > >>because the task is to copy an array of some type to another
    > >>type, bytewise.

    > >
    > >
    > > Strange task. Not portable. Use memcpy(), it will convert the
    > > pointers to 'void*'.

    >
    > Its an interpreter and the language provides this kind of copy...
    > Yes, memcpy(...) is more appropriate here I think.
    > But why is this not portable?


    I think you're onto something with alignment. Relative sizes
    of objects and char differ with platforms and implementations.
    'memcpy' is implementation-specific, so they can play tricks
    inside, of which you don't have to know.

    > I can static_cast to void*


    Yes, you can. It's explicitly allowed.

    > And: If c++ does not guarntee that pointers to differnt tyes
    > have the same properties, why one can static_cast to void* then?


    A pointer to void* is a special pointer. You can convert any
    other pointer to an object to it and back and it should yield
    the same value.

    > And stop!
    > Actually I copy valarrays.
    > Eg. valarray<long int> to valarray< char>


    ....mmm.. I don't think it's a good idea. 'memcpy' is not going
    to help you. The Standard only allows copying PODs with memcpy.

    > so
    > char* src (and dest) is set to &v[0] (v being a valarray -
    > of course different valarrays for src and dest, size is checked before).
    >
    > Might there be something evil with the alignment?


    Sure there might. Why do you think you need to copy a valarray
    of long int into a valarray of char?

    Victor
    Victor Bazarov, Jul 31, 2003
    #7
    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. wwj
    Replies:
    7
    Views:
    549
  2. Tim Johansson
    Replies:
    15
    Views:
    8,369
    Christopher Benson-Manica
    Apr 8, 2004
  3. spoilsport
    Replies:
    5
    Views:
    3,837
    BartenderSam
    Mar 4, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  5. Perro Flaco

    const char* to char* conversion

    Perro Flaco, Jun 19, 2006, in forum: C++
    Replies:
    34
    Views:
    1,286
    Victor Bazarov
    Jun 21, 2006
Loading...

Share This Page