I found something while playing with VS2017 - c++

While I'm trying to learn throw catch I just compiled my code and I found this output what does that mean?
#include "stdafx.h"
#include <iostream>
using namespace std;
void MightGoWrong() {
bool error = true;
if (error) {
throw 8;
}
// -------------------------
int main()
{
cout << MightGoWrong;
return 0;
}
And output is : 012211A4 what does that mean?
Output
Code

You are not calling your function.
cout << MightGoWrong; is simply printing the address of the function. To call it you should do cout << MightGoWrong();.

Related

XercesDOMParser scoped instantiation causes segfault

I have just come across a strange behavior of the Xerces-C library which I do not understand. The following code, which has already been seen in loads of examples, works for me:
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace xercesc;
int main (int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize ();
XercesDOMParser* parser = new XercesDOMParser ();
// here one might want to add some useful operations
delete parser;
XMLPlatformUtils::Terminate ();
}
catch (...) {
cout << "caught some exception" << endl;
}
return 0;
}
Surely, this code does not do many meaningful things. But it runs and in particular terminates cleanly.
Now, I was trying to avoid the new/delete and switch to a scoped object, like so:
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace xercesc;
int main (int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize ();
XercesDOMParser parser;
// here one might want to add some useful operations
XMLPlatformUtils::Terminate ();
}
catch (XMLException& exc) {
cout << "caught an XMLException" << endl;
}
catch (...) {
cout << "caught an exception" << endl;
}
return 0;
}
This or similar code has also been seen many times. However, when I run it, it creates a segfault after (?) XMLPlatformUtils::Terminate() (that's at least, what my debugger is suggesting). Still, I have successfully worked with a parser object created in that way. And if I omit the call to Terminate(), I see my process terminate cleanly.
Does anyone have a clue what I am doing wrong here?
Paul's comment already gave the correct answer and his explanation why does make sense. So for completeness, here is the code that actually works (note the explicitly created inner scope):
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace xercesc;
int main (int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize ();
{
XercesDOMParser parser;
// here one might want to add some useful operations
}
XMLPlatformUtils::Terminate ();
}
catch (XMLException& exc) {
cout << "caught an XMLException" << endl;
}
catch (...) {
cout << "caught an exception" << endl;
}
return 0;
}
Thank you Paul!

Segmentation fault(core dumped) -ROS error

Trying to write a cpp code to print out messages from camera image using darknet. I built a class in which there is mutex method which I use for utilizing callback message in multiple threads. Although catkin_make builds the file successfully, it gives segmentation error when I run the ros command with rosrun . The code is as follows:
#include "ros/ros.h"
#include "darknet_ros_msgs/BoundingBoxes.h"
#include "darknet_ros_msgs/BoundingBox.h"
#include<string>
#include<thread>
#include<iostream>
#include <mutex>
#include "geometry_msgs/Twist.h"
class Firstolo
{
private:
std::mutex yolo_mtx;
darknet_ros_msgs::BoundingBoxes last_yolo_msg;
public:
void callback(const darknet_ros_msgs::BoundingBoxes& msg)
{
std::lock_guard<std::mutex> lck(yolo_mtx);
last_yolo_msg = msg;
}
const darknet_ros_msgs::BoundingBoxes getYoloLastMsg()
{
std::lock_guard<std::mutex> lck(yolo_mtx);
return last_yolo_msg;
}
void dothejob()
{
std:: cout << "Here it goes: " << getYoloLastMsg().bounding_boxes[0].xmin << std::endl;
std:: cout << "Here it goes: " << getYoloLastMsg().bounding_boxes[0].xmax << std::endl;
std:: cout << "\033[2J\033[1;1H";
}
Firstolo()
{
}
Firstolo(Firstolo&)
{
std::mutex yolo_mtx;
}
~Firstolo()
{
}
};
int main( int argc, char **argv)
{
ros::init(argc,argv,"cood_subscriber");
Firstolo nc;
ros::NodeHandle nh;
ros::Subscriber sub;
sub = nh.subscribe("/darknet_ros/bounding_boxes", 100, &Firstolo::callback, &nc);
nc.dothejob();
ros::spin();
return 0;
}
Edit: It turns out that the problem is in the void dothejob(). I added std::lock_guardstd::mutex lck(yolo_mtx); to the void dothejob() and Segmentation error no longer shows up. Now the only remaining problem is that std:: cout << "Here it goes: " << getYoloLastMsg().bounding_boxes[0].xmin << std::endl; line keeps waiting for messages rather than printing them out. In fact, messages naturally should appear since there is darknet running in the background and generating messages.

Net Beans C++ Code Giving always error in the output

#include <iostream>
using namespace std;
int main ()
{
cout << "test" << endl;
return 0;
}

rapidjson + c++: "abort() has been called" error

I need to parse json in my C++ program. I decided to use RapidJson library for this purpose, but I got "abort() has been called" error. I truncated the code to this:
#include <iostream>
#include <cstdlib>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/encodings.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
wchar_t request[] = L"{\"result\":\"OK\"}";
int main()
{
WDocument d;
d.Parse(request);
WValue& v = d[L"result"]; // The exception throws here
if (wcscmp(v.GetString(), L"OK"))
{
cout << "Success!" << endl;
}
else
cout << "Fail!" << endl;
system("pause");
return 0;
}
but I got the error again. Where is the mistake? Thanks in advance!
check this line:
wchar_t request[] = L"{\"result\":\"OK\"}";
there is a character before the left brace.

Is std::uncaught_exceptions useful for avoiding all exceptions? [duplicate]

This question already has answers here:
How to catch segmentation fault in Linux?
(5 answers)
Catching access violation exceptions?
(8 answers)
Closed 6 years ago.
I need to catch segmentation fault and other unknown exceptions in my application. But I do not know how I can do that!
Can I use std::uncaught_exceptions for this aim?
Can I use std::uncaught_exceptions for this aim?
Consider this code:
int main(int argc, char* argv[])
{
int *val = NULL;
*val = 1;
std::cout << "uncaught: " << std::uncaught_exceptions() << std::endl;
return 0;
}
This will likely cause a segmentation fault and nothing will be output.
I need to catch segmentation fault and other unknown exceptions in my application. But I do not know how I can do that!
Exception handling in C++ can be done through the try-catch block, and you could use the std::signal function to catch certain errors like SIGSEGV, SIGFPE, or SIGILL, example:
#include <iostream>
#include <exception>
#include <csignal>
#include <cstdio>
extern "C" {
void sig_fn(int sig)
{
printf("signal: %d\n", sig);
std::exit(-1);
}
}
int main(int argc, char* argv[])
{
int *val = NULL;
std::signal(SIGSEGV, sig_fn);
try {
*val = 1;
} catch (...) {
std::cout << "..." << std::endl;
}
if (std::uncaught_exception()) {
std::cout << "uncaught" << std::endl;
}
std::cout << "return" << std::endl;
return 0;
}
But you should note that this type of exception handling is really meant to do clean-up and shutdown, not necessarily catch and release; take this code for example:
#include <iostream>
#include <exception>
#include <csignal>
#include <cstdio>
extern "C" {
void sig_fn(int sig)
{
printf("signal: %d\n", sig);
}
}
int main(int argc, char* argv[])
{
int *val = NULL;
std::signal(SIGSEGV, sig_fn);
while (true) {
try {
*val = 1;
} catch (...) {
std::cout << "..." << std::endl;
}
}
if (std::uncaught_exception()) {
std::cout << "uncaught" << std::endl;
}
std::cout << "return" << std::endl;
return 0;
}
This code will cause and catch the segmentation fault forever!
If you are trying to catch a segmentation fault, you need to investigate why the segmentation fault (or any error for that matter) happened in the first place and correct that issue; using the above code as an example:
int *val = NULL;
if (val == NULL) {
std::cout << "Handle the null!" << std::endl;
} else {
*val = 1;
}
For further reading: here is a SO Q&A on what a segfault is, as well, here is the Wiki on it, and MIT has some tips on handling and debugging segfaults too.
Hope that can help.