C - 一行代码正在改变结构

时间:2017-12-17 23:11:16

标签: c pointers memory-management struct memory-address

我的代码中出现了一个重大问题,我现在已经尝试修复了几个小时。

以下代码与我所遇到的问题相关...

方法addBucket:

void addBucket(SPACE * hashmap,char * tempvalue, char * tempkey){
    printf("BEGINNING OF FUNC...\n");

    void *prevadd = hashmap[0];
    char *value = varString(tempvalue);
    char *key = varString(tempkey);
    void *aftadd = hashmap[0];

    printf("BUCKET %s - %s\n",value,key);

    BUCKET *newBucket = malloc(sizeof(BUCKET *));
    fillBucket(value,key,newBucket);
    int hash = hashFunc(key);

    printf("FILL, FULFILLED\n");

    if(!hashmap[hash]){
        hashmap[hash] = malloc(sizeof(BASE*));
        hashmap[hash]->first = NULL;
    }

    ITEM *location;
    location = hashmap[hash]->first;

    //This creates a new item in the list, if there isn't any.
    //It does this by initialising the base, called box.
    if(!location){
        hashmap[hash]->first = (ITEM *) calloc(1,sizeof(ITEM *));
        hashmap[hash]->first->next = NULL;
        hashmap[hash]->first->prev = NULL;
        hashmap[hash]->first->data = newBucket;
    }

        //This instead adds a new item to the list.
    else{
        //This loop reaches the last ITEM in the linked list itself
        while(location->next){
            location = location->next;
        }

        //This initialises the newItem that will be added
        ITEM *newItem = (ITEM *) calloc(1,sizeof(ITEM));
        newItem->next = NULL;
        newItem->data = newBucket;
        newItem->prev = location;
        location->next = newItem;
    }
}

使用的声明结构:

//Declares a struct called BUCKET.
//Serves as the bucket of the hash table.
typedef struct bucket{
    char * value; //The value inputted.
    char * key; //The key to be hashed.
}BUCKET;

//Declares a struct called ITEM.
//Holds the bucket, as well as the address to the next bucket.
//It also holds the address to the previous bucket.
typedef struct item{
    struct bucket * data;
    struct item * next;
    struct item * prev;
}ITEM;


//Declares a struct called BASE.
//Serves as the base node for the linked lists.
//The amount of initialised linked lists is the same as the amount of bases.
typedef struct base{
    struct item * first;
}BASE;

//Declares a struct of an array of BASES, meaning linked lists.
//Essentially defines the size of the hashspace.
typedef BASE *SPACE;

...方法expandHashspace(); :

//Makes the size of the entire hashspace larger.
//Only takes a value larger than the current size due to possible data loss.
SPACE* expandHashspace(SPACE *hashmap, int newSize){
    if(newSize>100 || newSize<hashSpaceSize){
        printf("Exiting...\n");
        return NULL;
    }
    else {
        SPACE *nw = NULL;
        nw = realloc(hashmap, sizeof(SPACE *) * newSize);
        hashmap = nw;
        hashSpaceSize = newSize;
        return hashmap;
    }
}

这里还有initHashmap()方法:

SPACE* hashmapInit(SPACE *hashmap){
    hashmap = calloc(5, sizeof(SPACE *));
    hashSpaceSize = 5;
    return hashmap;
}

我在这里做的是初始化hashmap,添加三个存储桶,扩展hashmap,然后再添加三个存储桶。这里的顺序用更简单的术语表示:

initHashmap();
addBucket(...); x3
expandHashmap();
addBucket(...); x3

但是,在最后一部分,只要我运行一次addBucket,就会出现SIGSEGV错误。通过调试检查,我发现了一些关闭的东西。

您是否看到变量*prevadd*aftadd?我在调试时添加了它们,以查看hashmap [0]的地址发生了什么。这是我的结果图片:

Can you see the problem?

正如你在那里看到的那样,hashmap [0]的地址在这两条char *行中变化很大。具体而言,地址更改发生在char *value行。

请放轻松我,因为我刚刚开始学习C 3个月前,我仍然非常不习惯内存分配。如果错误是明显的,请指出,如果我分配内存或释放它的方式有问题,我很高兴听到它们(我的代码有一个非常重要的heisenbug,我无法解决对于我的生活,但那不是重点。

提前感谢...抱歉所有最近的问题。

更新:忘了添加varString(); ...

char* varString(const char *origString){
    size_t i;
    for(i = 0;origString[(int)i]!='\0';i++){}
    if(origString[i-1]=='\n') i-=2;
    char *newString = malloc(i);
    for(int j = 0; j <= i; j++){
        newString[j] = origString[j];
    }
    newString[i+1] = '\0';
    return newString;
}

1 个答案:

答案 0 :(得分:5)

这不是一个答案,但它需要比评论中更多的格式:

请注意,您正在撰写"Value No. 1"

请注意aftadd的值为0x756c6156

在内存中,假设有一个小端机器,aftadd中的数字布局将为:

0x56 0x61 0x6c 0x75

在ASCII中,这些将是:

'V' 'a' 'l' 'u'

暗示提示。