向量错误,如何调试 [英] vector error, how to debug
问题描述
我有一个整数变量,我正在测试它如下
#define NULL_VAL -1
...
if(x [i] .id!= NULL_VAL)
{
std :: cout<< x [i] .id<< std :: endl;
...
}
输出为
32767aborted
索引i = 0并且这被分配给了,所以在某处它被覆盖了
。
x定义为矢量模板
Vec< sx;
struct s {
int id;
...
}
有没有办法可以追溯到这个值的来源
?
im *****@hotmail.co.uk 写道:
我有一个整数变量,我正在测试它如下
#define NULL_VAL -1
..
if(x [i] .id!= NULL_VAL)
{
std :: cout<< x [i] .id<< std :: endl;
..
}
输出是
32767aborted
索引i = 0并且这被分配给,所以在某处它被覆盖了
。
x被定义为一个向量在模板上
Vec< sx;
此向量不包含任何元素。要添加元素,请使用''push_back''。
阅读常见问题5.8,当你在它时。
>
struct s {
int id;
..
}
有没有办法可以追溯到这个价值来自哪里
来自?
错误发生在您的计划的第42行。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
在文章< 11 ********************** @ h48g2000cwc.googlegroups .com> ;,
im*****@hotmail.co.uk 写道:
我有一个整数变量,我正在测试它如下
#define NULL_VAL -1
..
if(x [i] .id!= NULL_VAL)
{
std :: cout<< x [i] .id<< std :: endl;
..
}
将上面的代码替换为:
断言(i> = 0&& i< x.size());
if(x.at(i).id!= NULL_VAL ){
std :: cout<< x [i] .id<< std :: endl;
}
那又做什么用?
-
有两件事情根本不能被怀疑,逻辑和感知。
怀疑那些,你不再*有任何人讨论你的怀疑,
也没有讨论它们的能力。
im ***** @ hotmail.co.uk 写道:
我有一个整数变量,我测试它如下
#define NULL_VAL -1
..
if(x [i] .id!= NULL_VAL)
{
std :: cout<< x [i] .id<< std :: endl;
..
}
输出是
32767aborted
索引i = 0并且这被分配给,所以某处它被覆盖了
。
x被定义为一个向量在模板上
Vec< sx;
struct s {
int id;
..
}
有没有办法可以追溯并查看此值的来源?
C ++语言没有内置功能。你可以使用一个调试器:你
然后可以通过程序逐步观察变量i。但是,
调试器是特定于平台的,并且有关特定调试器的细节
因此在该组中是偏离主题的。您可以在论坛中找到更好的帮助
专用于您的编译器或操作系统。
Best
Kai-Uwe Bux
I have an integer variable and I am testing it as follows
#define NULL_VAL -1
...
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
...
}
The output is
32767aborted
The index i=0 and this is being assigned to, so somewhere it is getting
overwritten.
x is defined as a vector over a template
Vec<sx;
struct s{
int id;
...
}
Is there a way I can trace back and see where this value is coming from
?
im*****@hotmail.co.uk wrote:I have an integer variable and I am testing it as follows
#define NULL_VAL -1
..
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
..
}
The output is
32767aborted
The index i=0 and this is being assigned to, so somewhere it is
getting overwritten.
x is defined as a vector over a template
Vec<sx;This vector contains no elements. To add elements, use ''push_back''.
Read FAQ 5.8, while you''re at it.
>
struct s{
int id;
..
}
Is there a way I can trace back and see where this value is coming
from ?The error is on line 42 of your program.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
In article <11**********************@h48g2000cwc.googlegroups .com>,
im*****@hotmail.co.uk wrote:
I have an integer variable and I am testing it as follows
#define NULL_VAL -1
..
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
..
}Replace your code above with:
assert( i >= 0 && i < x.size() );
if ( x.at(i).id != NULL_VAL ) {
std::cout << x[i].id << std::endl;
}
What does it do then?
--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
im*****@hotmail.co.uk wrote:
I have an integer variable and I am testing it as follows
#define NULL_VAL -1
..
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
..
}
The output is
32767aborted
The index i=0 and this is being assigned to, so somewhere it is getting
overwritten.
x is defined as a vector over a template
Vec<sx;
struct s{
int id;
..
}
Is there a way I can trace back and see where this value is coming from?The C++ language has no built feature for this. You can use a debugger: you
can then step through the program an watch the variable i. However,
debuggers are platform specific and specifics about particular debuggers
are therefore off-topic in this group. You may find better help in a forum
dedicated to your compiler or OS.
Best
Kai-Uwe Bux
这篇关于向量错误,如何调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!