结构体内部指针的大小咨询及环形缓冲区初始化代码问题
Hey there! Let's tackle your questions about the circular buffer implementation and struct pointer sizes step by step.
1. Circular Buffer Implementation for
plane Struct First, let's recap your code setup to ground our discussion:
// Assume your plane struct looks something like this (you can adjust fields as needed) typedef struct plane { int flight_id; float current_altitude; char destination[50]; } plane; typedef struct queue { struct plane *q; int size, capacity, front, rear; } queue; // Your global queue pointer queue *q; int queue_init(int size){ q = (queue*) malloc(sizeof(queue)); q->q = malloc(sizeof(struct plane) * size); q->size = 0; q->capacity = size; q->front = 0; q->rear = 0; return 0; }
Key Observations & Improvements:
- Your core implementation correctly sets up a circular buffer:
q->qpoints to an array ofplanestructs, withfronttracking the start of the queue andrearmarking the next insertion spot. - A notable limitation: using a global
queue *qmeans you can't create multiple independent queues. A more flexible approach is to havequeue_initreturn the initialized queue pointer instead of relying on a global variable, plus add error handling for malloc failures:queue* queue_init(int size){ queue *new_queue = (queue*) malloc(sizeof(queue)); if (!new_queue) return NULL; // Handle queue struct allocation failure new_queue->q = malloc(sizeof(plane) * size); if (!new_queue->q) { free(new_queue); // Clean up if plane array allocation fails return NULL; } new_queue->size = 0; new_queue->capacity = size; new_queue->front = 0; new_queue->rear = 0; return new_queue; } - To complete the buffer, you'll want enqueue/dequeue functions. Here's a simple example:
// Enqueue a plane (returns 0 on success, -1 if buffer is full) int enqueue(queue *target_queue, plane new_plane) { if (target_queue->size == target_queue->capacity) return -1; target_queue->q[target_queue->rear] = new_plane; target_queue->rear = (target_queue->rear + 1) % target_queue->capacity; target_queue->size++; return 0; } // Dequeue a plane (returns 0 on success, -1 if buffer is empty; writes result to out_plane) int dequeue(queue *target_queue, plane *out_plane) { if (target_queue->size == 0) return -1; *out_plane = target_queue->q[target_queue->front]; target_queue->front = (target_queue->front + 1) % target_queue->capacity; target_queue->size--; return 0; }
2. Size of Pointers Inside Structs
Great question! Here's the straightforward breakdown:
- The pointer
struct plane *qinside yourqueuestruct has a fixed size that depends only on your system's architecture, not how big theplanestruct itself is.- On 32-bit systems: all pointers (no matter what they point to) are 4 bytes—they just need to hold a 32-bit memory address.
- On 64-bit systems: all pointers are 8 bytes, since they need to accommodate a 64-bit address space.
- Even if your
planestruct is 200 bytes or 2000 bytes large, the pointer to it will still be 4 or 8 bytes. The pointer only stores the memory address of the first byte of theplaneinstance/array. - Quick note: The total size of the
queuestruct might be slightly larger than the sum of its members due to memory alignment (compilers add small padding blocks to ensure members are aligned to specific byte boundaries for performance), but this doesn't change the size of the pointer itself.
内容的提问来源于stack exchange,提问作者jo_tablanco
相关产品推荐
相关产品推荐

