【问题标题】:C Strange stack-overflow when calling another function [duplicate]调用另一个函数时C奇怪的堆栈溢出[重复]
【发布时间】:2015-11-26 08:20:48
【问题描述】:

我在一个结合了 C 和 C++ 代码的项目中使用 VS2012。在 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

【问题讨论】:

  • 您可能正在炸毁您的堆栈 - 猜测的唯一方法是查看被调用函数的代码,而不是调用者的代码。
  • 请勿发布图片。发布文字!
  • 您在其他地方有大型本地阵列吗?是否可以创建Minimal, Complete, and Verifiable Example 并展示给我们?
  • 我添加了从main() 开始一直到函数的整个流程。很短……
  • 我没有任何本地数组

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


【解决方案1】:

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

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

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

/STACK:reserve[,commit]

/F stack-size-in-bytes

【讨论】:

    猜你喜欢
    • 2012-02-14
    • 2011-03-03
    • 2019-08-07
    • 2020-11-14
    • 1970-01-01
    • 2020-12-03
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多