Reputation: 117
I would appreciate some help to initialize a double pointer. here is my code:
EstablishmentCloud_site **__ptrTest;
EstablishmentCloud_site siteCloud;
__ptrTest = new EstablishmentCloud_site *[1];
siteCloud.f_site_id="site";
siteCloud.f_status="status";
siteCloud.f_name="name";
*(__ptrTest[0])=siteCloud;
It seems that initialisation of __ptrTest is wrong because I get a "Access violation reading location". What is the good syntax?
Of course, at the end, this code will be in a loop to insert several EstablishmentCloud_site in my __ptrTest.
Thanks!
Upvotes: 0
Views: 196
Reputation: 117
With your help, I've managed to create this function. I hope it's not so crappy code! Thanks!
establishmentConversion(mySourceObject establishmentSource)
{
EstablishmentCloud__establishment establishmentCloud;
[...]
establishmentCloud.site_list = new EstablishmentCloudcloud_siteArray;
establishmentCloud.site_list->__ptr = new EstablishmentCloud__cloud_site *;
for(int i=0; i<(*(establishmentSource.site_list)).__size; i++)
{
establishmentCloud.site_list->__ptr[i] = new EstablishmentCloud__cloud_site;
establishmentCloud.site_list->__ptr[i]->f_site_id=(*((*(establishmentSource.site_list)).__ptr[i])).f_site_id;
establishmentCloud.site_list->__ptr[i]->f_status=(*((*(establishmentSource.site_list)).__ptr[i])).f_status;
establishmentCloud.site_list->__ptr[i]->f_name=(*((*(establishmentSource.site_list)).__ptr[i])).f_name;
}
return establishmentCloud;
}
Upvotes: 0
Reputation: 17007
The syntax to use depends on what you are trying to accomplish. As it stands, it is not clear if there is a syntax error or a logical error.
Here is what the current syntax says to do (skipping some irrelevant steps):
You are missing the step where the pointer is assigned a valid value. If the intent is to point to siteCloud
, Killzone Kid's answer is the way to go (ptrTest[0] = &siteCloud;
I am not going to advocate using a double underscore). If the intent is to copy the values from siteCloud
to the object pointed to by the array element, you need to create that object first (something like ptrTest[0] = new EstablishmentCloud_site
).
The former method (assigning addresses) can run into problems if the objects do not have a sufficiently long lifespan. The latter method (more allocations) can run into memory leaks if you do not adequately clean up afterwards. If either of these are problems in your situation, you may want to reconsider if you really want an array of pointers. (You might find that there are standard templates that can make your implementation easier.)
Upvotes: 1