std::endl results in crash - c++

During the development of a simple example (I haven't programmed C++ for some time) I encountered a weird behaviour. Following hello world program crashes under Windows (Mingw):
#include <iostream>
int main () {
for (int idx = 0; idx < 5; idx++) {
std::cout << "Hello World" << std::endl;
}
return 0;
}
If I remove std::endl the program does not crash though.
I use following commands to compile and execute the example, with Mingw32 (g++ 4.8.1) on a 64bit system and OS:
g++ example.cpp -o example.exe
example.exe
The error message is:
example.exe does not work any longer...
Is this a known issue or an obvious mistake of mine?

<< endl is a manipulation essentially a function. your output cant be flushed which is causing problems.

Related

Why is CLion not showing exceptions?

CLion appears to be not showing me any exceptions when running my code. To test this, I've created a new project with only the following code:
#include <iostream>
int main() {
std::cout << "--- One" << std::endl;
throw 6;
std::cout << "--- Two" << std::endl;
return 0;
}
Which leads to the following output:
C:\Users\david\CLionProjects\untitled\cmake-build-debug\untitled.exe
--- One
Process finished with exit code 0
As you can see, code before the exception is executed and code following it is not executed (as you would expect). But instead of a message about the exception, it says "Process finished with exit code 0", as if no exception had occurred.
The same code compiled and executed on Ubuntu (via terminal) displayed an error message. So I assume the problem is with CLion.
How can I resolve this problem so that I can see messages for exceptions in my code?
Is there any setting that could lead to such behaviour?
I'm using CLion on Windows 10 with Cygwin. Here's a screenshot of the problem:
Throw requires also try and catch
From:
http://www.cplusplus.com/doc/tutorial/exceptions/
// exceptions
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
that compiled and run under cygwin shows:
$ g++ prova1.cc -o prova1
$ ./prova1
An exception occurred. Exception Nr. 2

C++ promise.set_value fails with unknown error under linux

I'm trying to get my simulation running on our high performance server. It (unfortunately) uses CentOS Linux release 7.7.1908 (Core) instead of Win10, under which I'm developing the program. With this came a large amount of errors, one of them I was not able to fix on my on:
#include <future>
#include <iostream>
int main(int argument_count, char** arguments) {
int i = 1234;
std::cout << "Initialized i" << std::endl;
std::promise<int> promise;
std::cout << "Constructed promise" << std::endl;
promise.set_value(std::move(i));
std::cout << "Set value" << std::endl;
std::future<int> future = std::move(promise.get_future());
std::cout << "Retrieved future" << std::endl;
int j = std::move(future.get());
std::cout << "Got value: " << j << std::endl;
return 0;
}
When compiling this under Win10 with "cl test.cpp", the output looks like I'd expect:
Desktop>test.exe
Initialized i
Constructed promise
Set value
Retrieved future
Got value: 1234
On the other hand, when compiling on the server with "g++ -std=c++11 test.cpp", the output is different:
~/test_dir$ ./a.out
Initialized i
Constructed promise
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted
I do get the same error when trying this with an Ubuntu 16.04.6 LTS machine. I don't see why this happens.
Obviously, something is fishy in this line: promise.set_value(std::move(i)) since the output before is printed and the line after that statement is not executed any more. Furthermore, the compiler/ linker does find a one of the two versions "void set_value (const T& val);" or "void set_value (T&& val);" that would be appropriate for the template specification "int", i strongly suspect the later.
But why is the program aborting when setting an integer as the value of a promise? Even when inlining the value and skipping the variable all together does produce the error.
Can someone point me to where the error is?
Try compiling using the pthread flag:
g++ -std=c++11 test.cpp -pthread
Linking against both pthread and stdc++ may resolve the issue.
Example adding stdc++ in Cmake:
target_link_libraries(
# Your library or binary here
stdc++
pthread
)
Tested with the following code:
#include <iostream>
#include <future>
int main(int argc, char *argv[]) {
static_cast<void>(argc);
static_cast<void>(argv);
std::promise<int> pr;
auto fut = pr.get_future();
pr.set_value(10);
std::cout << fut.get() << std::endl;
return 0;
}

[Solus Linux][Clion] Clion doesn't compile the simplest program

I downloaded Clion and all packages which I needed to work (I hope) and IDE doesn't compile "Hello World" program :(
Code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Build errors:
https://pastebin.com/J5UA9HpX
Any ideas? Regards, Dziura.
Sorry for bad English

Why is cout flushing early?

I've been learning how flushing works with cout, so I decided to perform this quick test.
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
cout << "Line 1..."; // OR cout << "Line 1..." << flush;
usleep(500000);
cout << "\nLine 2" << endl;
cout << "Line 3" << endl ;
return 0;
}
In the case that is presented above, the expected output is for:
Line 1...
Line 2
Line 3
to print out altogether after some delay. However, in the scenario in which
"<< flush;" is included, the expected result is for Line 1 to print immediately, then after some delay, Line 2 and Line 3 print.
These expected outputs ONLY occur when I compile my program on a Linux machine using the command:
g++ -o myFile.out myFile.cpp -Wall
Then run it using:
./myFile.out
When I run these same code pieces on my windows machine, line 1 is ALWAYS displayed immediately, regardless of the insertion of "<< flush;". Why does this happen?
It should be noted that on my windows machine, I am compiling and running my code through codeblocks x64. According to Codeblocks Settings > Compiler > Toolchain Executables, my C++ compiler is "mingw32-g++.exe". Isn't this the same compiler as running g++ on Linux as I did earlier? Thanks!

Code blocks runing error: Build failed

I am new to C++, and now learning it using code blocks (version: codeblocks-16.01mingw-setup.exe). My test codes are as follows:
#include<iostream>
#include<stdlib.h>
int main()
{
int sum = 0, val = 1;
// keep executing the until val is greater than 10
while (val <=10 ) {
sum += val; // short-cut assignment
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
system("pause");
return 0;
}
These codes are written in an empty file named ex1.cpp. Then I tested by click "Build and run". As a result, another file main.cpp (I did not write this) pops up:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Screenshot attached for your better checking:
The reason why you are getting this error is because your compiler settings is not correct.You need to make sure that you use GNU GCC MinGW Compailer.Go To Settings-->Compiler and make sure every thing is same as on the screen shot.
Solving common codeblocks problems :Link
I really did something wrong about coding:
when I create an empty file in the project, it will result in two main functions in the that project one of which is that "hello world" file automatically generated, which is not allowed by C++.
To build it successfully, what I did is to overwrite the codes in the main.cpp.