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

无法定位并修复Segmentation Fault错误,附GDB报错及代码

Fixing Segmentation Fault in Your C String Concatenation Program

Let's break down the issues causing your segmentation fault, starting with the GDB error message you shared:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040073b in concat (nextstr=0x7fffffffee66 "the", longstr=0x7fffffffe750 "", i=0x0) at main.c:24
24 printf("%c -> %c\n", nextstr[j], longstr[*i]);

The key clue here is i=0x0—this means the i pointer passed to concat is a NULL pointer. When you try to dereference it with *i, you're accessing memory at address 0, which triggers the SIGSEGV. Here's why this is happening, plus other critical bugs:

Critical Bugs in Your Code

1. Incorrect Pointer Argument Passing

In your main function, you call concat like this:

if (concat(argv[j], longstring, i)) return 1;

The third parameter i is an int value, but concat expects an int * (pointer to int). Passing the value 0 (your initial i value) as a pointer means concat is trying to dereference address 0 (NULL), which is invalid memory. This is the direct cause of the segmentation fault in line 24.

Additionally, even if this didn't crash, modifying *i inside concat wouldn't update the i variable in main—you need to pass a pointer to i so changes are shared between functions.

2. Out-of-Bounds Access to argv

Your for loop in main uses this condition:

for (j = 1; j <= argc; j++) {

The argv array only has valid entries from argv[0] (program name) to argv[argc-1] (last command-line argument). argv[argc] is guaranteed to be a NULL pointer. When j reaches argc, you're passing a NULL pointer to concat as nextstr, and accessing nextstr[j] inside concat will also trigger a segmentation fault.

3. Potential Buffer Overflow (Minor but Important)

Your length check in concat happens after you increment *i:

(*i)++;
j++;
if (*i > MAX_CHARS) {

This means you might write one character past the end of longstr before checking. You should check the length before writing to the array.

Fixed Code

Here's the corrected version with comments highlighting the changes:

// define max for command line 
#define MAX_CHARS 1000 
#include <stdio.h> 

int concat(char[], char[], int *); 
void printreverse(char[], int); 

int main(int argc, char *argv[]) { 
    char longstring[MAX_CHARS] = { '\0' }; 
    int i = 0, j; 

    if (argc < 2) { 
        printf("%s requires command-line args\n", argv[0]); 
        return 1; 
    } 

    // Fixed: Loop while j < argc (not <=) to avoid accessing argv[argc] (NULL)
    for (j = 1; j < argc; j++) { 
        // Fixed: Pass address of i (&i) instead of the value i
        if (concat(argv[j], longstring, &i)) return 1; 
    } 

    i--; 
    longstring[i] = '\0'; //delete trailing space 
    printf("%s\n", longstring); 
} 

int concat(char nextstr[], char longstr[], int *i) { 
    int j = 0; 
    while (nextstr[j] != '\0') { 
        // Fixed: Check length before writing to avoid buffer overflow
        if (*i >= MAX_CHARS - 1) { // Leave space for null terminator
            printf("Error: Input is too long!\n"); 
            return 1; 
        } 
        printf("%c -> %c\n", nextstr[j], longstr[*i]); 
        longstr[*i] = nextstr[j]; 
        (*i)++; 
        j++; 
    } 

    if (j > 0) { 
        // Fixed: Check length before adding space
        if (*i >= MAX_CHARS - 1) { 
            printf("Error: Input is too long!\n"); 
            return 1; 
        } 
        longstr[*i] = ' '; 
        (*i)++; 
    } 
    // Ensure null terminator is always present (redundant but safe)
    longstr[*i] = '\0'; 
    return 0; 
} 

Key Fixes Recap

  • Pass &i instead of i to concat so the function can modify the index variable in main
  • Change the loop condition to j < argc to avoid accessing the NULL pointer at argv[argc]
  • Move length checks to before writing to longstr to prevent buffer overflow
  • Ensure the null terminator is set correctly after adding spaces

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:28:06