如何在 C++ 中初始化指向结构体的指针数组?
更新 : 答案在底部。
大家好,
如何初始化“结构体指针数组”?问题是,数组是一个成员变量,并且传递给构造函数中数组声明的大小是变量实体。
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();
在这里,我希望该表是指向 Doubly 节点的指针数组,并且希望所有指针都初始化为 NULL 。这里的代码有什么问题吗?还有什么其他更好的方法来执行上述操作?提前致谢。
更新:
讨论后,考虑到尺寸很大,令人失望,我修改了代码。但是如何用一定数量的 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>
答案更新:
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
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 . 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 .
<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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++ 标准不支持变长数组。相反,只需使用
std::vector
:Variable-length arrays are not supported by the C++ standard. Instead, simply use a
std::vector
:该行
创建一个局部变量
表
,它遮盖了同名的成员。另请注意,该行
不正确,因为它设置了
hashsize
bytes,而您的table
是hashsize
的数组指针,每个指针多于1个字节(32位机上一般为4个字节),即sizeof(table) >= hashsize * sizeof(doubly*)
然而,段错误可能是尝试写入大块内存(至少 4G)的结果,正如@Nawaz 指出的那样)。
The line
creates a local variable
table
which over-shades the member of the same name.Also note that the line
is not correct as it sets
hashsize
bytes, whereas yourtable
is an array ofhashsize
pointers, each of which is more than one byte (usually 4 on 32-bit machine), i.e.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).
正如所评论的,您确实应该使用 std::vector 。
您可能应该更仔细地查看 STL 所提供的内容。否则你会错过很多 C++ 的优秀特性,并且会用类来处理 C。尝试熟悉 RAII 以了解有关 C++ 内存管理可能性的更多信息 (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization)。像 memset 这样的东西在现代 C++ 中并没有太多使用。
As commented, you should really use std::vector.
You should probably look more carefully what the STL has to offer. Otherwise you will miss a lot of C++ nice features and would be doing C with classes. Try to get also familiar with RAII to know more about C++ memory management possibilities (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization). Things like memset aren't really much used in modern C++.