You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用strcpy导致程序崩溃,请求分析优先级队列代码问题

Troubleshooting the 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, accessing pq->entries will immediately cause undefined behavior, which could manifest as a crash later when you try to use strcpy.
  • Similarly, if eintrag = malloc(...) returns NULL, trying to assign to eintrag->pqvalue will 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->pqvalue is uninitialized or set to NULL, strcpy will try to write to an invalid address.
  • Allocating insufficient memory: If you only allocate strlen(value) bytes instead of strlen(value) + 1, strcpy will 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:21:40