GDB调试提示找不到strlen.S文件,该错误是否属于正常行为?
Hey there, let's break this down clearly—your program has a critical bug causing the segmentation fault, and the "missing .S file" message is just a GDB side effect, not the actual problem.
1. The Real Reason for the SIGSEGV: Invalid Pointer Use
Take a close look at your reverse function:
char* reverse(char s[]) { char rev[MAX]; // ... reverse logic ... return rev; }
The rev array is a local stack variable. When the reverse function finishes running, its stack frame gets destroyed, and the memory holding rev is no longer valid. When you return a pointer to this dead memory and pass it to puts, puts calls strlen on that invalid pointer—this is exactly what triggers the segmentation fault.
This is classic undefined behavior in C: accessing memory your program no longer owns. Even if the program seems to "work" occasionally, it's just luck (the memory hasn't been overwritten yet) — it's still broken.
Also, there's a typo in your getLine function: s[i++] == '\n'; should be s[i++] = '\n'; (assignment, not equality check). That's why your newline characters aren't being stored correctly.
2. Fixing the Program
You have a few solid options to fix the pointer issue:
- Reverse the string in-place: Modify the original
sarray directly instead of creating a local array. This avoids any pointer problems entirely. - Use dynamic memory allocation: Allocate memory for
revwithmalloc(lives on the heap, not the stack). Just remember tofreeit later to avoid memory leaks. - Let the caller provide the destination: Pass an empty array from
maintoreverseto store the reversed string, so you don't need to return a pointer to local memory.
Here's a quick in-place reverse implementation that works correctly:
char* reverse(char s[]) { int start = 0; int end = 0; // Find the end of the string while (s[end] != '\0') end++; end--; // Move back from the null terminator // Swap characters from start and end while (start < end) { char temp = s[start]; s[start] = s[end]; s[end] = temp; start++; end--; } return s; }
3. Fixing the "Missing .S File" Message in GDB
The error about strlen.S: No such file or directory happens because GDB is trying to show you the source code for the strlen function (part of glibc), but your system doesn't have glibc's debug symbols or source files installed.
To fix this on Debian 9 i386:
- Install the debug symbols package:
sudo apt-get install libc6-dbg:i386 - If you want to see the actual glibc source code in GDB, install the source package:
sudo apt-get install glibc-source:i386
Once installed, GDB will find the strlen.S file and show you the exact assembly line where the crash occurred—but remember, this is just for better debugging context; fixing your program's pointer bug is the real solution.
内容的提问来源于stack exchange,提问作者Ed Crap

