c++ - Initializing a ref-to-ptr in a struct after malloc() -
I am experiencing a problem using VC ++ and debug CRT with a DLL in development.
I have a structure that holds some references.
Structure data {TA * & amp; a; TB * & amp; B; TC * & amp; C; TD * & amp; D; Four ** characters; Int num_chars; Private: // Because: // Data A; // data b; // a = b; // Impossible Data is & amp; Operator = (Cantt Data & amp;); // Attach "= delete;" Data for C ++11 / Default CTOR (Private because the structure should be made manually using Molok) Data (TA * A and A, TB * and B, TC * and C, TD * and D): A (A), B (B), C (C), D (D), Character (NULL), NewMars (0) {}};
And create it like this:
Data & amp; () Find () Static Static Data * Data = (Structure Data *) Molec (Format Data); Return data; }
Now it should hold the undefined Rif-to-PTR, which I want to init by:
void func (TA * a) , TB * b, TC * C, TD * D) {Gate () -> A = A; () - & gt; B = get B; () - & gt; C = c; () - & gt; D = D; ...}
which works for everything, but Riff-to-PTR ..
I received a INVALID_POINTER_WRITE_FILL_PATTERN_cdcdcdcd
Going by using WinDbg (Remote Kernel debugging "KD" instance) > Go () -> A = A;
!
Thank you for your help! :)
Edit: Solution
The solution is to use points from the correct answer.
C'tor must be made public:
structure data {TA * & amp; a; TB * & amp; B; TC * & amp; C; TD * & amp; D; Four ** characters; Int num_chars; (D), B (B), C (C), D (D): DTA (TA * and T, TB * and B, TC * and C, TD * and D), Character (NULL), num_chars (0 } {} Private: // Because: // data A; // data b; // a = b; // Impossible Data is & amp; Operator = (Cantt Data & amp;); // Attach "= delete;" For C ++ 11};
To create the structure then using placements new
:
Data & amp; Get (...) {// ... to init and create some stuff, overloading, other init-method etc. such as: static structure data * data = new (malloc (sizeof (struct data)) data (...); // Specify all references in at least return data; }
Then use it and maybe specify everything that is not a reference:
zero function (TA * A, TB * b , TC * C, TD * D) {Go (A, B, C, D); Go () - & gt; Character = ... ...}
To completely clear the whole thing, it is essential to call the door and by calling
, because we have the placement New
:
Data-> ~ Use data (); Free (data);
Note that malloc ()
gives uninitialized memory C ++ objects need to be created. The way to bring an object into unpublished memory is to use the new placement (this code also adds clean up):
#include & lt; New & gt; Statistics * & amp; () Get () {static data * data = new (molok (size (structure data)) data (...); Fixed std :: unique_ptr & lt; Data, zero (*) (data *)> Clean (data, [] (data * d) {d-> data (); free (data);}); Return data; }
There is no way in C ++ to reset the references, that is, they need to be set during creation. Personally, I would not use the malloc ()
but would rather use any appropriate allocation:
static data * data (new data (...) ;
... or, as Jarod42 pointed out, in fact
static data data (...); Return and Data;
Comments
Post a Comment