本文介绍了为什么分段错误(核心转储)错误适用于我的C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到这个错误,我不知道它是如何应用到我的程序。这是我的计划。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT nextword(字符*海峡);无效的主要(无效)
{
  字符*海峡=你好今天是美好的一天!\\ t \\ N!
  INT I = nextword(STR);
  而(ⅰ!= -1)
    {
      的printf(%S \\ n,及(STR [I]));
      I = nextword(NULL);
    }
}INT nextword(字符*海峡)
{
  //创建两个静态变量 - 这些停留在通过调用
  静态的char * S;
  静态INT nextindex;
  诠释thisindex;
  //重新设置静态变量
  如果(STR!= NULL)
    {
      S =海峡;
      thisindex = 0;
      // TODO:提前该指数过去任何前导空格
      而(S [thisindex] =='\\ n'|| S [thisindex] =='\\ t'|| S [thisindex] =='')
    thisindex ++;    }
  其他
    {
      //设置返回值是nextindex
      thisindex = nextindex;
    }
  //如果我们不与该字符串做...
  如果(thisindex!= -1)
    {
      nextindex = thisindex;
      // TODO:两件事情
      // 1:地方'\\ 0'后,当前字
      // 2:提前nextindex到开始
      //下一个字
      而(S [nextindex]!=''|| S [nextindex]!='\\ n'|| S [nextindex]!='\\ t')
    {
      如果(S [nextindex] =='\\ 0')
        返回-1;
      其他
        {
          nextindex ++;
          如果(S [nextindex] ==''|| S [nextindex] =='\\ n'|| S [nextindex] =='\\ t')
        海峡[nextindex] ='\\ 0';
        }
    }    }
  返回thisindex;
}

我的程序应该有一个输出的控制台

 您好!
今天

一个
美丽
天!!


解决方案

您应该所有警告编译并启用调试信息(如果使用的 GCC 的如在Linux上,这意味着编译 GCC -Wall -g )。

那么你应该学习如何使用调试器(即 GDB 在Linux上)和可能的泄漏检测仪像的

分段故障可能,如果你恰巧解引用一些坏指针,例如一个空的一个或一个未初始化的。它如果你写到只读段也可能发生(你可能改写一个字符串文字被放到一个只读 - 所以叫的.text .RODATA - 段)

考虑到编译器的每一个警告(并使他们),并使用调试器是所有C程序员的基本技能。

I keep getting this error and I am not sure how it applies to my program. This is my program.

#include<stdio.h>
#include<stdlib.h>

int nextword(char *str);



void main(void)
{
  char *str = "Hello! Today is a beautiful day!!\t\n";
  int i = nextword(str);
  while(i != -1)
    {
      printf("%s\n",&(str[i]));
      i = nextword(NULL);
    }
}

int nextword(char *str)
{
  // create two static variables - these stay around across calls
  static char *s;
  static int nextindex;
  int thisindex;
  // reset the static variables
  if (str != NULL)
    {
      s = str;
      thisindex = 0;
      // TODO:  advance this index past any leading spaces
      while (s[thisindex]=='\n' || s[thisindex]=='\t' || s[thisindex]==' '                )
    thisindex++;

    }
  else
    {
      // set the return value to be the nextindex
      thisindex = nextindex;
    }
  // if we aren't done with the string...
  if (thisindex != -1)
    {
      nextindex = thisindex;
      // TODO: two things
      // 1: place a '\0' after the current word
      // 2: advance nextindex to the beginning
      // of the next word
      while (s[nextindex] != ' ' || s[nextindex] != '\n' || s[nextindex] != '\t')
    {
      if ( s[nextindex] == '\0')
        return -1;
      else
        {
          nextindex++;
          if (s[nextindex]==' '||s[nextindex]=='\n'||s[nextindex]=='\t')
        str[nextindex]='\0';
        }
    }

    }
  return thisindex;
}

My program is supposed to have an output to the console of

Hello!
Today
is
a
beautiful
day!!
解决方案

You should compile with all warnings and debugging info enabled (if using GCC e.g. on Linux, that means compiling with gcc -Wall -g).

Then you should learn how to use the debugger (i.e. gdb on Linux) and possibly a leak detector like valgrind

a segmentation fault may happen if you dereference some "bad" pointer, e.g. a NULL one or an uninitialized one. It also may happen if you write into a read-only segment (you are probably overwriting a string literal which is put into a read-only -so called .text or .rodata- segment)

Taking account of every warning of the compiler (and enabling them) and using a debugger are essential skills of any C programmer.

这篇关于为什么分段错误(核心转储)错误适用于我的C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:01