找到一个悬垂的指针

时间:2014-09-03 17:12:55

标签: c pointers memory struct

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

`#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();
}`

2 个答案:

答案 0 :(得分:2)

您是否可以访问调试器?我在调试器中运行您的代码并在第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。

如果没有,你可以嵌入print语句在崩溃前打印出变量。

答案 1 :(得分:0)

您可以在代码行添加print语句。

    #include <iostream>

    std::cout << "At Line: " << __LINE__ << endl;

将它放在各种代码行中,你可以看到哪些行被执行,并找到它崩溃的地方。

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