0

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!

cyanat
  • 77
  • 7
  • 1
    Do you have to use a double pointer? `std::vector` is much easier and safer to use. – NathanOliver May 30 '18 at 16:17
  • 2
    Please don't think of them as "double pointers" - they are "pointers to pointers". –  May 30 '18 at 16:18
  • 1
    Are you required to use a pointer to pointer type? If not, don't go down that path. – R Sahu May 30 '18 at 16:20
  • Avoid double underscores. They 're reserved for use in the standard library implementation. Using them outside the standard library can have interesting and unfortunate effects. More on that here: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 May 30 '18 at 16:30
  • 1
    Also, you can drop the `[1]` in `__ptrTest = new EstablishmentCloud_site *[1];`. You don't need to specify size if it's only 1. – corsel May 30 '18 at 17:26
  • 1
    If you have *"several EstablishmentCloud_site in my __ptrTest"* then you need to allocate that many pointers, e.g. `__ptrTest = new EstablishmentCloud_site *[number];` **and** each pointer will need to point to sufficient allocated storage to holding each `EstablishmentCloud_site`, e.g. `__ptrTest[x] = new EstablishmentCloud_site;` for each of them. – David C. Rankin May 30 '18 at 20:21
  • I use object generated by gSoap library (for Webservice), and gSoap use pointer to pointer, so I can't use vector. It's also becose of gSoap generation that I have some variables with double underscore. Thanks all, especially to corsel and David, your response were very usefull! – cyanat Jun 01 '18 at 10:30

2 Answers2

1

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

JaMiT
  • 14,422
  • 4
  • 15
  • 31
0

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;
}
cyanat
  • 77
  • 7