1

On using the following code

#include <iostream>
#include <vector>
#include <string>

int main()
{

    std::vector<std::string> vec;
    std::string temp{};
    while(std::cin >> temp) vec.push_back(temp);

    std::cout << "\n[ " << vec[0];
    for(int i {1}; i < vec.size(); i++) std::cout << ", " << vec[i];
    std::cout << "]";

}

with the following inputs

kghukg kjhukg 6887 ^D

I get

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

I am using CLion on ubuntu and

asmmo@asmmo:~$ g++ --version
g++ (Ubuntu 9.3.0-8ubuntu1) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I get that error with std=-s++1z and c++2a

Considering a comment, I updated the code to

#include <iostream>
#include <vector>
#include <string>
#include <thread>
using namespace std::chrono_literals;
int main()
{

    std::vector<std::string> vec;
    std::string temp{};
    while(std::cin >> temp) vec.push_back(temp);
    std::cout<<vec.size();
    std::this_thread::sleep_for(10s);
    std::cout << "\n[ " << vec[0];
    for(int i {1}; i < vec.size(); i++) std::cout << ", " << vec[i];
    std::cout << "]";

}

then used

kjjkj jkh 555 ^D

but it waited thwn didn't display anything but the error

asmmo
  • 6,922
  • 1
  • 11
  • 25
  • Maybe, before output in `std::cout << "\n[ " << vec[0];` you may check whether there is anything in `vec`. Though, I'm not clear why the `vec` still should be empty after input of `kghukg kjhukg 6887 ^D` (but who knows...) – Scheff's Cat Mar 31 '20 at 13:03
  • I tried to reproduce on [**Compiler Explorer**](https://gcc.godbolt.org/z/kZZarJ) but I failed (i.e. the program did what expected). (Though, it's using redirected input and that might be a difference if it's actually a terminal issue...) – Scheff's Cat Mar 31 '20 at 13:11
  • You could print the size of the vector after the while loop. If it's 0 it would explain the segfault – Thomas Sablik Mar 31 '20 at 13:13
  • 1
    @ThomasSablik that's what I did in the update. the size couldn't be displayed – asmmo Mar 31 '20 at 13:14
  • 1
    @asmmo use `std::cout << vec.size() << std::endl;` – fjardon Mar 31 '20 at 13:16
  • I can't ^D without pressing enter. Nothing happens. I think it's an terminal issue. `./main <<< "kjjkj jkh 555 "` works for me. – Thomas Sablik Mar 31 '20 at 13:23
  • 1
    @ThomasSablik https://stackoverflow.com/a/7370353/657700 – fjardon Mar 31 '20 at 13:25
  • 1
    @ThomasSablik maybe a bug in clion. I used terminal and all is ok. – asmmo Mar 31 '20 at 13:25
  • @fjardon Yes, ^D ^D helped. – Thomas Sablik Mar 31 '20 at 13:27

1 Answers1

2

My psychic guess is that this is a problem with how CLion is submitting input to the program.

https://youtrack.jetbrains.com/issue/CPP-5704

I can disable keycaps for ⌘+D and Ctrl-D, and rely on the "Send EOF" action or the ⌘-D and Ctrl-D keypresses with Clion keycaps disabled, the result is the same: the program does not receive a EOF but receives some sort of kill signal.

Apparently some people find a way to circumvent this:

https://xbuba.com/questions/45803954

Disabling the run.processes.with.pty in Registry (open via Find Action) usually helps.

I never understood the hype with Jetbrains tools ...

fjardon
  • 7,921
  • 22
  • 31