C - 一行代码正在更改结构的地址

标签 c pointers memory-management struct memory-address

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

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

添加桶的方法:

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?

如您所见,在这两行 char * 中,hashmap[0] 的地址变化很大。具体来说,地址更改发生在 char *value 行。

请放轻松,因为我 3 个月前才开始学习 C,而且我仍然非常不习惯内存分配。如果错误很明显,请指出,如果我在分配内存或释放内存的方式上有问题,我很高兴听到他们的消息(我的代码有一个我无法修复的非常严重的 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;
}

最佳答案

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

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

注意 aftadd 的值为 0x756c6156

在内存中,假设是小端机器,aftadd 中数字的布局将是:

0x56 0x61 0x6c 0x75

在 ASCII 中,这些将是:

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

提示提示。

关于C - 一行代码正在更改结构的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47860356/

相关文章:

c - 段错误问题

c++ - C和C++中的main有什么区别

C 每个案例后的 Switch-case 花括号

将 **array + a 更改为 *array[a]

java - 什么是类加载器泄漏?

cocoa - Coredata、EXC_BAD_ACCESS 或相关对象上的奇怪行为

c - memcpy 复制了错误的字节数

c++ - VC++中通过A类函数指针调用B类成员函数

pointers - 为什么从双向链表中删除节点比从单链表中删除节点快?

c++ - 如何阻止这种内存泄漏