0

I'm getting "Killed" and "Bus error" randomly when assigning a string literal to an element in an array of strings.

int length = seqLength * 20;
string encoded[numSeqs][length + 1];
string headers[length + 1];
string keys[length];
headers[0] = "name";

If I comment out the last line, it shows no error.

Just in case, the data I'm testing with is numSeqs = 1, seqLength = 42, hence, length = 8400, and I'm compiling it with g++ (g++ -lz -fopenmp main.cpp -o encoder).

  • 4
    First: You are using a non-standard extension to C++, so called _variable length arrays_. In standard C++ the size of an array must be a compile-time constant. Secondly, you might just be exhausting the stack. Use `std::vector` instead of arrays. It will solve both problems. – user17732522 Jan 03 '22 at 03:59
  • You are probably exceeding the stack limit. an individual `string` takes up a fair number of fixed bytes depending on implementation. As @user17732522 it's also non-standard. But the fact it compiles indicates the compiler has the extension.. I also concur you should use vectors of strings. – doug Jan 03 '22 at 03:59
  • 1
    @PeterTrencansky usage of `new` is highly discouraged in C++ – justANewb stands with Ukraine Jan 03 '22 at 04:00
  • @PeterTrencansky that is not a proper way, it would not even compile. – Slava Jan 03 '22 at 04:37
  • @Jellyboy -- Sutter has done much more than write books and standards. Maybe you are thinking about H. Schildt, not H. Sutter. Also, many C++ code reviewers look at using `new[]` and `delete[]` as a "bug", unless it is explained by the coder as to why they are using it. – PaulMcKenzie Jan 03 '22 at 05:05
  • 1
    "killed" typically means the OS has detected a process accessing memory that the OS has not assigned for use by that process. A "bus error" typically occurs when hardware detects usage of memory that isn't physically mapped, so generates a hardware signal/trap/interrupt, and the OS has a registered handler that signals a currently scheduled process that is presumed to be the culprit. VLAs (if they compile) of non-trivial objects (e.g. `std::string`) are prone to trigger such signals, since VLAs are allocated on stack and elements dynamically allocate/deallocate memory (or other resources). – Peter Jan 03 '22 at 05:48
  • 1
    @Jellyboy apparently, SO C++ community: https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new – justANewb stands with Ukraine Jan 03 '22 at 06:52
  • @user17732522 Thanks a lot! Changing everything was a pain in the neck, but it worked. – David Guevara Jan 03 '22 at 09:07
  • @PaulMcKenzie Sutter is a self-described "author". I got some of his talks, he's an academic mathematician completely detached from reality. He creates complexity in his mind to sell books later. –  Jan 03 '22 at 12:52
  • @justANewbie "Community" –  Jan 03 '22 at 13:02
  • @Jellyboy Well, the OP fixed the issue with very little problems using `std::vector`. Herb Sutter and the SO community to the rescue, I guess. As to Sutter, he is the one who was hired by Microsoft to overhaul their standard library from Plauger. That is no simple feat. – PaulMcKenzie Jan 03 '22 at 14:28
  • On "academic mathematician", I don't believe that's a bad thing, but he is neither. (If the C++ committee in fact had more academics and fewer "practitioners", the language might be in a much better spot. The complexity we have comes from the practitioners.) – Jeff Garrett Jan 03 '22 at 15:12
  • @JeffGarrett Complexity comes from academics who have the pressure to publish and end up creating complexity where it does not exist. Even Peter Higgs said he would not have predicted the Higgs boson if he was today in academia. It's a very corrupt environment where over 70% of papers cannot be reproduced https://en.wikipedia.org/wiki/Replication_crisis –  Jan 03 '22 at 15:22
  • @PaulMcKenzie std::vector is the only thing good in the STL. The rest is pure garbage. That's why companies create their own as EASTL, Folly, Abseil, even boost. What's in the STL is slow and cumbersome to use. –  Jan 03 '22 at 15:23
  • @Jellyboy STL contains algorithm functions, iterators, etc. not just containers. Also, Alex Stepanov created the STL, not Sutter. And note, all of the other alternatives you mentioned uses similar, if not the same concepts as introduced by the STL (iterators, for example). – PaulMcKenzie Jan 03 '22 at 15:28
  • @DavidGuevara -- It may be a pain in the neck, but after those changes were applied, the issue was solved. How long did it take you to make the changes? Did the amount of time taken to make the changes was equal to, less than, or more than the amount of time it took to try and figure out the problem? In other words, a few hours to make the changes, as opposed to a few days trying to diagnose the bug? – PaulMcKenzie Jan 03 '22 at 15:33

0 Answers0