我对一个我无法克服的错误感到非常绝望。
对于我在大学的 C 编程课,我必须为 GML(图形建模语言)输入流实现解析器。
成功时,解析器向调用者返回一个抽象数据类型,它是一个作为图形表示的邻接矩阵。
好的,解析器完美地工作,在过去的几天里让我感到绝望不会有问题。在解析器中,有一个函数调用又调用 malloc。malloc 在扫描器将逐个符号传递给解析器期间经常被调用。但是在离开扫描程序例程之前,总是通过调用 free() 来释放 malloc 的内存块。
但是在解析器的深处有一个致命的函数调用,它又调用了一个函数,该函数使用 malloc 保留 12 个字节的内存(三个整数属性)以保存结构。需要该结构来存储有关图中单个边的信息(源节点、目标节点、权重)。
该调用进行了两次。第一次,一切顺利。然后,由于根据 gml 语法可能出现 1 到 n 条边,代码进入一个 while 循环,只要在输入流中找到边,相同的指针就会被分配一个指向新边结构的指针。循环内的边缘识别例程的第一次调用,总共是第二次(第一次发生在进入循环之前,参见 ma),由于 malloc 返回 NULL 而不断失败。
我根本不知道为什么。
这与内存短缺问题无关,因为当我在该程序的 main() 函数中 malloc 1000 字节时,只是为了好玩,它工作正常。
我使用 Code::Blocks 和 DevCPP 作为 IDE。在这两种情况下,程序都会遇到同样的问题。
这是我的主要解析例程:
DirectedGraph Graph(char* sourceString, int*currentPosition){
int sym;
int restartPosition = 0;
int* backupPosition;
char* backupString;
int nodeCount = 0;
int currentSrc = -1;
int currentTgt = -1;
int currentWgt = -1;
EdgeDescription e;
DirectedGraph correctMatrix;
MatrixStruct* errorMatrix = NULL;
/*begin parsing*/
bool isGraphHeader = GraphHdr(sourceString, currentPosition);
if(isGraphHeader == true){
bool isNode = Node(sourceString, currentPosition);
if(isNode == true){
while(isNode == true){
nodeCount++;
restartPosition = *currentPosition;
isNode = Node(sourceString, currentPosition);
}
*currentPosition = restartPosition;
/*now get edge information (from-to-weight)*/
/*as we have already read the next symbol, we have to reset*/
/*our read position by one symbol backwards*/
e = Edge(sourceString, &restartPosition); /*<======== HERE I CALL THE FATAL ROUTINE FOR THE FIRST TIME - EVERYTHING´s JUST FINE, PROGRAM PROCEEDS*/
restartPosition = 0;
/*just for clearer coding in if statement*/
currentSrc = e->source;
currentTgt = e->target;
currentWgt = e->weight;
destroyEdge(e);
if(currentSrc != -1 && currentTgt != -1 && currentWgt != -1){
/*initialize matrix with counted number of nodes*/
correctMatrix = CreateNewGraph(nodeCount);
/*the edge is inserted only when it lies within the boundaries*/
/*of our graph. but we do not interrupt the whole processing, we just skip it.*/
while(currentSrc != -1 && currentTgt != -1 && currentWgt != -1){
if(currentSrc <= nodeCount && currentTgt <= nodeCount){
InsertEdge(correctMatrix, currentSrc, currentTgt, currentWgt);
restartPosition = *currentPosition;
}
e = Edge(sourceString, currentPosition); /* <============== THIS IS THE CALL THAT FAILS*/
currentSrc = e->source;
currentTgt = e->target;
currentWgt = e->weight;
}
/*as we have read over the next symbol in the loop, reset the position to read*/
*currentPosition = *currentPosition - 1;
sym = GetNextSymbol(sourceString,currentPosition);
if(sym == rightBrace){
sym = GetNextSymbol(sourceString, currentPosition);
if(sym == eot){
return correctMatrix;
}
else{
return errorMatrix;
}
}
else{
return errorMatrix;
}
}
else{
return errorMatrix;
}
}
else{
return errorMatrix;
}
}
else{
return errorMatrix;
}
}
这里是 GetNextSymbol(扫描仪,将符号传递给解析器):
/**
* DOCUMENTATION
* ============================
* This is the main scanning function
* which is used by the parser to recognize
* terminal symbols and valid literals.
*
* RETURNS: the enum code for the recognized symbol.
* or an error code, when invalid symbol encountered.
*/
int GetNextSymbol(char* sourceString, int* currentPosition){
int symbolCode;
int loopCounter = 0;
char* currentIdentifier = (char*)malloc(10);
char* currentNumber = (char*)malloc(10);
int identifierPosition = 0;
int numberPos = 0;
int numericVal = 0;
char currentChar;
currentChar = getNextChar(sourceString, currentPosition);
/*skip all blanks, empty chars,
linefeeds, carriage returns*/
while(currentChar == ' '
|| currentChar == 11
|| currentChar == 10
|| currentChar == 13
|| currentChar == '\t')
{
currentChar = getNextChar(sourceString, currentPosition);
}
/*=====================================*/
/*Section 1: scan for terminal symbols */
/*====================================*/
if(currentChar == '['){
symbolCode = leftBrace;
}
else if(currentChar == ']'){
symbolCode = rightBrace;
}
/*=====================================*/
/*Section 2: scan for valid literals */
/*====================================*/
else if(isdigit(currentChar)){
/*here we calculate the numeric value of a number expression*/
/*when calculated, we assign the numeric value to the symCode variable*/
/*this works out because the values for a real symbol are always negative*/
symbolCode = digit;
while(isdigit(currentChar)){
currentNumber[numberPos] = currentChar;
currentChar = getNextChar(sourceString, currentPosition);
loopCounter++;
numberPos++;
}
currentNumber[numberPos] = '\0';
numericVal = atoi(currentNumber);
symbolCode = numericVal;
/*when identifier or braces follow number without space: reset currentPos*/
/*to the position of the previous char*/
if(isalpha(currentChar)){
*currentPosition = *currentPosition - loopCounter;
}
else if(currentChar == ']'){
*currentPosition = *currentPosition - loopCounter;
}
else if(currentChar == '['){
*currentPosition = *currentPosition - loopCounter;
}
}
else if(isalpha(currentChar)){
while(isalpha(currentChar)){
currentIdentifier[identifierPosition] = currentChar;
currentChar = getNextChar(sourceString, currentPosition);
loopCounter++;
identifierPosition++;
}
/*check wether we have found a valid identifying label*/
/*and deallocate the reserved mem space*/
currentIdentifier[identifierPosition] = '\0';
symbolCode = recognizeIdentifier(currentIdentifier);
/*when number or braces follow identifier without space: reset currentPos*/
/*to the position of the previous char*/
if(isdigit(currentChar)){
*currentPosition = *currentPosition - 1;
}
else if(currentChar == ']'){
*currentPosition = *currentPosition - 1;
}
else if(currentChar == '['){
*currentPosition = *currentPosition - 1;
}
}
else if(currentChar=='\0'){
symbolCode = eot;
}
/*neither terminal symbol nor end of text found on current position --> illegal symbol*/
else{
symbolCode = error;
}
free(currentIdentifier);
free(currentNumber);
return symbolCode;
}
现在是“边缘”识别程序中的致命呼叫。首先,结构的标头
#ifndef GML_EDGE_STRUCT_H_INCLUDED
#define GML_EDGE_STRUCT_H_INCLUDED
typedef struct EdgeStruct* EdgeObj;
typedef struct EdgeStruct {
int source;
int target;
int weight;
} EdgeStruct;
typedef EdgeObj EdgeDescription;
EdgeDescription createNewEdge(int src, int tgt, int wgt);
void destroyEdge(EdgeObj);
#endif // GML_EDGE_STRUCT_H_INCLUDED
实施
#include "GML_EDGE_STRUCT.h"
#include <stdio.h>
#include <stdlib.h>
EdgeDescription createNewEdge(int source, int target, int weight){
EdgeDescription e;
int bytesRequested = sizeof(EdgeStruct);
e = malloc(bytesRequested);
e->source = source;
e->target = target;
e->weight = weight;
return e;
}
我知道,这几乎是代码;)只是为了表明,所有可以释放的东西,我都释放了。
在过去的两天里,我在谷歌上搜索了我的问题,当然也在这里堆栈溢出,并且有数百个网站、帖子等关于 malloc 返回 null 的问题。他们都说基本相同:没有足够的内存(也就是说,我们称之为不太可能),或者碎片堆,所以没有足够大小的内存块可用。
但是:我要求的只是 12 个(字面意思是:十二个)字节来存储三个 int 属性。这似乎太多了。
我是否超出了一些我不知道的内部限制?
帮助将不胜感激。
提前感谢罗兰
编辑 2012-11-24:
谢谢你的回答。但。这个问题必须具有更基本的性质。
因为:当我测试我的程序的其他部分(文件 I/O)等时,它们远没有解析器复杂,并且只从 main() 深入调用一次,我也不能 malloc。我读的文件大约有 140 个字节。即使我测试与所有其他部分隔离的 I/O 部分,即使我将它们外包到不同的项目中,我也没有从系统中获得内存。绝不。我已经重新启动计算机,一切。绝对地。不。改变。
有任何想法吗?与此同时,我在这个项目上投入了太多时间,其中大部分时间都在跟踪那些该死的内存错误...... :-(((