【问题标题】:SIGSEGV Segmentation faul when trying to get a sub string尝试获取子字符串时出现 SIGSEGV 分段错误
【发布时间】:2022-01-11 04:43:39
【问题描述】:

我收到以下错误:

线程 1 收到信号 SIGSEGV,分段错误

代码如下:

int extraercadena(char *cad, char *tCad, int ini, int fin){
    int iC, iS;

    if ((ini>fin) || (ini < 0) || (fin >= strlen(cad)))
        return 0;

    iS = 0;

    for(iC = ini; iC <= fin; iC++){
        //error is here, the system halt here
        tCad[iS] = cad[iC];
        iS++;
    }

    tCad[iS] = '\0';

    return 1;
}

【问题讨论】:

标签: c substring


【解决方案1】:

有七种可能性:

  • cad 无法取消引用,因为它未初始化或为 NULL 或其他无效指针。
  • iC 是否定的。
  • cad 不指向至少包含 iC+1 元素的数组。
  • tCad 无法取消引用,因为它未初始化或为 NULL 或其他无效指针。
  • tCad 不指向至少包含 iS+1 元素的数组。
  • tCad 指向不可写的内存。 (例如,字符串文字返回的内存通常在可能的情况下是只读的。)
  • 这是程序中其他一些未定义行为的结果。 (不太可能。)

【讨论】:

    猜你喜欢
    • 2020-06-24
    • 2016-06-16
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多