简体   繁体   English

getIntegerv()和std :: cout的奇怪行为

[英]Strange behaviour with getIntegerv() and std::cout

This code: 这段代码:

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main(void)
{
    GLint version;
    glGetIntegerv(GL_MAJOR_VERSION, &version);
    std::cout << version << std::endl;
    //(Un)comment the next line
    //std::cout << "" << version << std::endl;
    glfwTerminate();
    return 0;
}

outputs: 输出:

32766

or 要么

0
0

when the line is commented or uncommented, respectively. 分别在该行被注释或未注释时。
I know that you have to init glfw and glew libraries before using some methods(with glfw initialized this still happening), but... How is possible that the previous line change its behavior commenting or uncommenting the next line? 我知道您必须先使用某些方法初始化glfw和glew库(使用glfw进行初始化,但是这种情况仍在发生),但是...上一行如何更改注释或取消注释下一行的行为? The machine goes to the past and execute the previous line? 机器过去并执行上一行吗? omg 我的天啊

PS: I know what is an "undefined behavior" but that doesn't mean that you can break the laws of physics, travel to the past and modify the behavior of some methods. PS:我知道什么是“不确定行为”,但这并不意味着您可以打破物理学定律,追溯过去并修改某些方法的行为。

I know that you have to init glew libraries before using some methods, but... How is possible that the previous line change its behavior commenting or uncommenting the next line? 我知道在使用某些方法之前必须先初始化glew库,但是...上一行如何更改其行为注释或取消注释下一行的行为?

Because this is what the "undefined" in undefined behavior means. 因为这就是未定义行为中 “未定义”的含义。

When you call a GL function without a current GL context, anything can happen. 当您在没有当前GL上下文的情况下调用GL函数时,可能会发生任何事情。 So you have undefined behavior from the GL side here. 因此,您在GL端具有未定义的行为。 However, in the real world, most implementations will just do nothing in that case, so version does not get written to, and you are printing the content of an uninitialized variable, so you have undefined behavior on the C++ side then. 但是,在现实世界中,大多数实现在那种情况下什么都不做,因此不会写入version ,并且您正在打印未初始化变量的内容,因此您在C ++端具有未定义的行为。 In the real world, you most likely print some contents of your stack, and by changing the code, you are changing the compiled result. 在现实世界中,您很可能会打印堆栈的某些内容,并通过更改代码来更改已编译的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM