调用strcpy导致程序崩溃,请求分析优先级队列代码问题
strcpy Crash in Your Priority Queue Code Hey there, let's break down the likely reasons your program is crashing when calling strcpy with this priority queue implementation, and how to fix them:
1. Missing Memory Allocation Checks
Looking at your code snippet, none of the malloc or realloc calls are followed by a NULL check. If any of these allocations fail (e.g., due to insufficient memory), you'll end up working with a NULL pointer. For example:
- If
pq = malloc(...)returns NULL, accessingpq->entrieswill immediately cause undefined behavior, which could manifest as a crash later when you try to usestrcpy. - Similarly, if
eintrag = malloc(...)returns NULL, trying to assign toeintrag->pqvaluewill crash your program.
Fix: Add checks after every memory allocation:
priorityqueue_t * pqueue_create() { priorityqueue_t *pq = malloc(sizeof(priorityqueue_t)); if (!pq) { // Check for malloc failure return NULL; } pq->entries = malloc(4 * sizeof(pqentry_t *)); if (!pq->entries) { // Clean up if entries allocation fails free(pq); return NULL; } pq->last = 0; pq->size = 4; return pq; }
2. Incorrect Allocation for eintrag->pqvalue
Your code cuts off at eintrag->pqvalue = (char *) ma..., but it's clear you're trying to allocate memory for the string value. Common mistakes here that lead to strcpy crashes are:
- Forgetting to allocate memory entirely: If
eintrag->pqvalueis uninitialized or set to NULL,strcpywill try to write to an invalid address. - Allocating insufficient memory: If you only allocate
strlen(value)bytes instead ofstrlen(value) + 1,strcpywill write past the allocated memory (since strings need an extra byte for the null terminator\0). This corrupts the heap, leading to a crash either immediately or later.
Fix: Allocate enough space for the string plus the null terminator, and check the allocation:
eintrag->pqvalue = malloc(strlen(value) + 1); if (!eintrag->pqvalue) { free(eintrag); // Handle error, maybe return early or clean up further } strcpy(eintrag->pqvalue, value);
3. Invalid isFull Implementation
If your isFull function has incorrect logic (e.g., checking pq->last > pq->size instead of pq->last >= pq->size), you'll try to insert entries beyond the bounds of the pq->entries array. This corrupts adjacent memory, which can cause a crash when strcpy tries to access the corrupted heap.
Fix: Verify that isFull correctly checks if the queue has reached its maximum capacity:
int isFull(priorityqueue_t *pq) { return pq->last >= pq->size; // Assuming last is the index of the next empty slot }
4. NULL value Parameter
If you call pqueue_insert with a NULL value pointer, strcpy will attempt to read from a NULL address, which triggers an immediate crash.
Fix: Add a check for NULL before using value:
void pqueue_insert(priorityqueue_t *pq, char* value, float p) { if (!pq || !value) { // Validate inputs return; // Or handle error appropriately } // Rest of your code... }
Start by checking these areas—memory allocation failures and incorrect string memory setup are the most likely culprits here.
内容的提问来源于stack exchange,提问作者Truut

