CPP console applications exits without providing error information - c++

When I compile and run the below piece of code the exe crashes yet doesn't provide information of any sort regarding why it crashed. (Seg faults etc. not reported). Here is the sample code that I tried:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector <int> values;
int temp = values.back();
cout << temp << endl;
return 0;
}
The same piece of code when compiled and run-on Linux produce a seg fault. Is there something to be configured specifically in windows to let console applications generate information regarding runtime errors?

Calling std::vector::back on an empty vector is undefined behavior. The compiler or runtime aren't required to emit a diagnostic. There might be a tool or sanitizer that could help depending on the compiler you're using.

Is there something to be configured specifically in windows to let console applications generate information regarding runtime errors?
The problem is that value.back() causes undefined behavior (because your vector is empty). The effect of undefined behavior is undefined. It might result in a seg fault, but also could just exit the application. You can't enforce a segfault for that, and not really enforce a crash.
Using static analyzers and increasing the warning level can help in certain cases. But there is no way to detect all possible errors.

Related

VS Code: Declaring a stack leads to problems with cout

I am learning C++ and was writing a simple program when I noticed that declaring a stack caused cout to not output to the terminal. Here is my program:
#include <iostream>
#include <stack>
int main ()
{
std::stack<int> myStack;
std::cout<<"Hello World!"<<std::endl;
return 0;
}
This problem only occurs in the Visual Studio Code editor. If I use an online C++ compiler, there are no issues and I see "Hello World!" as output. If I remove the declaration of the stack, the output is also normal, even in VS Code. However, when the declaration is present, I see no output when I compile and run my code in VS Code. I have no idea why this would be the case, and other STL containers (I've tested vector, array, and set) do not cause a similar issue, although the problem still occurs when queue is used.
Any help resolving this issue, or any insight as to why it occurs in the first place, would be greatly appreciated.
p.s. This is my first time asking a question, so I apologize if it is poorly written.

Defining a vector breaks program - MinGW

I have been coding for quite a few years now, but have only just recently started getting into C++.
I have already made quite a few programs in it, but have recently started running into some odd behaviour. The cases are simple enough that I expect this to be an error with my environment, and not the language itself, but I have run into a dead end.
Consider this simple program:
#include <iostream>
using namespace std;
int main() {
cout << "Test" << endl;
return 0;
}
If I compile that and run it, I get, as expected, "Test", in my console.
Now, if I add a vector to it:
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "Test" << endl;
vector<double> whatever;
return 0;
}
For some reason, I do not get any output from that.
I have tried initalizing the vector as an empty vector aswell as with predefined values.
I tried adding a for loop running from 0 to 2^32 to see if the program failed entirely, or if it is just the output. This is where things got even weirder.
At first, I placed the loop before defining the vector; that caused the cout to suddenly work again (i.e. "Test" was printed in the console), aswell as stalling the program as expected. I then moved the for loop to after the vector definition, and then it broke entirely; I received no output, and the program exited almost instantly without error.
The issues persist when I remove the using namespace std; and prefix my cout and vector with std::
I use the g++ v6.3 compiler from MinGW. I am running Windows 10.
I am aware that this problem is probably extremely hard to reproduce, but I'll try my luck here before throwing my computer out the window.
Thanks in advance.
Edit: I fixed the issue by using Cygwin instead of MinGW. I will leave the question open in case someone has encountered a similar issue and has a fix that doesn't involve abandoning MinGW
I am running Linux and ran the code, and it ran successfully.
So this must be a compiler issue.
Maybe try using a different compiler like Cygwin / Microsoft Windows SDK.

Is replacing all standard error-dialogs with debug-breaks or error-throws possible?

I would like to replace any standard error-dialog with throw or debug-break (with standard I mean anything which is not explicitly written by me) since like in the reason-section described, it would cause debugging of a Windows-Service sometimes to be impossible.
to accomplish this I did try defining something like:
-D "_HAS_ITERATOR_DEBUGGING=0"
but above did just disable the error-dialogs and is really not enough to track down the issues, so I would like it to throw-exception or to debug-break instead of showing the error-dialog.
is there anything else you would suggest me to define or do?
Reason:
While developing a Windows-Service, I had some hard times to find a bug which was causing the server to crash:
there was an Off-by-one error in std::unordered_map usage
the App's crash-dump feature was prevented by std::unordered_map which tried to show an error message-box (since compiled in debug-mode)
but the program did crash without giving any feedback, basically because a windows-service is not allowed to show any error-dialog (except by using "WTSSendMessage(...)")
even when the debugger was attached, still nothing...
Only using git history and rechecking all recent changes was it possible to find the issue
Reproduce:
by running below in a service (compiled in debug-mode) you can reproduce this issue:
#include <unordered_map>
// will cause crash by trying to increment iterator pointing to end
inline static void simulateCrash() {
typedef std::unordered_map<quint32, quint32> Hash;
Hash list;
list[0xC001] = 0xDEAD;
Hash::iterator it = list.begin();
it = list.erase(it);
++it; // should crash here
}
You probably want to use _set_invalid_parameter_handler to overwrite default handler which terminates the program and displays a runtime error message.
_CrtSetReportMode is also useful to avoid dialog from _CrtDbgReport (used in several checks).

c++ Intel inspector shows many errors with boost - do I need to worry?

I have a problem with my current program. For some reason it always crashed after the final line of code on windows. I got a "application is no longer responding" error or something like this.
So I tried the Intel inspector. And Luckily it told me some bad errors in my project where I accessed some uninitialized memory.
Besides this obvious problems that I understand I get also some:
Incorrect memcpy calls in: boost::algorithm::trim()
Uninitialized partial memory access in: myptree.get<boost::posix_time::ptime>("path.to.node") where myptree is of type boost::property_tree::ptree
Uninitialized memory access in: cout << myptime where myptime is of type boost::posix_time::ptime
...
does this mean that I use the boost library functions not properly? Or are this false positives?
I'm just confused because the functions work, they do what I want them to do and I get no error message.
I also get a Memory not deallocated warning at the end (from [Unknown] source).
example for trim:
#include <iostream>
#include <boost/algorithm/string.hpp>
int main() {
std::string test = " test ";
boost::algorithm::trim(test);
std::cout << test << std::endl;
return 0;
}
gives me a incorrect memcpy call...
Boost will happily forward bad arguments; it often has no way to check them. If boost::algorithm::trim passes a bad argument to memcpy, it will be because you passes a bad argument to trim.
So, yes, you should worry. There are almost certainly multiple bugs in your program. Check your calls to the functions reported.

VALGRIND: invalid read of size 8

i have problems with valgrind,
i created this class:
class shop {
private:
vector<vector<string> > products_;
public:
shop(string ProductFile);
FindMinPrice(string product);
}
//method in the cpp file
vector<string> shop::FindMinPrice(string product){
string ProductName=(string)products_[0][0];
}
i didn't write the entire code but its work fine with the GCC compiler.
when i run valgrind check it shows:
invalid read of size 8
and in eclipse it send me to the ProductName line.
what is wrong with the design ? and how so that the GCC compile and run but VALGRIND collapse?
thank you.
It appears that your products_ vector of vectors is empty, meaning that the access of element products_[0][0] is undefined behavior.
The unfortunate thing about undefined behavior is that your program may appear to work, and may even complete without any visible problems.
Using c-style cast in c++ is not a good practise.
Your shop method doesnt return any value.