数组结构
我正在尝试制作一个小的自定义向量...
元素类,应该指向下一个类的地址,但是使用此代码
class foo {
private:
int attr;
public:
foo(){attr = 10;}
int get_attr(){return attr;}
void set_attr(int a){attr =a;}
};
class element_foo {
private:
foo data;
element_foo *ptr_next;
public:
element_foo(){ptr_next = NULL;}
element_foo(int dat){data.set_attr(dat); ptr_next = NULL;}
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
foo get_data(){return data;}
element_foo get_next(){return *ptr_next;}
void print_array(){
if (ptr_next == NULL) {
std::cout<< data.get_attr()<<std::endl;
}
else {
std::cout<< data.get_attr()<<std::endl;
this->get_next().print_array();
}
}
};
int main (int argc, char * const argv[]) {
// insert code here...
element_foo a1(10);
element_foo a2(15,a1);
element_foo a3(20,a2);
a3.print_array();
std::cout << "Hello, World!\n";
return 0;
}
,当我打印 a3 时,它会出现分段错误...为什么?我的错误在哪里?
I'm trying to make a little custom vector...
element class, should point to the adress of the next class but with this code
class foo {
private:
int attr;
public:
foo(){attr = 10;}
int get_attr(){return attr;}
void set_attr(int a){attr =a;}
};
class element_foo {
private:
foo data;
element_foo *ptr_next;
public:
element_foo(){ptr_next = NULL;}
element_foo(int dat){data.set_attr(dat); ptr_next = NULL;}
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
foo get_data(){return data;}
element_foo get_next(){return *ptr_next;}
void print_array(){
if (ptr_next == NULL) {
std::cout<< data.get_attr()<<std::endl;
}
else {
std::cout<< data.get_attr()<<std::endl;
this->get_next().print_array();
}
}
};
int main (int argc, char * const argv[]) {
// insert code here...
element_foo a1(10);
element_foo a2(15,a1);
element_foo a3(20,a2);
a3.print_array();
std::cout << "Hello, World!\n";
return 0;
}
when I print a3, it get to segmentation fault... why? where is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误出现在这个构造函数中:
您正在获取本地地址。
ptr_next = &next;
一旦函数结束,地址就无效。您需要做的是将
next
作为指针传递:并将 main 更改为:
编辑:
或者,您可以通过引用传递它:
The error is in this constructor:
You are taking the address of a local.
ptr_next = &next;
Once the function ends, the address is invalid.What you need to do is to pass
next
in as a pointer:And change your main to this:
EDIT:
Alternatively, you can just pass it by reference:
问题是您正在存储指向临时对象的指针。
'
next
' 只是您传入的a1
/a2
的副本,因为按值复制。您应该使用按引用复制,以便&next
准确引用a1
/a2
的地址,而不是它们的副本。当上面这行执行时,
a2->ptr_next
并没有指向a1
,而是一个已经被破坏的临时本地对象。
因此
a2->ptr_next
是悬空的。以后通过该指针进行的任何访问都具有未定义的行为。The problem is you are storing a pointer to a temporary object.
'
next
' is just a copy ofa1
/a2
you passed in because of copy-by-value. You should use copy-by-reference so that&next
refers exactly to the address ofa1
/a2
, not a copy of them.When the above line is executed,
a2->ptr_next
doesn't not point toa1
,but a temporary local object which has already been destructed.
Thus
a2->ptr_next
is dangling. Any later access via this pointer has undefined behavior.