Results 1 to 4 of 4

Thread: Reinterpret cast can fail on a structure with char array inside it =

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Reinterpret cast can fail on a structure with char array inside it =

    I have the typical code to save a structure .
    Qt Code:
    1. const char* buffer;
    2. buffer = reinterpret_cast<const char*> (&value);}
    To copy to clipboard, switch view to plain text mode 
    It fails when the structure (value) I pass to it has a fixed char array inside it (in example char title[200]).
    Any idea ?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reinterpret cast can fail on a structure with char array inside it =

    post an example code please
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reinterpret cast can fail on a structure with char array inside it =

    Thanks Here you are

    Qt Code:
    1. struct W__header {
    2. int number;
    3. char title [200];
    4. double inicial, final;
    5. int num;
    6. float interval;
    7. };
    To copy to clipboard, switch view to plain text mode 

    To set the title I have ( I think here is the problem :
    Qt Code:
    1. void set_title(std::string txt) {
    2. strncpy(title,texto.c_str(),200) ; }
    To copy to clipboard, switch view to plain text mode 

    And my 'put' function :

    Qt Code:
    1. template <class TTput_stream>
    2. void W_inpout::W_stream::put (TTput_stream &value) {
    3. loc+= sizeof value; it works fine
    4. lof=loc;
    5. const char * buffer ;
    6. buffer = reinterpret_cast<const char*> (&value);
    7. }
    To copy to clipboard, switch view to plain text mode 

    I user put(my_class->header);

    The buffer and the function works fine with my other structures.
    At debugger I see title filled with the std::text passed to ser_title an a lot of \0 until the end of var

    Maybe reinterpret_cast does not work becase the structure has the char 'title' inside it ?


    Any help would be appreciated.
    Last edited by tonnot; 15th July 2011 at 18:16.

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reinterpret cast can fail on a structure with char array inside it =

    I have fix the problem.

    If you want a generic function so save to a byte array any class using templates, you cannot use std:string inside your classes, because inside this generic type template function you cannot know the real size of the class you want to store.

    That is to say, if you want to use std::string into a class, you need a custom function to can compute the size of the string and so, can to know the right number of bytes of your instance.

    So, you have to use char[] and strcpy the std::string to the char . Its a pitty, because then you need to include stdio.h.

    Good weekend

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 08:28
  3. Cast to const char *
    By brevleq in forum Qt Programming
    Replies: 3
    Last Post: 3rd January 2009, 09:39
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  5. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.