Results 1 to 3 of 3

Thread: memset struct with QString elements

  1. #1
    Join Date
    Jan 2006
    Location
    Germany - Mannheim
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default memset struct with QString elements

    Hi,

    I'm a bit confused at the moment. I've got a struct with about 30 elements of different types.
    Most of them are regular C/C++ variables and a few of them are QStrings. Now when I try to memset my instance of the struct my program crashes.

    something like this
    Qt Code:
    1. memset(&mystruct, 0x00, sizeof(mystruct));
    To copy to clipboard, switch view to plain text mode 

    I noticed that QStrings seem not to like this and cause the crash. The question now is:
    What is the appropriate way to initialize my struct ? listing all members and assign them values by hand seems not very handy. Or is there something like qMemset ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memset struct with QString elements

    Quote Originally Posted by Timewarp
    What is the appropriate way to initialize my struct ? listing all members and assign them values by hand seems not very handy. Or is there something like qMemset ?
    You can't use memset to "initialize" objects in C++. Each object has it's own constructor which knows how to properly initialize such object and that's the only way you can do this. If you overwrite a fully constructed object using memset will get unpredictable results.

    If you really want to use memset you can move the scalar variables to another structure and add this structure as a member variable.
    Qt Code:
    1. class SomeClass
    2. {
    3. struct Data
    4. {
    5. int x;
    6. // ...
    7. };
    8. // ...
    9. private:
    10. Data d;
    11. };
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    mundhra (26th October 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany - Mannheim
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memset struct with QString elements

    ehm ... ok ! To be honest I want to delete this topic because of this stupid question
    Of course you're right and I should get some sleep now before posting more stupid things like this

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 17:26
  2. Replies: 16
    Last Post: 23rd May 2008, 11:12
  3. Make error with Qt Jambi 4.3.2_01
    By pamalite in forum Installation and Deployment
    Replies: 0
    Last Post: 22nd November 2007, 13:05
  4. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 21:47
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.