我有一个定义的结构包含一个std :: list。在我的代码中,我尝试遍历这个列表,但是我得到了一些奇怪的结果。std :: list.begin()给出空指针
struct my_struct_t {
std::list<int> my_list;
//More fields
};
这是我在我的头文件中定义的结构。
而且在一个文件中的一些示例代码,其中包括这头将是:
std::list<int>::iterator my_iterator;
struct my_struct_t* test_struct = (struct my_struct_t*) malloc(sizeof(struct my_struct_t));
my_iterator = test_struct->my_list.begin();
printf("Beginning of list address: %p\n", my_iterator);
my_iterator = test_struct->my_list.end();
printf("End of the list address: %p\n", my_iterator);
printf("Address of the list: %p\n", &(test_struct->my_list));
此代码编译并运行正常,但输出会是这样的:
Beginning of list address: (nil)
End of the list address: 0x86f010
Address of the list: 0x86f010
最后两行对我来说非常有意义,因为列表应该是空的。但是如何/为什么我会在开始时得到一个空指针?我怎样才能解决这个问题?
第1步:阅读一个很好的C++入门,从[那些]中选择一个(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。第2步:意识到尽管有许多相似之处,但C++和C语言却是截然不同的,只是其缺陷非常相似。 – 2013-02-20 07:23:31