简体   繁体   English

堆上链表结构的C数组

[英]C Array of Linked List Struct on Heap

typedef struct cache_line {
    char valid;
    mem_addr_t tag;
    struct cache_line* next;
} cache_line_t;

typedef cache_line_t* cache_set_t;
typedef cache_set_t* cache_t;


/* The cache we are simulating */
cache_t cache;


void initCache()
{
        cache = malloc (S * sizeof(cache_set_t));
        for (int i = 0; i < S; i ++ ){
                cache[i]= malloc(sizeof(cache_line_t));
                cache_line_t *temp = *(cache+i);

                temp -> valid = 0;
                temp -> tag = 0;
                cache_line_t* curr = *(cache+ i );
                for (int j = 1; j < E; j++){
                        curr.next = malloc(sizeof(cache_line_t));
                        curr = curr.next;
                        curr.valid=0;
                        curr.tag=0;
                }
                curr.next = NULL;
        }

}

So my head is swimming trying to remember all the details of pointers and structs and I've been up for a while trying to finish this project.所以我的脑袋在游动,试图记住指针和结构的所有细节,我已经有一段时间试图完成这个项目。 What I'm trying to do here is allocate an array of this structure (a linked list) on the heap and I keep having issues with type mismatches and whatnot (sorry if its messy I keep on trying new things and recompiling).我在这里试图做的是在堆上分配一个这个结构的数组(一个链表),我一直遇到类型不匹配和诸如此类的问题(对不起,如果它很混乱,我继续尝试新事物并重新编译)。 Any help on where I'm going wrong would be appreciated.任何有关我出错的地方的帮助将不胜感激。

Well, the fact the code is terribly obfuscated with abuse of typedef probably goes a long way towards both yours and the compilers problems.好吧,代码被typedef滥用严重混淆的事实可能对您和编译器的问题大有帮助。 I wouldn't have a single typedef in this program myself.我自己在这个程序中不会有一个 typedef。 It serves no real abstraction here.它在这里没有真正的抽象。 Here's what I'd suggest (with some omission of error checking):这是我的建议(省略了一些错误检查):

struct cache_line {
    char valid;
    mem_addr_t tag;
    struct cache_line* next;
};

struct cache_line** cache;

void initCache()
{
    cache = malloc (sizeof(*cache) * S);
    for (int i = 0; i < S; i ++ ){
        struct cache_line** curr_p = &cache[i];
        for (int j = 1; j < E; j++){
            *curr_p = malloc(sizeof(**curr_p));
            (*curr_p)->valid = 0;
            (*curr_p)->tag = 0;
            (*curr_p)->next = NULL;
            curr_p = &(*curr_p)->next;
        }
    }
}

Key things to note:需要注意的关键事项:

  1. I removed all the typedefs.我删除了所有的 typedef。 They served no real purpose here but an attempt to save typing.他们在这里没有真正的目的,只是为了节省打字。 And they did it at the cost of code quality.他们以牺牲代码质量为代价。 I also removed it from the struct , since I believe the previous statement applies to it as well.我也从struct删除了它,因为我相信前面的声明也适用于它。

  2. I allocated memory canonically.我规范地分配了内存。 By writing malloc(sizeof(*pointer_variable)) , you allocate enough memory regardless of what pointer_variable points at.通过编写malloc(sizeof(*pointer_variable)) ,无论pointer_variable指向什么,您都可以分配足够的内存。 It's somewhat type agnostic.这有点类型不可知论。

  3. I traverse the linked list with the "link traversal" idiom.我用“链接遍历”习语遍历链表。 Instead of keeping track of the "node", I keep track of the pointer that is pointing at the node.我没有跟踪“节点”,而是跟踪指向节点的指针。 In the beginning it's cache[i] , and at every iteration it becomes the pointer inside the newly allocated node.一开始它是cache[i] ,在每次迭代时它都成为新分配节点内的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM