Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

c++ - std::cerr can't print float variable

I am writing a program that is supposed to operate with matrices and to use OpenGL framework for drawing them. Honestly, it's just for practice. Anyway, I tried to compile it and, of course, it didn't run well. Inserting as many logging as I could, I found out that my std::cerr can't print float values. I really got no idea how that may stop working. I have made some deep researches on my little code like replacing variables with their values, coding external programs using same functions and so on; and found even more unbelievable thing that there's absolutely no reason for this.

Useful details

OS X 10.9. No bugs regarding to precompilation may occur other than ones caused by #pragma once.

Compiler

Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn).

g++ -std=c++11 -framework OpenGL -framework GLUT

main.cpp

#include <GLUT/glut.h>

#include <cstdlib>

#include "Window.hpp"

Window *black_screen;

void Display    ()                                      {   black_screen->Display   ();                         }
void Reshape    (int NEW_WIDTH, int NEW_HEIGHT)         {   black_screen->Reshape   (NEW_WIDTH, NEW_HEIGHT);    }
void Keyboard   (unsigned char key, int x, int y)       {   black_screen->Keyboard  (key, x, y);                }
void Special    (int key, int x, int y)                 {   black_screen->Special   (key, x, y);                }

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

    Window *black_screen = new Window(800, 800);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glutDisplayFunc     (   Display     );
    glutReshapeFunc     (   Reshape     );
    glutKeyboardFunc    (   Keyboard    );
    glutSpecialFunc     (   Special     );

    black_screen->AddMatrix(Matrix(5));

    Display();

    glutMainLoop();
}

WINDOW.hpp

#pragma once

#include <GLUT/glut.h>
#include <cstdlib>
#include <iostream>
#include <string>

#include "Matrix.hpp"

class Window {
    std::vector <Matrix> matrices_;
    float WIDTH_, HEIGHT_,
          SIZE_X_, SIZE_Y_;
    const char *NAME_;
public:
    Window      (const float WIDTH, const float HEIGHT);
private:
    void Type   (const float x, const float y, const char *string) const;
public:
    void AddMatrix  (const Matrix &A);
    void Write  (const std::string &message);
    void Display    ();
    void Reshape    (int NEW_WIDTH, int NEW_HEIGHT);
    void Keyboard   (unsigned char key, int x, int y);
    void Special    (int key, int x, int y);
};

WINDOW.cpp

// ---  THIS IS CONSTRUCTOR --- --- --- --- ---

Window::Window          (const float WIDTH, const float HEIGHT) :
    WIDTH_(WIDTH), HEIGHT_(HEIGHT),
    SIZE_X_(800), SIZE_Y_(800),
    NAME_("MATRIX") {
        ...
}


// ---  THIS IS DISPLAY FUNCTION CALLED FROM MAIN::Display()    ---

void    Window::Display     () {
    glLoadIdentity();                       Write("glLoadIdentity();");
    glClear(GL_COLOR_BUFFER_BIT);                   Write("glClear(GL_COLOR_BUFFER_BIT);");
    glMatrixMode(GL_PROJECTION);                    Write("glMatrixMode(GL_PROJECTION);");
    Write("	NOW i WILL TRY TO OUTPUT HEIGHT_ :");
    std::cerr << HEIGHT_ << std::endl;
    glOrtho(0, WIDTH_, 0, HEIGHT_, 1, -1);              //Write("glOrtho(0, WIDTH_, 0, HEIGHT_, 1, -1);");
    ...
}

output

[   glutInitWindowSize(SIZE_X_, SIZE_Y_); :
[       SIZE_X_ 800.000000
[       SIZE_Y_ 800.000000
[   glutCreateWindow(NAME_); :
[       NAME_   MATRIX
[   glLoadIdentity();
[   glClear(GL_COLOR_BUFFER_BIT);
[   glMatrixMode(GL_PROJECTION);
[       NOW i WILL TRY TO OUTPUT HEIGHT_ :
make: *** [run] Segmentation fault: 11
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't show this in the code posted, but it appears you have two different definitions for black_screen, one global and one inside the main function. The one inside main has been initialized properly, but the global one is a bad pointer. The segmentation fault is the result of undefined behavior from dereferencing the bad pointer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...