<p>I've got a structure as follows:</p>

<pre><code>typedef struct
{
    std::wstring DevAgentVersion;
    std::wstring SerialNumber;

} DeviceInfo;
</code></pre>

<p>But when I try to use it I get all sorts of memory allocation errors.</p>

<p>If I try to pass it into a function like this:</p>

<pre><code>GetDeviceInfo(DeviceInfo *info);
</code></pre>

<p>I will get a runtime check error complaining that I didn't initialize it before using it, which I seemed to have fixed with:</p>

<pre><code>DeviceInfo *info = (DeviceInfo*)malloc(sizeof(DeviceInfo));
</code></pre>

<p>But then, in the function, when I try to set either of the structures stings, it complains that I'm trying to access a bad pointer when trying to set a value to the string.</p>

<p>What is the best way to initialize this structure (and all of it's internal strings?</p>

You should use
```
new
```
instead of
```
malloc
```
, to assure the constructor gets called for the
```
DeviceInfo
```
and its contained
```
wstring
```
s.
```
```
DeviceInfo *info = new DeviceInfo;
```
```
In general, it's best to avoid using
```
malloc
```
in C++.
Also, make sure to
```
delete
```
the pointer when you're done using it.
Edit: Of course if you only need
```
info
```
in the local scope, you shouldn't allocate it on the heap. Just do this instead:
```
```
DeviceInfo info; // constructed on the stack
GetDeviceInfo( &info ); // pass the address of the info
```
```