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. Particularly what the memory looks like. Here's the relevant bits:
我正在为一个类编写一些代码,但是由于我没有C方面的经验,我有点不确定我所编写的代码实际上是做什么的。特别是记忆的样子。这是有关:
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? And does the code *hallocp = temp
copy the temp LIST into the hallocbuf array? I am trying to have all my LIST structs sit in the allocated memory for hallocbuf. Is this what I'm doing? I'm a bit uncertain of how the typedef, structs and pointers play together.
我的问题是,在CreateList函数中,程序是如何为temp分配内存的?代码*hallocp = temp是否将临时列表复制到hallocbuf数组中?我试图让我的列表结构都位于hallocbuf的分配内存中。这就是我要做的吗?我有点不确定typedef、struct和指针是如何一起运行的。
Thanks!
谢谢!
3 个解决方案
#1
4
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. 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.
通常,隐藏LIST和Node是typedef后面的指针这一事实并不是个好主意。如果很明显哪些变量是指针,那么需要分配释放的内存的位置就会清楚得多,并且在变量声明中有一个显式的*可以清楚地说明这一点。
#2
3
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. The space is deallocated when the block in which it was allocated is exited (in your case, when you hit the return
).
temp被分配到用于具有“自动存储持续时间”的对象的空间中——这通常是在运行时堆栈上,但是您并不需要知道细节。当分配空间的块退出时(在您的例子中,当您单击return时)释放空间。
The line *hallocp = temp;
does indeed copy the value of temp
into the memory that hallocp
is pointing at, which is hallocbuf[0]
.
行*hallocp = temp;确实将temp的值复制到hallocp指向的内存中,也就是hallocbuf[0]。
The problem is that temp
is just a pointer itself - and it's not pointing at anything. 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只是一个指针本身——它没有指向任何东西。这被称为“悬浮指针”。这意味着当你试图访问它指向的内容时,你会有一个错误。这发生在以下几行:
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分配的内存中,因为它没有为结构体预留空间——它只是一个指针数组,而不是一个结构体数组。
#3
0
If LIST were
如果列表
typedef struct listhead LIST;
and you accessed temp
你临时访问
temp.size = 0;
...
then
然后
*hallocp++ = temp;
would use hallocp as a pointer into the hallocbuf buffer and place the newly initialized element there. Not the best way to do it, though. You could replace temp with
将使用hallocp作为指针进入hallocbuf缓冲区,并将新初始化的元素放在那里。但这并不是最好的方法。你可以用它来代替临时工
hallocp->size = 0;
...
++hallocp;
#1
4
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. 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.
通常,隐藏LIST和Node是typedef后面的指针这一事实并不是个好主意。如果很明显哪些变量是指针,那么需要分配释放的内存的位置就会清楚得多,并且在变量声明中有一个显式的*可以清楚地说明这一点。
#2
3
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. The space is deallocated when the block in which it was allocated is exited (in your case, when you hit the return
).
temp被分配到用于具有“自动存储持续时间”的对象的空间中——这通常是在运行时堆栈上,但是您并不需要知道细节。当分配空间的块退出时(在您的例子中,当您单击return时)释放空间。
The line *hallocp = temp;
does indeed copy the value of temp
into the memory that hallocp
is pointing at, which is hallocbuf[0]
.
行*hallocp = temp;确实将temp的值复制到hallocp指向的内存中,也就是hallocbuf[0]。
The problem is that temp
is just a pointer itself - and it's not pointing at anything. 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只是一个指针本身——它没有指向任何东西。这被称为“悬浮指针”。这意味着当你试图访问它指向的内容时,你会有一个错误。这发生在以下几行:
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分配的内存中,因为它没有为结构体预留空间——它只是一个指针数组,而不是一个结构体数组。
#3
0
If LIST were
如果列表
typedef struct listhead LIST;
and you accessed temp
你临时访问
temp.size = 0;
...
then
然后
*hallocp++ = temp;
would use hallocp as a pointer into the hallocbuf buffer and place the newly initialized element there. Not the best way to do it, though. You could replace temp with
将使用hallocp作为指针进入hallocbuf缓冲区,并将新初始化的元素放在那里。但这并不是最好的方法。你可以用它来代替临时工
hallocp->size = 0;
...
++hallocp;