C++ If 语句被跳过
C++ If statement skipped
我正在用两个 AABB 实现冲突解决,但编译器似乎完全跳过了 "if(*yDepth > 0)"。当我将断点应用于 visual studio 内的语句时,它会回复“当前不会命中此断点。这是指针的问题吗?我尝试移动语句的内容,改变条件,但它似乎总是跳过有问题的 if 语句。
Vector2* normal = new Vector2();
Vector2* vFrom1to2 = new Vector2();
*vFrom1to2 = Vector2(b->getX() - a->getX(), b->getY() - a->getY());
float* xDepth = new float();
*xDepth = (a->getWidth()) + (b->getWidth()) - abs(vFrom1to2->x);
float* yDepth = new float();
if (*xDepth > 0) {
*yDepth = (a->getHeight()) + (b->getHeight()) - abs(vFrom1to2->y);
std::cout << "break";
if (*yDepth > 0) { //this statement is skipped completely. yDepth is greater than 0 on testing.
if (xDepth < yDepth) {
if (vFrom1to2->x < 0) {
normal->x == -1;
normal->y == 0;
}
else {
normal->x == 1;
normal->y == 0;
}
}
else {
if (vFrom1to2->y < 0) {
normal->x == 0;
normal->y == -1;
}
else {
normal->x == 0;
normal->y == 1;
}
}
}
}
在发布模式下——或者更准确地说,当优化开启时——编译器可以重新排序、重新排列、组合甚至删除代码行。这意味着您编写的代码行与实际生成的机器代码之间不再存在一一对应关系。这可能意味着某些代码行不能再为其分配断点,调试器可能会在逐步执行您的代码时跳过。
通常,由于这个原因,最好在调试模式下进行调试。如果这不可能,请在您感兴趣的行附近设置一个断点,然后从那里逐步执行代码,而不是在您感兴趣的确切行上。
(读者注意:提问者在评论中澄清他们在发布模式下遇到此问题)
我正在用两个 AABB 实现冲突解决,但编译器似乎完全跳过了 "if(*yDepth > 0)"。当我将断点应用于 visual studio 内的语句时,它会回复“当前不会命中此断点。这是指针的问题吗?我尝试移动语句的内容,改变条件,但它似乎总是跳过有问题的 if 语句。
Vector2* normal = new Vector2();
Vector2* vFrom1to2 = new Vector2();
*vFrom1to2 = Vector2(b->getX() - a->getX(), b->getY() - a->getY());
float* xDepth = new float();
*xDepth = (a->getWidth()) + (b->getWidth()) - abs(vFrom1to2->x);
float* yDepth = new float();
if (*xDepth > 0) {
*yDepth = (a->getHeight()) + (b->getHeight()) - abs(vFrom1to2->y);
std::cout << "break";
if (*yDepth > 0) { //this statement is skipped completely. yDepth is greater than 0 on testing.
if (xDepth < yDepth) {
if (vFrom1to2->x < 0) {
normal->x == -1;
normal->y == 0;
}
else {
normal->x == 1;
normal->y == 0;
}
}
else {
if (vFrom1to2->y < 0) {
normal->x == 0;
normal->y == -1;
}
else {
normal->x == 0;
normal->y == 1;
}
}
}
}
在发布模式下——或者更准确地说,当优化开启时——编译器可以重新排序、重新排列、组合甚至删除代码行。这意味着您编写的代码行与实际生成的机器代码之间不再存在一一对应关系。这可能意味着某些代码行不能再为其分配断点,调试器可能会在逐步执行您的代码时跳过。
通常,由于这个原因,最好在调试模式下进行调试。如果这不可能,请在您感兴趣的行附近设置一个断点,然后从那里逐步执行代码,而不是在您感兴趣的确切行上。
(读者注意:提问者在评论中澄清他们在发布模式下遇到此问题)