tags:

views:

91

answers:

4

Currently I have the following:

float some_function(){
    float percentage = 100;
    std::cout << "percentage = " << percentage;

    //more code
    return 0;
}

which gives the output

percentage = 100

However when I add some std::endl like so:

float some_function(){
    float percentage = 100;
    std::cout << "percentage = " << percentage  << std::endl;

    //more code
    return 0;
}

This gives the output:

percentage = 1000x6580a8

Adding more endl's just prints out more 0x6580a8's.

What could be causing this? This is compiled with gcc 4.4.3 on Ubuntu 10.04.

+2  A: 

The function is written correctly. On my machine ( g++ 4.4.3 on Ubuntu 10.04 ) everything works smoothly. Are you sure that the error isn't caused by some other part of the code ?

Piotr Duda
The error seems to be elsewhere unfortunately :(Having a lot of trouble reproducing this which makes me think that there is some memory corruption going on...
shuttle87
If you want to analyse the memory usage try Valgrind (http://valgrind.org/) or use a decent debugger ( I find the one in Eclipse worth recommending)
Piotr Duda
+1  A: 

Your code is perfectly valid. I suspect that you could be smashing your stack or heap in some other part of your code as the most likely cause. 0x6580a8 is too short to be an object address. Also, he would never get the same address in two runs of the same program.

DeadMG
While I get no errors/warnings in valgrind I suspect that this might be happening. The code for the project is very large and I have not written all of it. What would the best way to debug such a problem be?
shuttle87
Now that I have started looking I have found another 0x6580a8 being output from another completely unrelated piece of code.
shuttle87
What do you mean too short to be an address. An address is just a number. Why would he not get the same address in different runs of the program (one would hope that computers are deterministic). I would agree that smashing the stack is a good guess given the code we have but there are other options (like a badly written override for operator >> and function pointers).
Martin York
@Martin York: A 32bit address is 0x6580a800. 0x6580a8 is only 24-bit, unless it truncated the first few. Why he would get different addresses? Because the OS uses virtual memory and sticks everything wherever it likes. Deterministic is not runs identically every time when you modify your environment, like the OS's internal memory management structures, including what other programs have mapped.
DeadMG
0x658Da8 is the same address as 0x00658Da8 (you seem to be getting confused by the stream output formatters doing a nice job on pretty printing a hex number (which is all it is a hex number)). True you may get different results on each run. But you said "never" that's just not true. It is more likely that each run would return the same result, though it is also possible to get different results (but unlikely as each application is run in its own virtual address space).
Martin York
A: 

What if you tried \n ?

std::cout << "percentage = " << percentage  << "\n";
JonH
The '\n' doesn't add anything to the end of the output.
shuttle87
@shuttle87 - it works fine on my pc, can you post entire code with main function.
JonH
A: 

Is this your actual code, or is there a different sort of stream instead of cout?

It's taking the address of the endl manipulator instead of applying it to the stream, which implies that it can't see the matching version of endl for the stream type you're using.

What happens if you use << "\n" << std::flush instead?

Mark B
This is my actual code, cout is the stream. Using << "\n" << std::flush works fine.
shuttle87