PDA

View Full Version : memset struct with QString elements



Timewarp
5th February 2006, 02:22
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

memset(&mystruct, 0x00, sizeof(mystruct));

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 ?

jacek
5th February 2006, 02:39
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.
class SomeClass
{
struct Data
{
int x;
// ...
};
// ...
private:
Data d;
QString a;
};

Timewarp
5th February 2006, 02:48
ehm ... ok ! To be honest I want to delete this topic because of this stupid question :D
Of course you're right and I should get some sleep now before posting more stupid things like this :p