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

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.

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.

The C++ vector class does not run on my computer if a vector has data inside. It compiles but will not run

I asked a question about this earlier but I hadn't done hours of research yet. My C++ compiler appears to be fine, but my computer cannot run .exe which were compiled from C++ code using the vector class if the vector has any contents. Here is some extremely basic code that cannot run (no errors thrown, nothing is printed) as a result of this:
#include <vector>
#include <iostream>
using namespace std;
int main()
{
printf("Hello!\n");
std::vector<int> myints;
// myints.push_back(1);
// vector<int> myvector = {10,20,30,40,50};
return 0;
}
With both of those two lines commented out, the code prints Hello! as expected. If either line is uncommented, my computer cannot run the .exe that is built. I thought this was likely a compiler issue so I uninstalled and reinstalled g++ on Windows. Same results.
Then I had a friend compile my code, both with the lines commented and uncommented. He sent me the resulting .exes. The one with both lines commented produced the Hello! output, the one with them uncommented did not. I compiled the code (lines uncommented) and sent him the .exe and it ran.
This is not an issue unique to my compiler, this is coming from my machine somehow. I can do other C++ stuff just fine, like allocating space for an array with malloc() but I want to append stuff onto a list so I need vector to work.

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++ indentifier "read" conflicts?

Recently I'm working on my toy final c++ project, which will read content from a file at the begining and then process it.
Here's the simplified code.
#include <iostream>
#include <fstream>
//using namespace std;
struct command{
int a;
};
command read[1];
int main() {
std::ifstream fin;
fin.open("123.txt");
if (!fin){
std::cout << "failed" << std::endl;
return 0;
}
char c;
fin >> c;
std::cout << c;
return 0;
}
It works fine with Visual Studio 2019. However, when I'm trying to use devc++ 5.11 TDM-GCC 4.9.2, a strange bug happens. I get a segmentation fault on line fin>>c;, with return code 3221225477.
With great effort, the easiest way to make this code works is changing the identifier read to names like reading or whatever. Besides, moving the line command read[1]; into main function also helps.
My questions are:
Is it a behavior related to the compiler? MSVC is fine but GCC 4.9.2 is a little bit old or ...?
Does the identifier read conflict with something in my code? Why does it not a compile error but a segmentation fault?
Why does moving the declaration of read into main function help?
Update:Thanks for tips and I removed using namespace std. I think it has something to do with ifstream, because just std::cout<<"hello world"; works.
-Wall -Wextra provides no warnings.
GCC compiler is stricter than other compiler.
According to tadman's describe, we can guess there's read symbol in namespace std.
So you put it out of main,it's a conflict, you put in the main, it become a local symbol.
There is very likely a collision happening between your read and another in the global namespace. Not all compilers handle this situation the same way, but you can always avoid it if you are careful.
Visual Studio's Intellisense (or whatever alternative you prefer) can help you identify what symbol your read is colliding with. To do that, scope or comment out your code, then start typing "read". If there's another read in your scope, you'll likely see it and be able to get information about it.
This is one of the reasons I don't like using namespace std;. It pollutes your global namespace with a bunch of stuff and increases your chance of collision with a standard library identifier.

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

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!