简体   繁体   English

链表初始化数组

[英]array of linked lists initialization

Before I get downvoted for not looking at "similar" questions, I couldn't find anyone who actually tried to initialize an entire malloced "array" to NULL. 在我因不看“相似”问题而down之以鼻之前,我找不到真正试图将整个分配的“数组”初始化为NULL的人。

I'm trying to create a hash table. 我正在尝试创建一个哈希表。 The first malloc is for the array, the second malloc is for creating the hash table struct to put the array in. I plan to implement collision chaining with a linked list (called ListNode here). 第一个malloc用于数组,第二个malloc用于创建哈希表结构以将数组放入其中。我计划使用链接列表(在此称为ListNode)实现冲突链接。 After mallocing space for the array and putting it in the struct, I'd like to initialize all linked lists inside the array to NULL. 为数组分配空间并将其放入结构后,我想将数组内的所有链接列表初始化为NULL。

As of right now, this code assigns the POINTERS to the linked lists to NULL (on accident...I don't know how to fix), so they point to memory position 0x0. 截至目前,此代码将指向链表的POINTERS分配为NULL(偶然...我不知道如何解决),因此它们指向内存位置0x0。 And somehow, after 3 or so iterations through that for loop at the bottom, the entire hash table struct is now pointing to 0x0 and I get a seg fault. 并且以某种方式,经过底部的for循环大约3次迭代之后,整个哈希表结构现在都指向0x0,并且出现了段错误。

How are my all of my pointers suddenly becoming null, and not just my linked lists? 如何使我所有的指针突然变成空值,而不仅仅是链接列表? And how do I make it so that the ListNode pointer = NULL so I can perform normal linked list operations on them? 以及如何使ListNode指针= NULL,以便可以对它们执行常规的链表操作?

typedef struct
{
    ListNode **array;
    /* more stuff */
} HTable;

void *HTCreate(unsigned sizes[], int numSizes){
  arr = (ListNode*)malloc(sizeof(ListNode) * sizes[0]);

  if(arr == NULL)
  {
      exit(1);
  }

  ht = (HTable*)malloc(sizeof(HTable));
  ht->array = &arr;
  ht->sizes = size;
  /* more initializing */
  for(i = 0; i < ht->sizes[ht->sizeIndex]; i++)
  {
      ht->array[i] = NULL;
  }

return (void *)ht; }

I have some theories...it's might be a problem with the &arr and me confusing things with double pointers, but I've tried this with both double pointers and single pointers in the struct, so I am thoroughly lost. 我有一些理论...这可能是&arr的问题,我将双指针混淆了,但是我在结构中同时使用双指针和单指针进行了尝试,所以我彻底迷失了。

Thanks in advance! 提前致谢!

With the code you show, the only valid index of ht->array is 0 . 使用您显示的代码, ht->array的唯一有效索引是0 Everything else will be out of bounds. 其他一切都将超出范围。

If you want an array of pointers, you need to allocate such an array: 如果您想要一个指针数组,则需要分配这样一个数组:

// Allocate an array of pointers, each pointer being initialized to NULL
ht->array = calloc(sizes[0], sizeof(ListNode *));

You have a dangling pointer in your code, unless all your variable are global ones which would be bad design. 除非所有变量都是全局变量,否则在代码中都将有一个悬空指针,这将是不好的设计。

HTable *ht = malloc(sizeof(HTable));
...
return (void *)ht;

This one is fine: you return a pointer to allocated memory (note don't cast malloc in C ) 这很好:您返回一个指向已分配内存的指针(注意不要在C中强制转换malloc

But this one is wrong: 但这是错误的:

(ListNode*) arr = malloc(sizeof(ListNode) * sizes[0]);
...
ht->array = &arr;  

arr is a local pointer to an allocated memory bloc. arr是指向已分配内存块的本地指针。 You can safely return the address of the bloc ( arr ), but not the address of a local pointer to it ( &arr ). 您可以安全地返回bloc( arr )的地址,但不能返回指向该bloc的本地指针( &arr )的地址。

Unless you have a good reason to do so, you should remove one indirection level: 除非有充分的理由,否则应删除一个间接级别:

typedef struct
{
    ListNode *array;
    /* more stuff */
} HTable;

and then 接着

ht->array = arr;               // fine the address of an allocated bloc

If you really need the two indirection level, you must use a dynamically allocate pointer: 如果您确实需要两个间接级别,则必须使用动态分配指针:

(ListNode*) arr = malloc(sizeof(ListNode) * sizes[0]);
...
(ListNode **)parr = malloc(sizeof(ListNode **));
ht->array = parr;              // fine the address of an allocated pointer

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

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