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

读取值并实现图时函数崩溃问题排查求助

Hey there, let's work through troubleshooting your graph edge addition crash together. From what you've shared, here's a breakdown of targeted steps to track down the issue:

Troubleshooting Crash in AddEdge() for Graph Implementation

First, let's recap your scenario to make sure we're aligned:

Your program successfully reads data from a file and adds all vertices to the graph. But the first call to AddEdge() only prints the first two printf statements before crashing (process return code -1), and fails to enter the subsequent while loop. You've manually checked pointer logic but haven't spotted the issue yet.

Your relevant struct definitions:

struct adjvertexlist { 
    int index_vertex; 
    struct adjvertexlist *next; 
}; 

struct vertexlist { 
    int index_vertex; 
    struct vertexlist *next; 
    struct adjvertexlist *list; 
}; 

Key Troubleshooting Steps

1. Check for Uninitialized or Failed Memory Allocations

When creating new adjacency nodes in AddEdge(), it's easy to miss critical checks:

  • Always verify that malloc() returns a non-NULL pointer. If allocation fails (e.g., out of memory), trying to access fields of a NULL node will crash your program immediately.
  • Make sure you explicitly set the next pointer of new adjvertexlist nodes to NULL. Uninitialized pointers point to garbage memory, which can cause crashes when you try to traverse or modify the list later.
  • Double-check that the list pointer of the target vertexlist node isn't NULL if you're trying to traverse it (handle the empty adjacency list case first before appending).

2. Confirm Target Vertices Exist Before Modifying Their Lists

If AddEdge() takes vertex indices as input, ensure you're successfully locating both the source and target vertices in your vertexlist:

  • If you traverse past the end of the vertexlist looking for a vertex that doesn't exist, you'll dereference a NULL pointer and crash.
  • Add quick debug prints (like printf("Found source vertex: %d\n", source->index_vertex);) to confirm you're pointing to valid nodes before touching their adjacency lists.

3. Inspect the printf Statements That Execute Before Crash

The crash happens right after the first two printfs—so one of those statements might be the culprit, not the code after them:

  • If a printf tries to dereference a bad pointer (e.g., printf("Target vertex: %d\n", bad_ptr->index_vertex);), that's enough to cause an immediate crash.
  • Print the addresses of your pointers alongside their values (like printf("Source vertex pointer: %p\n", source);) to see if any are NULL or pointing to invalid memory.

4. Use a Debugger to Pinpoint the Exact Crash Line

This is the most reliable way to get to the root of the problem:

  • Fire up gdb (for C/C++) with gdb ./your_program, then run run to start your program. When it crashes, use backtrace (shortened to bt) to see exactly which line of code triggered the crash.
  • At the crash point, use print to check the values of all relevant pointers (e.g., print source or print new_adj_node) to see if any are NULL or pointing to garbage memory.

5. Check for Index Mismatches

If your file uses 1-based vertex indices but your code expects 0-based (or vice versa), you might be trying to access a vertex that's out of bounds:

  • For example, if your vertexlist has 5 vertices (indices 0-4) but the file references index 5, you'll end up traversing past the end of the list and hitting a NULL pointer.

6. Rule Out Memory Corruption

Sometimes crashes happen because memory was corrupted earlier (even before AddEdge() is called):

  • Did you accidentally write beyond the bounds of a vertexlist or adjvertexlist node when adding vertices?
  • Are you reusing a pointer that was already freed (a use-after-free error)?
  • Tools like valgrind can help here—run valgrind ./your_program to get a detailed report of invalid memory accesses, leaks, or corruption.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:47:35