访问C中另一个结构中的结构指针

时间:2022-09-06 11:27:06

I have a type node whose pointer is being used in another struct as shown below.

我有一个类型节点,其指针正在另一个结构中使用,如下所示。

typedef struct element {
    void* data;
    struct element *next;
} node;

typedef struct queue {
    node *tail;
    node *head;
    int num_items;
} queue_t;

I create an empty queue using the following code, but I am not sure if head and tail should be set to NULL since temp is not pointing anywhere yet.

我使用以下代码创建一个空队列,但我不确定head和tail是否应该设置为NULL,因为temp还没有指向任何地方。

queue_t *temp;
temp = malloc(sizeof(queue_t));
if (temp == NULL){
return NULL;
}
temp->head = NULL;
temp->tail = NULL;
temp->num_items = 0;

As per my understanding, malloc will only make temp point to some address space whose size is equal to the size of the struct queue_t. The address space does not contain a valid queue element yet. So how are temp->head = NULL; and temp->tail = NULL; valid statements? Can someone please explain why this works?

根据我的理解,malloc只会将temp指向一些大小等于struct queue_t大小的地址空间。地址空间尚未包含有效的队列元素。那么temp-> head = NULL怎么样;和temp-> tail = NULL;有效的陈述?有人可以解释为什么这有效吗?

4 个解决方案

#1


2  

The address space does not contain a valid queue element yet.

地址空间尚未包含有效的队列元素。

Correct, the allocated memory only contains a queue_t

正确,分配的内存只包含queue_t

So how are temp->head = NULL; and temp->tail = NULL; valid statements?

那么temp-> head = NULL怎么样;和temp-> tail = NULL;有效的陈述?

head and tail are not part of struct element. head and tail are part of queue_t. You have allocated a queue_t so it is OK to assign values to head and tail. In this case you assign the NULL value to show that they don't point to anything valid yet.

head和tail不是struct元素的一部分。 head和tail是queue_t的一部分。您已经分配了一个queue_t,因此可以为head和tail分配值。在这种情况下,您指定NULL值以显示它们未指向任何有效的值。

When you allocate a node (aka struct element) you update head and tail like:

当您分配节点(也称为struct元素)时,您可以更新头部和尾部:

// Add first node
temp->head == malloc(sizeof(node));
temp->tail == temp->head;
if (temp->head == NULL){
    return NULL;
}
temp->num_items = 1;

// Initialize the new node
temp->head->next = NULL;   
temp->head->data = NULL;   

// Note: Adding more node requires more complex code

#2


0  

What is the definition of a "valid queue element"? If it's "sufficient space to hold a queue element and where the locations that hold the head and tail pointers have valid values", then setting them, to NULL makes it valid. If it's not that, what is it?

“有效队列元素”的定义是什么?如果“足够的空间来容纳队列元素以及保持头部和尾部指针的位置具有有效值的位置”,则将它们设置为NULL使其有效。如果不是那样,它是什么?

#3


0  

As per my understanding, malloc will only make temp point to some address space whose size is equal to the size of the struct queue_t.

根据我的理解,malloc只会将temp指向一些大小等于struct queue_t大小的地址空间。

Correct.

正确。

The address space does not contain a valid queue element yet.

地址空间尚未包含有效的队列元素。

Not sure what you what you meant by "valid", but that statement is also correct.

不确定你的意思是什么“有效”,但该陈述也是正确的。

So how are temp->head = NULL; and temp->tail = NULL; valid statements?

那么temp-> head = NULL怎么样;和temp-> tail = NULL;有效的陈述?

It is precisely those statements that makes your allocated space a valid queue element!

正是这些语句使您分配的空间成为有效的队列元素!

Can someone please explain why this works?

有人可以解释为什么这有效吗?

Your question fundamentally is no different from a statement such as int i;. Your implementation sets aside a space to hold an integer. However, it is as yet invalid because you have not given it any (meaningful) value. Once you set i = 0; or i = 42;, the space that you call i is now a valid integer.

您的问题从根本上与诸如int i;之类的陈述没有什么不同。您的实现留出了一个空格来保存整数。但是,它仍然无效,因为您没有给它任何(有意义的)值。一旦你设置i = 0;或者i = 42;,你调用i的空间现在是一个有效的整数。

Hope that helps.

希望有所帮助。

#4


0  

As per my understanding, malloc will only make temp point to some address space whose size is equal to the size of the struct queue_t.

根据我的理解,malloc只会将temp指向一些大小等于struct queue_t大小的地址空间。

The malloc function call returns an address to the beginning of the allocated memory of size specified in the argument of malloc function call(in bytes). The allocated memory space will be of size specified in the argument of the malloc. However, the address returned by malloc will be the beginning of that memory space. Therefore, you can access upto the size of the memory space safely using the pointer to that memory space.

malloc函数调用将地址返回到malloc函数调用参数中指定的已分配内存的开头(以字节为单位)。分配的内存空间的大小将在malloc的参数中指定。但是,malloc返回的地址将是该内存空间的开头。因此,您可以使用指向该内存空间的指针安全地访问最大内存空间大小。

The address space does not contain a valid queue element yet.

地址空间尚未包含有效的队列元素。

The C Standard library has allocated a valid memory space for your pointer variable temp to point to. However, the values stored at that memory space could be garbage. Therefore, the pointer to node and num_items data members which have some valid memory space allocated to them within your queue_t may have garbage value. For example, after allocating the memory for queue_t, you can try to print the value of num_items using printf function.

C标准库为指针变量temp指定了有效的内存空间以指向。但是,存储在该存储空间中的值可能是垃圾。因此,指向node_tms中具有一些有效内存空间的node和num_items数据成员的指针可能具有垃圾值。例如,在为queue_t分配内存后,您可以尝试使用printf函数打印num_items的值。

queue_t *temp = malloc(sizeof(queue_t));
if (temp == NULL){
    return NULL;
}
printf("The value of num_items: %d\n", temp->num_items);

The above example may print any garbage value. Since, C language doesn't have constructors to initialize newly created variables, you should initialize every variable you create with some stable value.

以上示例可以打印任何垃圾值。由于C语言没有构造函数来初始化新创建的变量,因此应该使用一些稳定的值初始化您创建的每个变量。

You can also use calloc function call which also sets allocated memory to zero after allocating the memory space.

您还可以使用calloc函数调用,它还在分配内存空间后将已分配的内存设置为零。

So how are temp->head = NULL; and temp->tail = NULL; valid statements?

那么temp-> head = NULL怎么样;和temp-> tail = NULL;有效的陈述?

The memory is allocated by malloc which may contain any garbage value. The data members of queue_t share that memory space. Since, memory space can have garbage data, the data members will be having any random garbage data. That's why it is a good approach to initialize data members of the struct allocated by malloc to some stable values. That's what you have done in your program.

内存由malloc分配,malloc可能包含任何垃圾值。 queue_t的数据成员共享该内存空间。由于内存空间可能有垃圾数据,因此数据成员将拥有任何随机垃圾数据。这就是为什么将malloc分配的struct的数据成员初始化为某些稳定值的好方法。这就是你在程序中所做的。

Actually, temp's data members head and tail should point to the addresses of variables of type node. The malloc call has allocated the pointer variables. These pointer variables can point to any variable of type node (or store the address of variable of type node). But you haven't allocated any node variable yet and you don't want to create dangling pointer. Therefore, you should initialize these pointer variables with NULL.

实际上,temp的数据成员head和tail应该指向node类型的变量的地址。 malloc调用已分配指针变量。这些指针变量可以指向node类型的任何变量(或存储类型node的变量的地址)。但是你还没有分配任何节点变量,你不想创建悬空指针。因此,您应该使用NULL初始化这些指针变量。

Your program should look like this:

你的程序应该是这样的:

queue_t *temp;
temp = malloc(sizeof(queue_t));
if (temp == NULL){
    return NULL;
}
temp->head = NULL;
temp->tail = NULL;
temp->num_items = 0;

//Allocate some node
node *n = malloc(sizeof(node));
int data = 1;
n->data=&data;
n->next=NULL;
temp->head=n;
temp->tail=n;
temp->num_items=1;

#1


2  

The address space does not contain a valid queue element yet.

地址空间尚未包含有效的队列元素。

Correct, the allocated memory only contains a queue_t

正确,分配的内存只包含queue_t

So how are temp->head = NULL; and temp->tail = NULL; valid statements?

那么temp-> head = NULL怎么样;和temp-> tail = NULL;有效的陈述?

head and tail are not part of struct element. head and tail are part of queue_t. You have allocated a queue_t so it is OK to assign values to head and tail. In this case you assign the NULL value to show that they don't point to anything valid yet.

head和tail不是struct元素的一部分。 head和tail是queue_t的一部分。您已经分配了一个queue_t,因此可以为head和tail分配值。在这种情况下,您指定NULL值以显示它们未指向任何有效的值。

When you allocate a node (aka struct element) you update head and tail like:

当您分配节点(也称为struct元素)时,您可以更新头部和尾部:

// Add first node
temp->head == malloc(sizeof(node));
temp->tail == temp->head;
if (temp->head == NULL){
    return NULL;
}
temp->num_items = 1;

// Initialize the new node
temp->head->next = NULL;   
temp->head->data = NULL;   

// Note: Adding more node requires more complex code

#2


0  

What is the definition of a "valid queue element"? If it's "sufficient space to hold a queue element and where the locations that hold the head and tail pointers have valid values", then setting them, to NULL makes it valid. If it's not that, what is it?

“有效队列元素”的定义是什么?如果“足够的空间来容纳队列元素以及保持头部和尾部指针的位置具有有效值的位置”,则将它们设置为NULL使其有效。如果不是那样,它是什么?

#3


0  

As per my understanding, malloc will only make temp point to some address space whose size is equal to the size of the struct queue_t.

根据我的理解,malloc只会将temp指向一些大小等于struct queue_t大小的地址空间。

Correct.

正确。

The address space does not contain a valid queue element yet.

地址空间尚未包含有效的队列元素。

Not sure what you what you meant by "valid", but that statement is also correct.

不确定你的意思是什么“有效”,但该陈述也是正确的。

So how are temp->head = NULL; and temp->tail = NULL; valid statements?

那么temp-> head = NULL怎么样;和temp-> tail = NULL;有效的陈述?

It is precisely those statements that makes your allocated space a valid queue element!

正是这些语句使您分配的空间成为有效的队列元素!

Can someone please explain why this works?

有人可以解释为什么这有效吗?

Your question fundamentally is no different from a statement such as int i;. Your implementation sets aside a space to hold an integer. However, it is as yet invalid because you have not given it any (meaningful) value. Once you set i = 0; or i = 42;, the space that you call i is now a valid integer.

您的问题从根本上与诸如int i;之类的陈述没有什么不同。您的实现留出了一个空格来保存整数。但是,它仍然无效,因为您没有给它任何(有意义的)值。一旦你设置i = 0;或者i = 42;,你调用i的空间现在是一个有效的整数。

Hope that helps.

希望有所帮助。

#4


0  

As per my understanding, malloc will only make temp point to some address space whose size is equal to the size of the struct queue_t.

根据我的理解,malloc只会将temp指向一些大小等于struct queue_t大小的地址空间。

The malloc function call returns an address to the beginning of the allocated memory of size specified in the argument of malloc function call(in bytes). The allocated memory space will be of size specified in the argument of the malloc. However, the address returned by malloc will be the beginning of that memory space. Therefore, you can access upto the size of the memory space safely using the pointer to that memory space.

malloc函数调用将地址返回到malloc函数调用参数中指定的已分配内存的开头(以字节为单位)。分配的内存空间的大小将在malloc的参数中指定。但是,malloc返回的地址将是该内存空间的开头。因此,您可以使用指向该内存空间的指针安全地访问最大内存空间大小。

The address space does not contain a valid queue element yet.

地址空间尚未包含有效的队列元素。

The C Standard library has allocated a valid memory space for your pointer variable temp to point to. However, the values stored at that memory space could be garbage. Therefore, the pointer to node and num_items data members which have some valid memory space allocated to them within your queue_t may have garbage value. For example, after allocating the memory for queue_t, you can try to print the value of num_items using printf function.

C标准库为指针变量temp指定了有效的内存空间以指向。但是,存储在该存储空间中的值可能是垃圾。因此,指向node_tms中具有一些有效内存空间的node和num_items数据成员的指针可能具有垃圾值。例如,在为queue_t分配内存后,您可以尝试使用printf函数打印num_items的值。

queue_t *temp = malloc(sizeof(queue_t));
if (temp == NULL){
    return NULL;
}
printf("The value of num_items: %d\n", temp->num_items);

The above example may print any garbage value. Since, C language doesn't have constructors to initialize newly created variables, you should initialize every variable you create with some stable value.

以上示例可以打印任何垃圾值。由于C语言没有构造函数来初始化新创建的变量,因此应该使用一些稳定的值初始化您创建的每个变量。

You can also use calloc function call which also sets allocated memory to zero after allocating the memory space.

您还可以使用calloc函数调用,它还在分配内存空间后将已分配的内存设置为零。

So how are temp->head = NULL; and temp->tail = NULL; valid statements?

那么temp-> head = NULL怎么样;和temp-> tail = NULL;有效的陈述?

The memory is allocated by malloc which may contain any garbage value. The data members of queue_t share that memory space. Since, memory space can have garbage data, the data members will be having any random garbage data. That's why it is a good approach to initialize data members of the struct allocated by malloc to some stable values. That's what you have done in your program.

内存由malloc分配,malloc可能包含任何垃圾值。 queue_t的数据成员共享该内存空间。由于内存空间可能有垃圾数据,因此数据成员将拥有任何随机垃圾数据。这就是为什么将malloc分配的struct的数据成员初始化为某些稳定值的好方法。这就是你在程序中所做的。

Actually, temp's data members head and tail should point to the addresses of variables of type node. The malloc call has allocated the pointer variables. These pointer variables can point to any variable of type node (or store the address of variable of type node). But you haven't allocated any node variable yet and you don't want to create dangling pointer. Therefore, you should initialize these pointer variables with NULL.

实际上,temp的数据成员head和tail应该指向node类型的变量的地址。 malloc调用已分配指针变量。这些指针变量可以指向node类型的任何变量(或存储类型node的变量的地址)。但是你还没有分配任何节点变量,你不想创建悬空指针。因此,您应该使用NULL初始化这些指针变量。

Your program should look like this:

你的程序应该是这样的:

queue_t *temp;
temp = malloc(sizeof(queue_t));
if (temp == NULL){
    return NULL;
}
temp->head = NULL;
temp->tail = NULL;
temp->num_items = 0;

//Allocate some node
node *n = malloc(sizeof(node));
int data = 1;
n->data=&data;
n->next=NULL;
temp->head=n;
temp->tail=n;
temp->num_items=1;