简体   繁体   中英

C++: Dynamic initialization of double pointer

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!

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):

  1. Create an array of pointers (these pointers are garbage initially) .
  2. Assign a value to the object pointed to by the first of these pointers (but since the pointer contains garbage, dereferencing it is an access violation) .

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.)

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM