Problem with using Boost::Regex (Console just freezes up) - c++

For some reason boost::regex overloads my application and it freezes without an error, but it compiles fine. For instance this code fails flatly. What am I doing wrong? I updated to boost 1.47 to see if it was a DLL error, but it still doesn't work. Can I get an example program to test out the boost::regex?
static const boost::regex expression("^[0-9]+");
std::string str = "123a1";
std::cout << boost::regex_search(str.c_str(), expression);

The first thing to do is to see if your version of Boost supports
threading. Compiling and running something like the following should
tell you that:
#include <iostream>
#include <boost/regex.hpp>
int
main()
{
#ifdef BOOST_HAS_THREADS
std::cout << "Boost has threads" << std::endl;
#else
std::cout << "Boost doesn't support threads" << std::endl;
#endif
return 0;
}
The second thing is to verify that all of the requirements are met.
You've just posted the actual lines, not the context in which they are
executed. If the first line is in namespace scope, you should be OK
(unless you've started threading in constructors to static objects,
before entering main: don't do that). If the first line has block
scope (i.e. is in a function), then you are only OK if the first call to
this function occurs before threading starts. (From what I understand,
with g++, you should be OK even if the first line has block scope, but
I'm not sure.)

After some work I deleted the boost installation from BoostPro and compiled boost myself and now it works. The problem was that BoostPro did not install all of the DLL's and I thought when it asked me for a missing DLL that BoostPro named them wrong (boost_regex-vc100-mt-1_47.dll instead of boost_regex-vc100-mt-gd-1_47.dll). After getting the correct DLL everything works fine. Thank you for your help troubleshooting this!

Related

Can't operate on vectors in C++ (Vscode)

I'm using the g++ compiler and used the https://code.visualstudio.com/docs/languages/cpp guide to install everything. Never had any sort of problem.
I can initialise a vector from the standard library, but as soon as I attempt to initialise it with values, or add to it, or print its size after adding to it, I get a blank line in the console. There is no compilation error and i've tried compiling with the -std flag as 'c++11' and 'c++17'. The odd thing is that even if I put a cout statement before I add to the array, then it won't even output anything - it's like it just halts the whole program.
I am using vscode, and I've read of vaguely similar issues but none of the problems are identical and none of the solutions have worked. My code is below:
Imports:
#include <iostream>
#include <limits>
#include <vector>
#include <string>
Main function:
int main()
{
std::cout << "BEFORE";
std::vector<int> data;
std::cout << data.size(); // Sometimes outputs 0 if the vector is not modified but prints nothing if it is(even after the statement)
data.push_back(20);
std::cout << data.size();
std::cout << "AFTER";
}
Output:
UPDATE: Flushing the buffer and updating mingw haven't changed anything.
Thanks to #n. 1.8e9-where's-my-share m. I was able to find an answer. The problem was to do with Vscode not being able to display the vector. I ran the compiled .exe using the mingw64 console (outside of vsc, not the internal one) and it produced the correct output.

Supersede c++ library printed lines

I have a c++ application, where I link my main.cpp with some pre-built libraries (.a files, I dont know their internal details). The main program looks something like this:
int main() {
printf("..this is my part of the code.\n");
// other code here
}
Then when I run my application, it produces the following output, where the first line comes from the linked library:
Welcome to product XYZ, version 1.2
..this is my part of the code.
As an experiment, I added an "exit(0)" as the first line in my main.cpp:
int main() {
exit(0);
printf("..this is my part of the code.\n");
// other code here
}
And I got this as the output:
Welcome to product XYZ, version 1.2
My question is, how does the linked library start printing even before the first line of my code gets executed? What would be the code in the library (an example), which would make that behavior? And secondly, if I want my line to be printed before the library line, how would I go about doing it?
(Note: the subject line for this question may not match the exact question that I am asking, I was not sure how to frame the subject line to summarize my question. Apologies for that in advance.)
As everyone has said in the comments, initializer code for statics and globals is executed before main(), and if this code prints a message, you cannot have something in main() supersede it. Suppose your main program is in main.cc, as you have it, and the library has a single file, thing.cc, like this:
#include <iostream>
class Thing {
public:
Thing() { std::cout << "Welcome to the Thing." << std::endl; }
};
static Thing _thing;
Now, compile this way:
c++ thing.cc -o thing.o
c++ main.cc thing.o -o main
./main
and you'll see the message from Thing appear before you can even exit.
When there is more than one, the order of these initializers is implementation-dependent, but they are all guaranteed to happen before main() is called.

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.

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.

How to handle C++ boost::filesystem functions that return string_type?

I'm trying to compile the following snippet of code:
#include <boost/filesystem/path.hpp>
//Other includes snipped for brevity
boost::filesystem::path inPath("C:\\PathThatDoesNotExist");
std::cout << "Folder \'" << inPath.external_directory_string()
<< "\' does not exist.";
and I'm getting this error:
.\PathCheck.cpp(31) : error C2679:
binary '<<' : no operator found which
takes a right-hand operand of type
'const
boost::filesystem3::path::string_type'
(or there is no acceptable conversion)
My research so far tells me that this means the type is not known at compile time and that I might need a typedef? but I can't quite make that work in my head or in my code. The external_directory_string() function is deprecated, so I'm also concerned that could be a factor somehow (I'm handling it with #define BOOST_FILESYSTEM_DEPRECATED).
Can someone explain string_type and recommend how to turn it into a std::string or char*? I know there are other ways to get this information from a directory path, but I'd like to understand the issue anyway. :)
RESOLVED: I have v2 of the 1.44 boost libraries, but had "#define BOOST_FILESYSTEM_VERSION 3" in my header (a co-worker using another version had added it). If I comment this directive, my code compiles correctly. Looks like we need to synchronize our libraries.
With the mods below, your code compiles OK for me. This applies with or without the #define you mentioned.
boost::filesystem::path inpath("C:\\PathThatDoesNotExist");
std::cout << "Folder \'" << inpath.external_directory_string()
<< "\' does not exist.";
What version of Boost, and what compiler are you using?
I am on Boost 1.44, Visual C++ Express 2010. external_directory_string appears to return std::string for me.
EDIT:
However, when I explicitly set the Boost Filesystem version like this, I get your error:
# define BOOST_FILESYSTEM_VERSION 3
The default version for me on VS 2010 is 2, so I would investigate why boost/filesystem/path.hpp is setting this to 3 on your build.
From the Boost docs (1.45):
This is Version 2 of the Filesystem library.
Version 3, a major revision with many
new and improved features is also
available, but breaks some existing
code.
Sounds to me like if you get your build back to v2 you will be on more solid ground.
The boost::filesystem::path typedefs external_string_type to std::string (see here http://www.boost.org/doc/libs/1_43_0/boost/filesystem/path.hpp), which would suggest the problem lies elsewhere?