我正在尝试编写队列。为了测试它,我已经出现了几个值,这些值是静态分配的堆栈变量的指针。当我打印出被推开的堆栈变量的地址,并由Dequeue返回的指针(对应于该项目的原始配对)时,我得到了似乎是相同的地址。当我将指针指向静态分配的变量(我使用&
运算符内联)的指针时,它会打印我期望的值10
。但是,当我尊重Dequeue返回的指针时,似乎是相同的地址,将会出现故障。我不太确定发生了什么,我对C内存模型的理解无法解释这一点。
输出
Storing item at index 0
Queue (head, tail) = 0, 1
1158904628, 0, 0,
Storing item at index 1
Queue (head, tail) = 0, 2
1158904628, 1158904632, 0,
Storing item at index 2
Queue (head, tail) = 0, 0
1158904628, 1158904632, 1158904636,
---------------------------
Enqueued pointer: 45137b34
Enqueued pointer value: 10
Queue (head, tail) = 1, 0
0, 1158904632, 1158904636,
Dequeued pointer: 45137b34
Segmentation fault (core dumped)
编译 gcc -o main queue.c
代码
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *buffer;
int size;
int capacity;
int head;
int tail;
} Queue;
void queue_init(Queue *queue, int capacity) {
void *ptr = malloc((sizeof(void*)) * capacity);
queue->buffer = ptr;
queue->capacity = capacity;
queue->size = 0;
queue->head = 0;
queue->tail = 0;
}
void* queue_dequeue(Queue *queue) {
if (queue->size == 0) return NULL;
void *item = queue->buffer[queue->head];
queue->buffer[queue->head] = NULL;
queue->head = (queue->head + 1) % queue->capacity;
printf("Queue (head, tail) = %d, %dn", queue->head, queue->tail);
queue->size--;
for (int i = 0; i < 3; i++) {
printf("%u, ", queue->buffer[i]);
}
printf("n");
return item;
}
int queue_enqueue(Queue *queue, void *item) {
if (queue->size == queue->capacity) return -1;
printf("Storing item at index %dn", queue->tail);
queue->buffer[queue->tail] = item;
queue->size++;
queue->tail = (queue->tail + 1) % queue->capacity;
printf("Queue (head, tail) = %d, %dn", queue->head, queue->tail);
for (int i = 0; i < 3; i++) {
printf("%u, ", queue->buffer[i]);
}
printf("n");
}
int main() {
int a = 10;
int b = 20;
int c = 30;
Queue q;
queue_init(&q, 3);
queue_enqueue(&q, &a);
queue_enqueue(&q, &b);
queue_enqueue(&q, &c);
printf("---------------------------n");
int *org = &a;
int *ptr;
printf("Enqueued pointer: %xn", org);
printf("Enqueued pointer value: %in", *org);
ptr = queue_dequeue(&q);
printf("Dequeued pointer: %xn", ptr);
printf("Dequeued pointer value: %in", *ptr);
return 0;
}
需要将 int *
更改为 void **
,
否则,整数空间(4bytes)不足以节省void*(8bytes)
修改如下
#include <stdio.h>
#include <stdlib.h>
typedef struct {
void **buffer;
int size;
int capacity;
int head;
int tail;
} Queue;
void queue_init(Queue *queue, int capacity) {
void *ptr = malloc((sizeof(void*)) * capacity);
queue->buffer = ptr;
queue->capacity = capacity;
queue->size = 0;
queue->head = 0;
queue->tail = 0;
}
void* queue_dequeue(Queue *queue) {
if (queue->size == 0) return NULL;
void *item = queue->buffer[queue->head];
queue->buffer[queue->head] = NULL;
queue->head = (queue->head + 1) % queue->capacity;
printf("Queue (head, tail) = %d, %dn", queue->head, queue->tail);
queue->size--;
for (int i = 0; i < queue->size; i++) {
printf("%p, ", queue->buffer[i]);
}
printf("n");
return item;
}
int queue_enqueue(Queue *queue, void *item) {
if (queue->size == queue->capacity) return -1;
printf("Storing item at index %dn", queue->tail);
queue->buffer[queue->tail] = item;
queue->size++;
queue->tail = (queue->tail + 1) % queue->capacity;
printf("Queue (head, tail) = %d, %dn", queue->head, queue->tail);
for (int i = 0; i < queue->size; i++) {
printf("%p, ", queue->buffer[i]);
}
printf("n");
return 0;
}
int main() {
int a = 10;
int b = 20;
int c = 30;
Queue q;
queue_init(&q, 3);
queue_enqueue(&q, &a);
queue_enqueue(&q, &b);
queue_enqueue(&q, &c);
printf("---------------------------n");
int *org = &a;
int *ptr;
printf("Enqueued pointer: %pn", org);
printf("Enqueued pointer value: %in", *org);
ptr = queue_dequeue(&q);
printf("Dequeued pointer: %pn", ptr);
printf("Dequeued pointer value: %in", *ptr);
return 0;
}