简体   繁体   English

如何正确初始化类函数中的指针?

[英]How to correctly initialise pointers within class function?

I am using a array of pointer's declared within a class to point to another array. 我正在使用在类中声明的指针数组来指向另一个数组。 When a pointer array element is initialised to a value within main that value is also carried to the array element it is pointing to. 当指针数组元素被初始化为main内的值时,该值也将被携带到它所指向的数组元素。 The following code show this and it works fine. 以下代码显示了这一点,并且工作正常。 However if I initialise the pointer within the init class function I get a segfault. 但是,如果我在init类函数中初始化指针,则会出现段错误。 Also if I try to access an element from the pointer array not initialised in main I also get a segfault. 另外,如果我尝试从未在main中初始化的指针数组访问元素,我也会遇到段错误。

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

class testclass {
public:
    double  *Wgt;
    double* *LWgt;

    void init() {
        Wgt = new double[26];
        LWgt = new double*[26];

        //segfault from here
        *LWgt[5] = 22.34543; 
    }
};

int main() {

    testclass *node;
    node = new testclass[10];

    for (int i = 0; i < 10; i++) {
        node[i].init();
    }

    for (int i = 0; i < 5; i++) {               //init_nconn here
        node[i].LWgt[23] = &node[i + 5].Wgt[12];
    }

    node[6].Wgt[12] = 50.6987;
    node[8].Wgt[12] = 0.999923;

    cout << *node[1].LWgt[23] << *node[3].LWgt[23] << "--\n";

    //No segfault
    *node[1].LWgt[23] = 33.234;

    cout << node[6].Wgt[12] << "---\n";
    //No segfault here
    cout << *node[1].LWgt[23] << "---\n";

    //Segfault from here
    cout << *node[3].LWgt[5] << "---\n";
}

Is there a way to initialise the pointer array within a class function without leading to a segfault? 有没有办法在不导致段错误的情况下初始化类函数中的指针数组?

    LWgt = new double*[26];

allocates memory for 26 double* but does not allocate memory for the pointers themselves. 为26 double*分配内存,但不为指针本身分配内存。

The line 线

    *LWgt[5] = 22.34543; 

causes undefined behavior since memory hasn't been allocated for any of the elements of LWgt . 导致未定义行为,因为尚未为LWgt任何元素分配内存。

In order to be able to use that, you have to allocate memory for LWgt[5] first. 为了能够使用它,您必须首先为LWgt[5]分配内存。

LWgt[5] = new double[<SOME_SIZE>];  // Allocate an array

or 要么

LWgt[5] = new double; // Allocate just one element.

That's because none of the pointers stored inside LWgt actually point to anything valid yet. 这是因为存储在LWgt中的指针实际上都没有指向任何有效的指针。 Remember that a pointer will be initialized by the runtime with some random value that may or may not be a valid address. 请记住,指针将由运行时使用某个随机值初始化,该随机值可能是也可能不是有效地址。 When you try to stuff a value inside an invalid address you're going to get an error. 当您尝试将值填充到无效地址内时,您将得到一个错误。

You need to initialize the pointers inside LWgt to point to some actual double memory locations before you can assign any values to them: 您需要在LWgt初始化指针以指向一些实际的double存储位置,然后才能为其分配任何值:

void init() {
    Wgt = new double[26];
    LWgt = new double*[26];

    for(int i = 0; i < 26; i++)
        LWgt[i] = new double;

    //segfault from here
    *LWgt[5] = 22.34543; 
}

As a side note, this kind of initialization is probably best done inside a constructor for your class rather than a special initialization function. 附带说明一下,这种初始化可能最好在类的构造函数中完成,而不是使用特殊的初始化函数。 The point of constructors is that they always run automatically when the class is instantiated. 构造函数的要点是,在实例化类时,它们总是自动运行。 You don't have to worry about remembering to call an init function right after you create the object. 您不必担心创建对象后就记得要调用init函数。

Lwgt is an array of pointers to double. Lwgt是指向double的指针的数组。 You'd have to allocate each element in the array or remove the ** in the declaration. 您必须分配数组中的每个元素或删除声明中的**。

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

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