简体   繁体   English

动态分配的指向结构(链表)的指针数组,以及如何访问每个列表?

[英]Dynamically allocated array of pointers to structs(linked lists), and how to access each list?

I searched quite a bit and I found similar problems but I still couldn't fix my issue.我搜索了很多,我发现了类似的问题,但我仍然无法解决我的问题。 I want to allocate memory for an array of pointers to tables (each table has its own linked list) I hope I explained the idea properly, here's the code:我想为指向表的指针数组分配 memory(每个表都有自己的链表)我希望我正确解释了这个想法,这是代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Meal 
{
    struct Meal* next;
    char* productname;
    int price;
    int quantity;
}Meal, * PMeal;

typedef struct Table //each table is supposed to have its own linked list of meals
{
    PMeal next;
    PMeal prev;
    int tableNumber;
    int cheque; 
}Table;

typedef struct allTables 
{
    int maxoftables;
    Table** tarray;       
    int numberoftables;
}allTables;

This is how I'm trying to dynamically allocate the array of pointers to tables:这就是我尝试动态分配指向表的指针数组的方式:

(I think that this part is correct, it doesn't crash) (我认为这部分是正确的,它不会崩溃)

    allTables tables;
    tables.tarray = (Table**)malloc(sizeof(Table*) * tables.maxoftables)

Note: tables.maxoftables is initialized before the call to malloc, it's the maximum number of tables注意:tables.maxoftables 是在调用 malloc 之前初始化的,是表的最大数量

And this is how I'm trying to initialize the linked lists in each table:这就是我尝试初始化每个表中的链接列表的方式:

(This is where it tells me "Access violation writing location") (这是它告诉我“访问冲突写入位置”的地方)

for (i = 0; i < tables.maxoftables; i++)
            {
                (tables.tarray[i])->cheque = 0;
                (tables.tarray[i])->next = NULL;
                (tables.tarray[i])->prev = NULL;
                (tables.tarray[i])->tableNumber = i + 1;
            }

I believe I could just allocate an array of struct Table but that is not allowed.我相信我可以分配一个结构表数组,但这是不允许的。

I hope everything that is needed for you to help me is here and that it's explained properly我希望您帮助我所需的一切都在这里并且得到了正确的解释

Thanks!谢谢!

Your code sample is nearly complete.您的代码示例已接近完成。 The allocation reserves proper space for N pointers, but what do they point to?分配为 N 个指针保留了适当的空间,但它们指向什么? The memory range from malloc() is uninitialized and could be random/leftover data. malloc() 中的 memory 范围未初始化,可能是随机/剩余数据。 A calloc() will both allocate memory and clear the range to zeros; calloc() 将分配 memory 并将范围清零; the result is that all your table pointers would be NULL.结果是所有表指针都是 NULL。 That also makes it easy to spot bad pointers when you encounter runtime exceptions.这也使得在遇到运行时异常时很容易发现错误的指针。

If calloc() was used and later when.cheque was assigned (or other struct members), the target address when writing to memory would start with the struct pointer (NULL) plus an offset of X bytes for the struct member:如果使用 calloc() 并且稍后分配了 .cheque (或其他结构成员),则写入 memory 时的目标地址将以结构指针(NULL)加上结构成员的 X 字节偏移量开始:

// pseudo code
(NULL + small-byte-offset) <-- 0

The first page of virtual memory can be referred to as "zero page".虚拟memory的第一页可以称为“零页”。 Within your user program, most often zero page is intentionally left unmapped and useful for debugging cases like this.在您的用户程序中,大多数情况下,零页是有意保留为未映射的,并且对于调试此类情况很有用。

While an array of table pointers was sufficiently allocated, they don't point to any valid memory.虽然表指针数组已充分分配,但它们并未指向任何有效的 memory。 You'd also need to allocate.maxoftables structures and reference those with your allocated pointers.您还需要 allocate.maxoftables结构并使用分配的指针引用这些结构。

It's unfortunate that allocating a struct array is somehow disallowed since it's fast/cheap to allocate an array with one request.不幸的是,分配一个结构数组在某种程度上是不允许的,因为用一个请求分配一个数组是快速/便宜的。 Instead, you'd have to resort to inefficient means like this: allocate a Table structure plus check and assign the alloc result for each element of the pointer array.相反,您必须求助于这样的低效方法:分配一个 Table结构加上检查并为指针数组的每个元素分配分配结果。

Side note when allocating with calloc(): since all your struct members will be zero, that leads to a side-benefit where only 1 struct member from your example would need to be initialized (i + 1).使用 calloc() 分配时的旁注:由于您的所有结构成员都将为零,这会带来一个附带好处,即您的示例中只有 1 个结构成员需要初始化 (i + 1)。

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

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