读取值并实现图时函数崩溃问题排查求助
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:
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 twoprintfstatements 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
nextpointer of newadjvertexlistnodes toNULL. Uninitialized pointers point to garbage memory, which can cause crashes when you try to traverse or modify the list later. - Double-check that the
listpointer of the targetvertexlistnode 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
vertexlistlooking 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
printftries 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++) withgdb ./your_program, then runrunto start your program. When it crashes, usebacktrace(shortened tobt) to see exactly which line of code triggered the crash. - At the crash point, use
printto check the values of all relevant pointers (e.g.,print sourceorprint 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
vertexlisthas 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
vertexlistoradjvertexlistnode when adding vertices? - Are you reusing a pointer that was already freed (a use-after-free error)?
- Tools like
valgrindcan help here—runvalgrind ./your_programto get a detailed report of invalid memory accesses, leaks, or corruption.
内容的提问来源于stack exchange,提问作者floreapaun

