C调用另一个函数时出现奇怪的堆栈溢出

时间:2015-11-26 08:20:48

标签: c visual-studio-2012 visual-c++

我使用VS2012,在一个结合了C和C ++代码的项目上。在GCC中运行代码时不会发生此问题。

(见底部的截图)

当调用函数FeedForwardNetwork_new_from_string()时,下面的C函数在堆栈溢出时失败,立即。在调用该函数之后,调试器会中断,但在执行任何该函数的代码之前。

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;
}

这是从main()开始的函数流程:

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

    return 0;        
}

函数getNeuralNetwork(),它调用有问题的函数:

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

}

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;
}

你能帮助我理解它为什么会发生以及如何解决它吗?

错误的屏幕截图(请使用浏览器放大以更好地查看):

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

1 个答案:

答案 0 :(得分:1)

您达到了Visual Studio编译器的1 Mb默认堆栈大小。 gcc具有8 Mb默认堆栈,这就是它与gcc一起使用的原因。

您可以使用malloccalloc动态分配缓冲区,也可以增加堆栈大小。

Visual Studio接受以下标志来设置堆栈大小:

/STACK:reserve[,commit]

/F stack-size-in-bytes