0

I work with VS2012, on a project which combines C and C++ code. This problem does not happen when running the code in GCC.

(See screenshot at the bottom)

The below C function fails on stack overflow, immediately when the function FeedForwardNetwork_new_from_string() is called. The debugger breaks after the call to that function, but before any of that function's code is being executed.

FeedForwardNetwork *FeedForwardNetwork_new_from_file(const char *filePath) {
    char *file_contents;
    long input_file_size;
    FILE *input_file = fopen(filePath, "rb");
    FeedForwardNetwork *ffn;

    if (input_file == NULL) {
        return NULL;
    }
    fseek(input_file, 0, SEEK_END);
    input_file_size = ftell(input_file);
    rewind(input_file);
    file_contents = (char*)malloc((input_file_size + 1) * (sizeof(char)));
    fread(file_contents, sizeof(char), input_file_size, input_file);
    file_contents[input_file_size] = '\0';
    fclose(input_file);

    ffn = FeedForwardNetwork_new_from_string(file_contents);

    free(file_contents);

    return ffn;
}

This is the flow to the function, starting from main():

int main(int argc, char **argv) {
    VideoAnalyzerUtils::getNeuralNetwork();

    return 0;        
}

The function getNeuralNetwork(), which calls the problematic function:

FeedForwardNetwork *VideoAnalyzerUtils::getNeuralNetwork() {
    if (VideoAnalyzerUtils::network_ == NULL) {
        VideoAnalyzerUtils::network_ = FeedForwardNetwork_new_from_file("net.txt");
    }
    return VideoAnalyzerUtils::network_;

}

The implementation of FeedForwardNetworkSettings_new_from_string():

FeedForwardNetworkSettings *FeedForwardNetworkSettings_new_from_string(const char *settingsStr) {
    char buffer[NEURON_STR_READ_BUFFER_SIZE];
    int withBias;
    double trainingRate ;
    double outputLayerTrainingRate;
    int hiddenLayerType;
    int outputLayerType;
    FeedForwardNetworkSettings *settings;

    _Neuron_getValueFromString(settingsStr, "withBias=", buffer);
    if (strcmp(buffer, "") == 0) {
        return NULL;
    }
    withBias = atoi(buffer);
    _Neuron_getValueFromString(settingsStr, "trainingRate=", buffer);
    if (strcmp(buffer, "") == 0) {
        return NULL;
    }
    trainingRate = atof(buffer);
    _Neuron_getValueFromString(settingsStr, "outputLayerTrainingRate=", buffer);
    if (strcmp(buffer, "") == 0) {
        return NULL;
    }
    outputLayerTrainingRate = atof(buffer);
    _Neuron_getValueFromString(settingsStr, "hiddenLayerType=", buffer);
    if (strcmp(buffer, "") == 0) {
        return NULL;
    }
    hiddenLayerType = atoi(buffer);
    _Neuron_getValueFromString(settingsStr, "outputLayerType=", buffer);
    if (strcmp(buffer, "") == 0) {
        return NULL;
    }
    outputLayerType = atoi(buffer);


    settings = (FeedForwardNetworkSettings*)malloc(sizeof(FeedForwardNetworkSettings));
    settings->withBias_ = withBias;
    settings->trainingRate_ = trainingRate;
    settings->outputLayerTrainingRate_ = outputLayerTrainingRate;
    settings->hiddenLayerType_ = (NeuronActivationFunction)hiddenLayerType;
    settings->outputLayerType_ = (NeuronActivationFunction)outputLayerType;

    return settings;
}

Can you please help me to understand why it would happen and how to fix it?

A screenshot of the error (Please use browser's zoom-in to see it better):

Screenshot http://s23.postimg.org/fvtetkjgq/c_error.jpg

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • 1
    You're probably blowing your stack - only way to guess would be to see the called function's code, not the caller's. – Mat Nov 26 '15 at 08:24
  • Do not post images. Post text! – too honest for this site Nov 26 '15 at 08:24
  • 1
    Do you have a large local array somewhere else? Is it possible to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? – Some programmer dude Nov 26 '15 at 08:24
  • I added the whole flow starting from `main()` and until the function. It is very short... – SomethingSomething Nov 26 '15 at 08:28
  • I don't have any local array – SomethingSomething Nov 26 '15 at 08:29
  • Show the `FeedForwardNetwork_new_from_string` function – Jabberwocky Nov 26 '15 at 08:34
  • There are a few things with the code you show that are suspect. The first is that you open a file in binary mode, the you terminate the data you read as a string and call a function that contains the word `string` in it's name, indicating it works on strings/text. Is the data binary or textual? Secondly, you don't seem to have any error handling. What if the memory allocation fails? Or if the `fopen` call fails? (The last is not unusual when running in an IDE because the programs working directory might not be what you expect). – Some programmer dude Nov 26 '15 at 08:36
  • NEWS - Checked now - the function `FeedForwardNetwork_new_from_string()` crashes anyway on same issue, even when I call it from main directly. Will now add its implementation – SomethingSomething Nov 26 '15 at 08:38
  • Now the big question, what is `NEURON_STR_READ_BUFFER_SIZE` and what is it defined to? – Some programmer dude Nov 26 '15 at 08:42
  • On an unrelated note, don't use symbol names with a leading underscore followed by an upper-case letter, those names are reserved for the implementation (compiler/standard library) in all scopes. Also, if you are really programming in C you [should not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (or other functions returning `void *`). – Some programmer dude Nov 26 '15 at 08:43
  • baaaaaaa... It's `#define NEURON_STR_READ_BUFFER_SIZE 1048576` !! – SomethingSomething Nov 26 '15 at 08:44
  • Now everything gets clear. I'll change it to malloc – SomethingSomething Nov 26 '15 at 08:44
  • @SomethingSomething you write _I don't have any local array_. Yes you do in the `*FeedForwardNetworkSettings_new_from_string` function: `char buffer[NEURON_STR_READ_BUFFER_SIZE];` : this is a local array and `[NEURON_STR_READ_BUFFER_SIZE` is probably very large. – Jabberwocky Nov 26 '15 at 10:10

1 Answers1

1

You hit the 1 Mb default stack size of the Visual Studio compiler. gcc has 8 Mb default stack which is why it works with gcc.

You can either allocate the buffer dynamically, using mallocor calloc, or increase the stack size.

Visual Studio accepts the following flags to set the stack size:

/STACK:reserve[,commit]

or

/F stack-size-in-bytes

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82