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

Xcode构建邻接矩阵时触发Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)错误

Fixing Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) in Your Course Prerequisite Top Sort Program

Hey there! That EXC_BAD_ACCESS error with address 0x0 is a classic sign you’re trying to access a null pointer—memory that doesn’t exist. Since it triggers right after you output the vertex count, let’s walk through the most likely issues and fixes tailored to your setup (using a linked list array for adjacency lists, which is perfect for topological sort):

Common Culprits & Fixes

1. Uninitialized Adjacency List Array

Chances are you declared an array of linked list heads but forgot to properly allocate or initialize it. For example:

  • If you have Node** adjList; but never called malloc(numVertices * sizeof(Node*)) to allocate the array itself.
  • Or you allocated the array but didn’t set each element to NULL, leading to wild pointers.

Fix: Always initialize your adjacency list explicitly:

typedef struct Node {
    int vertex;
    struct Node* next;
} Node;

Node** createAdjList(int numVertices) {
    Node** adjList = malloc(numVertices * sizeof(Node*));
    if (adjList == NULL) {
        fprintf(stderr, "Failed to allocate adjacency list\n");
        exit(EXIT_FAILURE);
    }
    // Initialize each linked list head to NULL
    for (int i = 0; i < numVertices; i++) {
        adjList[i] = NULL;
    }
    return adjList;
}

2. Out-of-Bounds or Null Pointer Reads When Parsing Input

After outputting vertex count, your next step is probably building the adjacency list from the course/prerequisite file. Common mistakes here:

  • Using 1-based course IDs with a 0-indexed array (e.g., trying to access adjList[courseId] where courseId starts at 1, but your array only goes up to numVertices - 1).
  • Trying to process a prerequisite entry that’s missing (empty) and dereferencing a null pointer as a result.

Fix:

  • Normalize course IDs to 0-indexed if needed (subtract 1 from each ID when accessing the array).
  • Add bounds checks before accessing any array entries: verify the course/prerequisite ID is between 0 and numVertices - 1 before modifying the adjacency list.

3. Null Pointer Access in Your Topological Sort Logic

Whether you’re using Kahn’s algorithm (in-degree based) or a DFS-based approach, you might be accessing a null node without checking first:

  • In Kahn’s algorithm: pulling a null node from your queue, or traversing an adjacency list without checking if currentNode != NULL.
  • In DFS: trying to visit neighbors of a node whose adjacency list head is null.

Fix: Add null checks everywhere you traverse linked lists. For example, when iterating through a node’s neighbors:

Node* current = adjList[vertex];
while (current != NULL) {
    // Process current->vertex (e.g., decrement in-degree)
    current = current->next;
}

Debugging Tips to Pinpoint the Exact Issue

  • Use Xcode’s Debugger: When the crash happens, look at the call stack to see exactly which line of code is triggering the error. If it’s a line like current->vertex, that means current is null.
  • Print Debug Info: Add print statements right after initializing the adjacency list, and after each edge is added. Print the vertex ID and the address of the adjacency list entry to confirm you’re not dealing with nulls.
  • Test with a Tiny Input: Start with a minimal test case (e.g., 2 courses: Course 1 is a prerequisite for Course 2) and step through your code manually—this makes it way easier to spot where things go wrong.

内容的提问来源于stack exchange,提问作者Josh Garza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:17:15