简体   繁体   English

C ++:动态初始化双指针

[英]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". 似乎__ptrTest的初始化是错误的,因为我得到了“访问冲突读取位置”。 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. 当然,最后,此代码将处于循环中,以便在我的__ptrTest中插入几个EstablishmentCloud_site。

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 ). 如果目的是指向siteCloud ,那么Killzone Kid的答案就是前进的方式( ptrTest[0] = &siteCloud; 我不会提倡使用双下划线 )。 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 ). 如果要将值从siteCloud复制到array元素指向的对象,则需要首先创建该对象(类似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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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