-4

Update on Question: Here is the SSCCE where I receive SIGSEV on str1_h[0][j] = bar1(str1_c[j]);. All variables are not-null which I checked using gdb:

#include <fstream>  //ifstream and ofilestream

using namespace std;

int bar1(int);
int bar2(int);
int bar3(int);

int main() {
    string str1 = "string";
    ifstream ifs;
    ifs.open("./file.txt");
    string line, str2;
    while (!ifs.eof()) {
        ifs >> line;
        str2 += line;
    }
    ifs.close();
    unsigned str2_l = str2.length();
    unsigned str1_l = str1.length();
    unsigned n = str2_l - str1_l + 1;   
    int str1_c[4];
    str1_c[0] = 0;
    str1_c[1] = 0;
    str1_c[2] = 0;
    str1_c[3] = 0;
    int x[3][4][n];
    int str1_h[3][4];       
    for (unsigned j = 0; j < 4; j++) {
        str1_h[0][j] = bar1(str1_c[j]);
        str1_h[1][j] = bar2(str1_c[j]);
        str1_h[2][j] = bar3(str1_c[j]);
        for (unsigned i = 0; i < n; i++) {
            x[0][j][i] = bar1(0);
            x[1][j][i] = bar2(0);
            x[2][j][i] = bar3(0);
        }
    }   
}
int bar1(int x) {
    return 0;
}
int bar2(int x) {
    return 0;
}
int bar3(int x) {
    return 0;
}
RE60K
  • 621
  • 1
  • 7
  • 26
  • is your algorithm recursive? crash during a call could mean stack overflow. – Jean-François Fabre Sep 30 '16 at 12:57
  • 1
    Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. Or at least show us relevant declarations and initializations, and the actual values of involved variables (like `j`). – Some programmer dude Sep 30 '16 at 12:57
  • can you step into `division` (put a breakpoint in the first line of it) – ratchet freak Sep 30 '16 at 12:58
  • Also note that non-pointer values can't be "null", so comparing them to that is pointless. – Some programmer dude Sep 30 '16 at 12:58
  • @Jean-FrançoisFabre it is not recursive – RE60K Sep 30 '16 at 12:59
  • @ratchetfreak i can't i recieve SIGSEV upon entering – RE60K Sep 30 '16 at 13:00
  • @JoachimPileborg please see variable declarations – RE60K Sep 30 '16 at 13:04
  • _Sorry I can't provide SSCCE due to complexity of the code_ So manufacture it, in a way that it reproduces the error? You are likely to find the problem, on your own, when doing so. – Algirdas Preidžius Sep 30 '16 at 13:19
  • @AlgirdasPreidžius there is the SSCCE – RE60K Sep 30 '16 at 13:25
  • Two things: First you should read ["Why is iostream::eof inside a loop condition considered wrong?"](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Then you should know that C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) which makes the definition of `x` technically wrong. – Some programmer dude Sep 30 '16 at 13:36

1 Answers1

1

You cannot have a dynamic array on the stack in C++:

int x[3][4][n];

either use new, or use an STL container (e.g. std::vector). See also How do I declare a 2d array in C++ using new?

Your array x is on the stack, thus writing to this array will likely overwrite important values on the stack (e.g. return pointers etc.)

Community
  • 1
  • 1
Martin Nyolt
  • 4,463
  • 3
  • 28
  • 36