-6

I've seen a post something like 2 hours ago about a problem i've met days ago, i'm also looking for an explanation too...

https://stackoverflow.com/questions/25281294/c-cout-float-fail-while-printf-do-the-job

I've encoutered exactly the same behavior with cout, which didn't like float at all ...

I've a seperate threaded-UserInterface, made with openGL, with a callback function attached on a button in my main thread.

class ObjectA//My class
{
    private:
      float light;
      float surface;
      float height;
    public:
      ObjectA();
      ~ObjectA();
      float r_light();
      float r_surface();
      float r_height();
      void render(int x, int y); // Not implemented when bug occured
}

ObjectA::ObjectA(void)
{
    light = 0;
    surface = 0;
    height = 0;
}

float ObjectA::r_light()
{
    return this->light;
}

void displayResult(ObjectA * a) //The callback function attached to the UserInterface-button
{
    cout << a->r_light() << endl;
}

When I runned it, the program crashed hard, and I had to finish the process manually ... The only solution i had was to replace cout by printf, but I didn't really liked that.

Anyone know why i couldn't cout this value ?

Community
  • 1
  • 1
Hotted24
  • 502
  • 3
  • 8
  • 1
    Do `light`, `surface`, and `height` ever get initialized in your `ObjectA` constructor? http://stackoverflow.com/questions/312312/what-are-some-reasons-a-release-build-would-run-differently-than-a-debug-build – Cory Kramer Aug 13 '14 at 12:24
  • 1
    Hard to tell without real code. – chris Aug 13 '14 at 12:26
  • @Cyber Yep initialized to 0 in constructor, will edit my post – Hotted24 Aug 13 '14 at 12:26
  • 1
    So, what makes you think it is `cout << ` that crashes, and not your `r_light()` function? Also, could you give the code that actually is problematic? Because this cannot compile. – Xarn Aug 13 '14 at 12:28
  • @BaummitAugen Yeah sure, i was just trying to remember the code. Sry for the mistake. – Hotted24 Aug 13 '14 at 12:29
  • 2
    And you'll get same (no)answers in that post. Not enough information to know what's going on. – Adriano Repetti Aug 13 '14 at 12:31
  • I just understand the issue the previous OP met when asked to provide more information, to debug this i really had nothing, just the line you have with a simple main, new ObjectA(), start interface with only callback on click (no conditions) ... Nothing fancy – Hotted24 Aug 13 '14 at 12:36
  • The `float` and the `cout` are both red herrings. You have undefined behaviour somewhere in your program, and that causes *something* to be broken enough to crash when you use `cout` but not when you use `printf`. – molbdnilo Aug 13 '14 at 12:40
  • **Mobile** is **ObjectA** right? As I said in your other post you need to _debug_ the code – Massimo Costa Aug 13 '14 at 12:41
  • @molbdnilo probably right, but i thought cout was more stable than printf, that's why I was wondering why printf worked and not cout ! – Hotted24 Aug 13 '14 at 12:42
  • @MassimoCosta Yep it is ... I was reading again the other post, messed up the names sry... And i definitely don't want to debug, just to understand why printf is better than cout on this case. Was not my post btw. – Hotted24 Aug 13 '14 at 12:44
  • Welcome to the programming!!! Often to _understand_ you **MUST** debug – Massimo Costa Aug 13 '14 at 12:46
  • @Hotted24 Did you try outputting something else with `cout`? Did that crash? Did you try `cout << 1.f`? Did that crash? What is your compilers and OS's exact name and version? – eerorika Aug 13 '14 at 12:48
  • @MassimoCosta Thx for the advice, but working in dev, all my day is debugging, i just wanna understand why cout is not working as well as printf, as everyone told me cout was safer – Hotted24 Aug 13 '14 at 12:50
  • @user2079303 Text and things like 150.f were ok, just float var. I remember `float test = 150.f; cout << test << endl;` was not working either. Compiling with VC++ 2010, on Seven64 – Hotted24 Aug 13 '14 at 12:52
  • @Hotted24, Does that also crash when your entire program is nothing more than that line (no opengl or anything)? – eerorika Aug 13 '14 at 12:57
  • @user2079303 See my answer below, your comment was the thing for my colleague to think about it. Thx – Hotted24 Aug 13 '14 at 13:03

1 Answers1

0

Got it, friend of mine has had the same issue.

_setmode(_fileno(stdout), _O_TEXT);

And cout go back to normal behavior. He's telling me something about a sh***y VC++ compiler and openGL old version. (Init openGl world screwed stdout in some sort of way, if i understood)

Hotted24
  • 502
  • 3
  • 8