简体   繁体   English

如何在C ++中初始化一个指向结构的指针数组?

[英]How to initialize an array of pointers to a struct in C++?

UPDATE : Answer at the bottom. 更新:在底部回答。

Hi Guys, 嗨,大家好,

How to initialize an 'array of pointers to a struct' ? 如何初始化'结构指针数组'? The catch is, the array is a member variable and the size passed to the declaration of the array in the constructor is variable entity. 问题是,数组是一个成员变量,构造函数中传递给数组声明的大小是变量实体。


    typedef struct Node {
        string key;
        int value;
        struct Node * left;
        struct Node * right;
    }doubly;

    class myHashStrKey{

    private:
        size_t hashsize;
        doubly * table[];

    public:
        myHashStrKey(){
            hashsize = ((size_t)-1);
            doubly * table[hashsize];
            memset(table,NULL,hashsize);// This is giving segmentation fault
        }

    };

//Called constructor;    myHashStrKey sss = myHashStrKey();

Here I want the table to be a array of pointers to the Doubly nodes and I want all the pointers to be initialized to NULL . 在这里,我希望表是一个指向Doubly节点的指针数组,我希望所有指针都被初始化为NULL。 Whats wrong with this code here ? 这个代码在这里有什么问题? What other better way are there to perform the above ? 还有什么更好的方法来执行上述操作? Thanks in advance. 提前致谢。

UPDATE : 更新:

After the discussion, considering the size is big let down I have modified the code . 经过讨论,考虑到大小很大,我已经修改了代码。 But how to fill vector table with certain number of NULL values ?I tried the below code but it is not working . 但是如何使用一定数量的NULL值填充向量表?我尝试了下面的代码,但它不起作用。

<pre><code>
for(int i =0;i < hashsize;i++){
            table.push_back((doubly *)NULL);
        }

table.insert(table.begin(),hashsize,NULL);
//Both give invalid static_cast from type `int' to type `doubly*'
</code></pre>

ANSWER UPDATE : 答案更新:


    myHashStrKey::table(myHashStrKey::hashsize, static_cast(0));
    myHashStrKey::table(myHashStrKey::hashsize);
    //Above 2 does not work

    for(int i =0;i != myHashStrKey::hashsize;i++){ //lesser than symbol spoils the display
        myHashStrKey::table.push_back((doubly *)NULL);
    }
    //Above works 
    myHashStrKey::table.insert(myHashStrKey::table.begin(),hashsize,((doubly *)NULL));
    //This too works

Variable-length arrays are not supported by the C++ standard. C ++标准不支持可变长度数组。 Instead, simply use a std::vector : 相反,只需使用std::vector

private:
    std::vector<doubly *> table;

...

myHashStrKey()
    : table(num_elements, NULL)  // Initialises vector
{
    ...
}

The line 这条线

        doubly * table[hashsize];

creates a local variable table which over-shades the member of the same name. 创建一个局部变量 table ,该table覆盖同名的成员。

Also note that the line 还要注意这一行

        memset(table,NULL,hashsize);// This is giving segmentation fault

is not correct as it sets hashsize bytes , whereas your table is an array of hashsize pointers , each of which is more than one byte (usually 4 on 32-bit machine), ie sizeof(table) >= hashsize * sizeof(doubly*) 因为它设置hashsize 字节是不正确的,而你的table是一个hashsize 指针数组,每个指针都超过一个字节(通常是32位机器上的4个字节),即sizeof(table) >= hashsize * sizeof(doubly*)

The segfault however is probably a result of trying to write a huge chunk of memory (at least 4G), as @Nawaz pointed out). 然而,段错误可能是试图写入大量内存(至少4G)的结果,正如@Nawaz指出的那样)。

As commented, you should really use std::vector. 如评论所述,您应该使用std :: vector。

You should probably look more carefully what the STL has to offer. 您可能应该更仔细地查看STL提供的内容。 Otherwise you will miss a lot of C++ nice features and would be doing C with classes. 否则你会错过许多C ++不错的功能,并且会用类做C。 Try to get also familiar with RAII to know more about C++ memory management possibilities (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization). 尝试熟悉RAII以了解有关C ++内存管理可能性的更多信息(http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization)。 Things like memset aren't really much used in modern C++. 像memset这样的东西在现代C ++中并没有被广泛使用。

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

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