简体   繁体   English

需要一些帮助来理解C中的指针和内存

[英]Need some help understanding pointers and memory in C

I'm writing a bit of code for a class, but since I have no experience in C I'm a bit unsure of what the code I've written actually does. 我正在为一个类编写一些代码,但是由于我没有C方面的经验,因此我不确定我编写的代码实际上是做什么的。 Particularly what the memory looks like. 特别是存储器的外观。 Here's the relevant bits: 这是相关的位:

typedef struct listnode *Node;
typedef struct listnode {
    void *data;
    Node next;
    Node previous;
} Listnode;

typedef struct listhead *LIST;
typedef struct listhead {
    int size;
    Node first;
    Node last;
    Node current;
} Listhead;

#define HALLOCSIZE 50

static LIST hallocbuf[HALLOCSIZE];
static LIST *hallocp = hallocbuf;

LIST *CreateList()
{
    if(hallocbuf + HALLOCSIZE - hallocp >= 1)
    {
        LIST temp;
        temp->size = 0;
        temp->first = NULL;
        temp->last = NULL;
        temp->current = NULL;

        *hallocp = temp;
        return hallocp;

    }else
        return NULL;
}

So my question is, in the CreateList function, how is the program allocating memory for temp? 所以我的问题是,在CreateList函数中,程序如何为temp分配内存? And does the code *hallocp = temp copy the temp LIST into the hallocbuf array? 代码*hallocp = temp将临时列表复制到hallocbuf数组中? I am trying to have all my LIST structs sit in the allocated memory for hallocbuf. 我试图让我所有的LIST结构都位于为hallocbuf分配的内存中。 Is this what I'm doing? 这是我在做什么吗? I'm a bit uncertain of how the typedef, structs and pointers play together. 我不确定typedef,结构和指针如何一起发挥作用。

Thanks! 谢谢!

So my question is, in the CreateList function, how is the program allocating memory for temp? 所以我的问题是,在CreateList函数中,程序如何为temp分配内存?

It isn't, which is a problem. 不是,这是一个问题。 It should do something like this: 应该执行以下操作:

temp = malloc(sizeof(Listhead));

And does the code *hallocp = temp copy the temp LIST into the hallocbuf array? 代码* hallocp = temp是否将临时列表复制到hallocbuf数组中?

It copies the pointer that was saved in temp into the first element of hallocbuf (assuming that hallocp hasn't been changed anywhere and still has the value that it has been initialized to, pointing to hallocbuf[0] ). 它将temp保存的指针复制到hallocbuf的第一个元素中(假设hallocp尚未在任何地方更改,并且仍然具有已初始化的值,指向hallocbuf[0] )。

Generally it's not usually a good idea to hide the fact that LIST and Node are pointers behind typedefs. 通常,隐藏LISTNode是typedef后面的指针这一事实通常不是一个好主意。 It's much clearer where memory needs to be allocated of freed if it's obvious which variables are pointer, and having an explicit * in the variable declaration makes that clear. 如果很明显哪些变量是指针,并且需要在变量声明中使用显式*表示清楚,那么应该在何处分配释放的内存就更加清楚了。

temp is allocated in the space used for objects with "automatic storage duration" - this is usually on a runtime stack, but you don't really need to know the details. temp在具有“自动存储持续时间”的对象的空间中分配-通常在运行时堆栈上,但是您实际上不需要知道详细信息。 The space is deallocated when the block in which it was allocated is exited (in your case, when you hit the return ). 当分配空间所在的块退出时(在您的情况下,当您按下return )将释放空间。

The line *hallocp = temp; *hallocp = temp; does indeed copy the value of temp into the memory that hallocp is pointing at, which is hallocbuf[0] . 确实确实将temp的值复制到hallocp指向的内存中,即hallocbuf[0]

The problem is that temp is just a pointer itself - and it's not pointing at anything. 问题是, temp只是一个指针本身-它不是任何指向。 This is called a "dangling pointer". 这称为“悬空指针”。 This means that when you try to access what it's pointing at, you have an error. 这意味着当您尝试访问它所指向的内容时,会出现错误。 That happens in these lines: 发生在以下几行:

temp->size = 0;
temp->first = NULL;
temp->last = NULL;
temp->current = NULL;

You can't have your structs sit in the memory allocated for hallocbuf , because it doesn't have room for structs - it's just an array of pointers, not an array of structs. 您不能将结构放在hallocbuf分配的内存中,因为它没有足够的结构空间-它只是一个指针数组,而不是结构数组。

If LIST were 如果LIST是

typedef struct listhead LIST;

and you accessed temp 并且您访问了temp

temp.size = 0;
...

then 然后

*hallocp++ = temp;

would use hallocp as a pointer into the hallocbuf buffer and place the newly initialized element there. 将使用hallocp作为指向hallocbuf缓冲区的指针,并将新初始化的元素放在此处。 Not the best way to do it, though. 不过,这并不是最好的方法。 You could replace temp with 您可以将temp替换为

hallocp->size = 0;
...
++hallocp;

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

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