-3

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("\tNOW 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
theoden8
  • 773
  • 7
  • 16
  • What exactly is the compiler? GCC or Clang/LLVM? – Severin Pappadeux Apr 21 '15 at 21:43
  • Clang/LLVM, as `g++ -v` says. – theoden8 Apr 21 '15 at 21:52
  • 2
    Please create another project with `main` that contains `cerr << 3.14f << "\n";`. Run that program. Does the value 3.14 get printed? – Thomas Matthews Apr 21 '15 at 22:01
  • 1
    What does it actually print, and what value does the float contain? Can you print other types? Can you print a float constant value? – interjay Apr 21 '15 at 22:01
  • @Thomas Matthews yes, it gets printed – theoden8 Apr 21 '15 at 22:09
  • @interjay i construct a window using `Window *black_screen = new Window(800, 800);`. as you can see, in constructor both 800 pass to WIDTH_ and HEIGHT_. I can't print also matrices_.size(), though I can print sizeof(matrices_.size()). I can print 3.14f in the same place, but I can't print that strange HEIGHT_. – theoden8 Apr 21 '15 at 22:14
  • Can you comment out everything in your Window::Display() and add them back line by line, followed with std::cerr << HEIGHT_ << std::endl; and see when you can finally get it print out? Is there any exception being thrown before that line? – newbie Apr 21 '15 at 22:18
  • @newbie ridiculously i commented everything but `std::cerr` and still have seg-fault. – theoden8 Apr 21 '15 at 22:21
  • does your program use a lot of stack space? check this: http://stackoverflow.com/questions/19522192/segmentation-fault-11-in-c-on-mac And also make sure your code does not write beyond the bounds of allocated memory. – newbie Apr 21 '15 at 22:24
  • 1
    My crystal ball says you're calling this method from an invalid pointer. – Mark Ransom Apr 21 '15 at 22:28
  • @MarkRansom i feel like a complete noob, but could you please make yourself more clear at this point? I checked carefully about 8 times before i remembered about this site. – theoden8 Apr 21 '15 at 22:35
  • @newbie i allocated memory for Window object dynamically, isn't it enough to say that stack space isn't the matter? – theoden8 Apr 21 '15 at 22:36
  • What I mean is, your call looks something like `ptr->Display();` and `ptr` is not a valid pointer. Maybe it's NULL, maybe the object has been deleted, maybe the pointer was never initialized. – Mark Ransom Apr 21 '15 at 22:38
  • @MarkRansom I'm afraid `Window::Display` is called like `black_screen->Display()`, whereas the latter is initialized and can't be `NULL` as it's constructed. Well, it should though I didn't initialize `matrices_` which is not needed in the case. – theoden8 Apr 21 '15 at 22:46
  • added `main.cpp` to the initial post – theoden8 Apr 21 '15 at 23:00
  • @theoden: Please include the complete code including `#include "..."` lines! The code given in the question will fail to compile. – Bill Lynch Apr 22 '15 at 03:38
  • @BillLynch isn't it too much for a little post? – theoden8 Apr 22 '15 at 12:01
  • @theoden: It's not really. In this particular case, you removed code that you thought was not relevant, but is actually the root problem that you were having. The best "my code doesn't work" questions have a relatively small set of code that we can copy onto a computer locally and compile it and get the same error. – Bill Lynch Apr 22 '15 at 12:19
  • 1
    @BillLynch yes, now i see. I won't redo same mistake next second time, thank you. – theoden8 Apr 22 '15 at 12:29

1 Answers1

2

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • thank you! The real problem in this particular case was that I declared `Window *black_screen;` and furtherly made a slip in giving it value. Instead of `Window *black_screen = new...` I must have done `black_screen = new ...`, which is obviously not a redeclaration but a working code. – theoden8 Apr 22 '15 at 12:24