【问题标题】:finding a dangling pointer找到一个悬空指针
【发布时间】:2014-09-03 17:12:55
【问题描述】:

我的代码有问题。我收到分段错误错误,我理解这是一个悬空指针问题(通常)或内存分配错误。编译器没有显示问题可能在哪一行,所以我的问题是如何检测这些问题以进一步关注?我的问题在哪里? 这是我的代码:

`#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
#define ALPHABET_SIZE (256)
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
#define LEVELS 255

// trie node
struct n
{
    char value,level,isLeaf;
    struct n* children[ALPHABET_SIZE];
    struct n* failLink;
};
typedef struct n node;
//trie
struct t
{
    node *root;
    int count;
};
typedef struct t trie;

void bytesCpy(char *to, char *from, int len)
{
    int i;
    for(i=0;i<len;i++)
    {
        to[i]=from[i];
    }
}

// Returns new trie node (initialized to NULLs)
node *getNode(trie *t, char value,char level)
{
    node *pNode = NULL;
    pNode = (node *)malloc(sizeof(node));
    if (pNode)
    {
        printf("ok\n");
        int i;
        for (i = 0; i < ALPHABET_SIZE; i++)
        {
            pNode->children[i] = NULL;
        }
        pNode->failLink = t->root;
        pNode->value=value;
        pNode->level=level;
        pNode->isLeaf=0;
    }
    else
        printf("error\n");
    return pNode;
}
// Initializes trie (root is dummy node)
void initialize(trie *t)
{
    t->root = getNode(t, '[', 0);
    //t->count = 0;
}
// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
void insert(trie *t, char key[], int len)
{
    int level;
    char value;
    node *node = t->root;

    for (level = 0; level<len; level++)
    {
        value = key[level];
        printf("value: %c\n",value);
        if (node->children[value] == NULL)
        {
            node->children[value] = getNode(t, value, level+1);
        }
        node = node->children[value];
    }
    node->isLeaf=1;

}
// Returns non zero, if key presents in trie
int search(trie *t, char key[])
{
    int level;
    int length = strlen(key);
    int value;
    node *node;
    node = t->root;
    for (level = 0; level < length; level++)
    {
        value =  key[level];//CHAR_TO_INDEX(key[level]);
        if (!node->children[value])
        {
            node = node->failLink;
            return 0;
        }
        node = node->children[value];
    }
    return (0 != node);// && node->value);
}

void search1(trie *t, char *c, int len)
{
    node *curNode = t->root;
    int i;

    for(i=0; i<=len; i++)
    {
        printf("i=%d curnode=%p\n",i,curNode);
        if(curNode->isLeaf) //leaf: cuvant gasit
        {
            printf("if1 curGasit \n");
            do{
                curNode=curNode->failLink;
                if(curNode->isLeaf)
                    printf("if1 curGasit \n");
                else break;
            }while(1);
            continue;
        }
        else //nu e gasit inca
        {
            if(curNode->children[c[i]]==NULL) //fail
            {
                printf("if2\n");
                curNode = curNode->failLink;
                continue;
            }
            else //litera gasita: go on
            {
                printf("el2\n");
                curNode=curNode->children[c[i]];
            }
        }
    }
    printf("end of search\n");
}

node* searchAux(trie *t, node *curRoot, char cuv[], char len, int level ,int failLevel)
{
    char cuvAux[1024];
    bytesCpy(cuvAux,cuv,len);


    printf("searchAux level:%d cuvAux:%s curRootLevel:%d\n",level,cuvAux,curRoot->level);
    if(cuvAux[level+1] == '\0')  //got to the end of cuvAux
    {
        printf("1st if\n");
        return curRoot;
    }

    if(curRoot->children[cuvAux[level+1]] == NULL)   //fail: letter not found
    {
        printf("3rd if\n");
        return searchAux(t, t->root, &cuvAux[failLevel+1], len, 0, failLevel+1);
    }
    else                          //letter found: go on
    {
        printf("3rd else\n");
        if(cuvAux[level+2] == '\0') //the found letter was the last of the string
        {
            printf("4th if\n");
            return curRoot->children[cuvAux[level+1]]; //return final pointer
        }
        else //the found letter was not the last of the string: continue with the next one
        {
            printf("4th else\n");
            return searchAux(t, curRoot->children[cuvAux[level+1]], cuvAux, len, level+1, failLevel);
        }
    }
}

void createFailLinks(trie *t, node* curRoot, char cuv[], int level)
{
    int i;
    char cuvAux[1024];

    bytesCpy(cuvAux,cuv,1024);

    if(curRoot == NULL)
        return;

    for(i=0;i<ALPHABET_SIZE/*curRoot->children[i] != NULL*/;i++)
    {
        if(curRoot->children[i] == NULL)
            continue;
        else
        {
            cuvAux[level] = curRoot->children[i]->value;
            printf("createFailLinks  %c%d\n",cuvAux[level],curRoot->children[i]->level);
            curRoot->children[i]->failLink = searchAux(t, t->root, cuvAux, level+1, 0, 0);
            createFailLinks(t,curRoot->children[i],cuvAux,level+1);
        }
    }
    printf("got\n");

}

void printTrie(node *curRoot)
{
    int i;

    if(curRoot == NULL)
        return;

    printf("%c: ", curRoot->value);

    for(i=0;i<ALPHABET_SIZE;i++)
        if(curRoot->children[i] != NULL)
        {
            printf("%c ", i);
        }
    printf("\n");

    for(i=0;i<ALPHABET_SIZE;i++)
        if(curRoot->children[i] != NULL)
        {
            printTrie(curRoot->children[i]);
        }
}

void checkLinks(node* curRoot)
{
    int i;
    if(curRoot == NULL)
        return;
    printf("node %c%d: ",curRoot->value,curRoot->level);
    for(i=0;i<256;i++)
        if(curRoot->children[i] != NULL)
            printf("\n\t%c%d:%c%d",curRoot->children[i]->value, curRoot->children[i]->level, curRoot->children[i]->failLink->value,curRoot->children[i]->failLink->level);
    printf("\n");
    for(i=0;i<256;i++)
        if(curRoot->children[i] != NULL)
            checkLinks(curRoot->children[i]);
}

int mai()
{
    FILE *fd = fopen("VirusDatabase.txt","r");//O_RDONLY);
    int i;
    char c;


    for(i=0;i<1000;i++)
    {
        fscanf(fd, "%c", &c);

        printf("%c",c);
    }
}

int main()
{
    // Input keys (use only 'a' through 'z' and lower case)
    char keys[][1024] = { "he", "she", "her", "his", "heres"};
    char cuv[] = {'\0','\0','\0','\0','\0','\0'};
    trie t;
    char output[][32] = { "Not present in trie", "Present in trie" };
    int i;
    char text[]={"andreiherutshevlastashecristihiskatjaheres"};

    initialize(&t);


    // Construct trie
    for (i = 0; i < ARRAY_SIZE(keys); i++)
    {
        insert(&t, keys[i], strlen(keys[i]));
    }

    createFailLinks(&t, t.root, cuv, 0);


    printTrie(t.root);
    printf("\n\n");

    checkLinks(t.root);
    search1(&t, text, strlen(text));

    return 0;
    // Search for different keys
    printf("%s --- %s\n", "abcd", output[search(&t, "abcd")]);
    printf("%s --- %s\n", "ab", output[search(&t, "ab")]);
    printf("%s --- %s\n", "ccdd", output[search(&t, "ccdd")]);
    printf("%s --- %s\n", "thaw", output[search(&t, "thaw")]);
    return 0;
    char a = getchar();
}`

【问题讨论】:

  • 一个好的调试器会告诉你错误发生在哪里
  • 你心里有数吗?
  • 依赖,在 Windows Visual Studio 中(Express 可以正常工作)或任何 Light IDE,例如 Code::Block , Eclipse 等...与 MinGW(和 GDB)。在 Linux 中,肯定是 QtCreator 或 Eclipse(和 GDB),在 Mac 中,是 Xcode(带有 LLDB)。可以从控制台使用 GDB,但一开始不推荐。
  • value = key[level]; printf("value: %c\n",value); if (node-&gt;children[value] == NULL) 好吗?看起来代码应该是children[CHAR_TO_INDEX(value)] == NULL 这里和其他地方。顺便说一句:简化:#define CHAR_TO_INDEX(c) ((int)c - (int)'a') --> #define CHAR_TO_INDEX(c) ((int)c - 'a').

标签: c pointers memory struct


【解决方案1】:

您可以使用调试器吗?我在调试器中运行了您的代码,并在此处的第 157 行遇到内存访问冲突:

return searchAux(t, t->root, &cuvAux[failLevel+1], len, 0, failLevel+1);

您似乎在递归调用 searchAux。即你有:

node* searchAux(trie *t, node *curRoot, char cuv[], char len, int level ,int failLevel)
{
  char cuvAux[1024];
  ...
  return searchAux(t, t->root, &cuvAux[failLevel+1], len, 0, failLevel+1);
  ...

无论如何,最终缓冲区大小变量 failLevel 超过了缓冲区的大小,因此您尝试访问数组边界之外的内存,这就是您遇到访问冲突的原因。

最简单的调试方法是使用交互式调试器。在 Windows 上,有一个免费版本的 Visual Studio,它带有一个非常好的调试器。在 linux 上你可以使用 GDB。

无法嵌入打印语句以在崩溃前打印出变量。

【讨论】:

    【解决方案2】:

    您可以在代码行中添加打印语句。

        #include <iostream>
    
        std::cout << "At Line: " << __LINE__ << endl;
    

    将它放在不同的代码行中,您可以查看执行了哪些行,并找到它崩溃的位置。

    这是针对 C++ 的。我的错。相同的想法,但放置 printf() 语句并查看它停止执行的位置以缩小崩溃位置。

    【讨论】:

    • 显然问题被标记为 C 而不是 C++
    • 我的错。相同的想法,但使用 printf()。
    • “所以我的问题是如何检测这些问题以便进一步关注?”我想我在这方面有所帮助......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 2021-12-18
    • 2011-07-14
    相关资源
    最近更新 更多