【问题标题】:Segmentation fault with large incomprehensible error?具有大的难以理解的错误的分段错误?
【发布时间】:2013-11-14 23:21:19
【问题描述】:

该程序旨在按字母顺序对单词进行排序,无论是输入的单词还是文本文件中的单词。它编译得很好,但是当我运行它时,我得到了大量的文本。这是它的一个小样本: :*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36: v=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
它有点像某些文件格式或什么的?
这后面是:
Segmentation fault (core dumped)
我正在 Ubuntu 上的 GCC 中编译。
程序是:

#include <stdio.h>
#include <string.h>

#define MO 109 // 109 is ASCII for "m".
#define FO 102 // 102 is ASCII for "f".
#define OO 101 // 101 is ASCII for "e" and denotes an error.

int main() // Main part of program.
{
        int i, j; // Counter integer assignment.
        int n = 100; // assignment of integer for the number of strings.
        char a; // For the m/f (manual or file) option.
        char str[100][100]; // Str is the main string to be sorted.
        char temp[100]; // Temp is to switch the values for bubble sorting.
        for(i = 0; i < 1; a = OO)
        {
                printf("To input text manually, press m. To sort a file, press f. \n");   
                // M/f option.
                scanf("%c", &a); // Gets m/f option.
                if(a == MO || a == FO) // Checks for valid input.
                {
                        i = 2; // Escape from loop with valid input.
                }
                if(a != MO && a != FO) // Invalid input.
                {
                        printf("Please insert a valid response. ");
                        i = 0; // Continue loop until a valid input is reached.
                }
        } 
        if(a == MO) // Manual insert option.
        {
                puts("Enter the number of strings to be sorted.");
                scanf("%d", &n); // Gets number of strings.
                for(i = 0; i <= n; i++)
                {
                        gets(str[i]); // Gets strings from user.
                }
        }
        if(a == FO) // File option.
        {
                char b[100]; // File address of text file to be sorted.
                FILE * f; // Text file.
                printf("Enter file path of file to be sorted.");
                scanf("%c", b); // Gets file path.
                f = fopen(b, "r"); // Opens file.
                fgets(*str, 100, f); // Coverts file into string str.
                fclose(f); // Closes file.
        }
        for(i = 0; i <= n; i++) // Begin bubble sort.
        {
                for(j = i + 1; j <= n; j++)
                {
                        if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.
                        {
                                 strcpy(temp, str[i]); // Switch two strings.
                                 strcpy(str[i], str[j]);
                                 strcpy(str[j], temp);
                        }
                }
        }
        printf("The sorted string:");
        for(i = 0; i <= n; i++)
        {
                puts(str[i]); // Prints final output.
        }
        return 0; // End of main.
}

Google 搜索告诉我,分段错误通常意味着我指的是内存中不存在的位置。但我找不到任何关于如何解决它的建议,甚至找不到具体的问题。 如果有人可以帮助我解决这个问题,我将不胜感激。谢谢。

【问题讨论】:

  • 这段代码有很多问题。
  • 你能告诉我它有什么问题吗?我不太擅长这种事情。
  • @user2973526 对于初学者,请使用'm''f''e' 而不是那些#defines。
  • 提示1:如果你定义一个数组为a[100],那么它的元素是a[0],...a[99]
  • 永远不要以任何理由使用gets。请改用 fget。

标签: c string file sorting segmentation-fault


【解决方案1】:

正如您问题的一位 cmet 所说,代码有很多问题......

例如

for(i = 0; i < 1; a = OO)
{
  // ...
}

在循环结束时,总是a == OO,因为您在for 语句的最后部分告诉它等于OO。因此,您在“循环”内设置 a 的值的所有工作都被浪费了。

但是回到问题的重点,关于 seg-fault:你是对的,它是由引用你的程序不拥有的内存引起的。在您的情况下,可能是因为:

   int n = 100; // assignment of integer for the number of strings.
   // ...
   char str[100][100]; // Str is the main string to be sorted.
   // ...

   for(i = 0; i <= n; i++) // Begin bubble sort.
    {
            for(j = i + 1; j <= n; j++)
            {
                    if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.

str[100] 超出了数组的限制。具有 100 个元素的数组将使用从 0 到 99 的索引。str[100] 将访问 '101st' 元素,该元素超出范围,因此可能导致 seg 错误。

【讨论】:

    【解决方案2】:

    我对你的算法做了一个小的修改,它对我有用:

    #include <stdio.h>
    #include <string.h>
    
    #define MO 109 // 109 is ASCII for "m".
    #define FO 102 // 102 is ASCII for "f".
    #define OO 101 // 101 is ASCII for "e" and denotes an error.
    
    int main() // Main part of program.
    {
        int i, j; // Counter integer assignment.
        int n = 100; // assignment of integer for the number of strings.
        char a; // For the m/f (manual or file) option.
        char str[100][100]; // Str is the main string to be sorted.
        char temp[100]; // Temp is to switch the values for bubble sorting.
        a = OO;
        i=0;
        while(i < 1)
        {
            printf("To input text manually, press m. To sort a file, press f. \n");
            // M/f option.
            scanf("%c", &a); // Gets m/f option.
            if(a == MO || a == FO) // Checks for valid input.
            {
                i = 2; // Escape from loop with valid input.
            }
            if(a != MO && a != FO) // Invalid input.
            {
                printf("Please insert a valid response. ");
                i = 0; // Continue loop until a valid input is reached.
            }
        }
        if(a == MO) // Manual insert option.
        {
            puts("Enter the number of strings to be sorted.");
            scanf("%d", &n); // Gets number of strings.
            for(i = 0; i <= n; i++)
            {
                gets(str[i]); // Gets strings from user.
            }
        }
        if(a == FO) // File option.
        {
            char b[100]; // File address of text file to be sorted.
            FILE * f; // Text file.
            printf("Enter file path of file to be sorted.");
            scanf("%c", b); // Gets file path.
            f = fopen(b, "r"); // Opens file.
            fgets(*str, 100, f); // Coverts file into string str.
            fclose(f); // Closes file.
        }
        for(i = 0; i < n; i++) // Begin bubble sort.
        {
            for(j = i + 1; j <= n; j++)
            {
                if(strcmp(str[i], str[j]) > 0) // Checks alphabetical value.
                {
                    strcpy(temp, str[i]); // Switch two strings.
                    strcpy(str[i], str[j]);
                    strcpy(str[j], temp);
                }
            }
        }
        printf("The sorted string:");
        for(i = 0; i < n; i++)
        {
            puts(str[i]); // Prints final output.
        }
        return 0; // End of main.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多