【发布时间】: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;
}
您能帮我理解为什么会发生这种情况以及如何解决它吗?
错误截图(请使用浏览器放大查看):
【问题讨论】:
-
您可能正在炸毁您的堆栈 - 猜测的唯一方法是查看被调用函数的代码,而不是调用者的代码。
-
请勿发布图片。发布文字!
-
您在其他地方有大型本地阵列吗?是否可以创建Minimal, Complete, and Verifiable Example 并展示给我们?
-
我添加了从
main()开始一直到函数的整个流程。很短…… -
我没有任何本地数组
标签: c visual-studio-2012 visual-c++