简体   繁体   English

为什么这些代码给我一个sigsegv错误?

[英]Why is these code giving me an sigsegv error?

Why is these code giving me a RUNTIME SIGSEGV ERROR. 为什么这些代码给了我一个运行时SIGSEGV错误。 I have tried running the code and works perfectly in codeblocks but some IDE is giving me these error. 我已经尝试运行代码,并且可以在代码块中完美运行,但是某些IDE给了我这些错误。

It takes a Fibonacci series then modulus each number in the series and only takes up the numbers at eve places till a single number is obtained. 它采用斐波那契数列,然后对序列中的每个数字求模,并且仅在前夕的位置接受数字,直到获得单个数字。

for example:input 1 9 {0 1 1 2 3 5 8 13 21}->{0 1 1 2 3 5 8 3 1}->{1 2 5 3}->{2 3}->3 例如:输入1 9 {0 1 1 2 3 5 8 13 21}-> {0 1 1 2 3 5 8 3 1}-> {1 2 5 3}-> {2 3}-> 3

#include <stdio.h>

int main(void) {
    // your code goes here
    int n,j,k,r,o;
    o=0;


//  printf("enter the number of test cases: ");
    scanf("%d",&n);
    int s[n];
    int a;
    a=n;

    while(n!=0)
    {


         r=k;
         int e[k/2];
         int m;
         scanf("%d",&k);//enter say 9
      if(k!=1)
            {
              int i[k];
              i[0]=0;
              i[1]=1;

              for(j=0;j<k;j++)
                  {
                     if(j>1)
                       {
                         i[j]=(i[j-2]+i[j-1])%10;
                       }


                  }


              while(r!=1)
                {
                 m=0;

                 for(j=0;j<r;j++)
                    {

                      if(j!=0)
                       {

                        if(j%2!=0)
                         {
                           e[m]=i[j];
                           m++;
                         }
                       }
                    }

                 for(j=0;j<k/2;j++)
                    {
                      i[j]=e[j];
                    }
                 r=r/2;
                }
             s[o]=e[0];
             o++;
             n--;
           }
        else
           {
             return 0;
           }


    }
    if(k!=1)
      {
        for(j=0;j<a;j++)
         {
             printf("%d\n",s[j]);
         }
      }


    return 0;
}

I want to know which point in the code is trigerring the error i know little about these error(like acessing array beyond bonds) can you explain me that? 我想知道代码中的哪一点正在触发错误,而我对这些错误知之甚少(例如超出键的访问数组),您能向我解释一下吗?

Your k variable is uninitialized when you first access it at 您第一次访问k变量时未对其进行初始化

r=k;
int e[k/2];

This means that it is equal to whatever happened to be stored at that memory location before. 这意味着它等于之前存储在该存储位置的任何内容。 This could be any random number and is very bad. 这可以是任何随机数,非常糟糕。

On the next line you declare an array e[k/2] with size k/2 , but since k was never initialized, this could be any size. 在下一行,您声明大小为k/2的数组e[k/2] ,但是由于k从未被初始化,因此它可以是任何大小。 If k happens to be negative, I get a segmentation fault at that line. 如果k恰好是负数,则在该行出现分段错误。

To fix this issue, you need to initialize all your variables before using them. 要解决此问题,您需要在使用所有变量之前对其进行初始化。

A segmentation fault (SIGSEGV) is what occurs if you try to access memory that isn't allocated to your program by the operating system. 如果尝试访问操作系统未分配给程序的内存,就会发生分段错误(SIGSEGV)。

To help with debugging these errors, run your code in the debugger. 为了帮助调试这些错误,请在调试器中运行代码。 This can then take you directly to the line where the segmentation fault occured. 然后,您可以直接将您带到发生分段错误的行。

just put the scanf("%d",&k); 只需将scanf("%d",&k); above r=k; 高于r=k;

A SIGSEGV is an error(signal) caused by an invalid memory reference or a segmentation fault. SIGSEGV是由无效的内存引用或分段错误引起的错误(信号)。 You are probably trying to access an array element out of bounds or trying to use too much memory. 您可能正在尝试超出范围访问数组元素或尝试使用过多的内存。

also. 也。 i am not sure about the return 0; 我不确定return 0; in the else statement. 在else语句中。 i haven't run the code so i may be mistaken but it should be return 1; 我没有运行代码,所以我可能会弄错,但是应该return 1; instead of return 0; 而不是return 0;

if(j!=0)
{
      if(j%2!=0)
      {
            e[m]=i[j];
            m++;
      }
}

you can replace it with if(j != 0 && j%2 != 0) 您可以将其替换为if(j != 0 && j%2 != 0)

我知道您指的是这些问题...这不是您应该解决的方式...您必须找到O(1)时间中剩下的数字...由于去除了奇数位,我们只需要担心偶数位置...现在,那些偶数位置被重复除以2可能会变得很奇怪...因此,有固定数量的步骤可以将整个数组转换为单个元素...在此的每一点上一步偶数放置数字除以2,因此在这些步骤之间或之后只有一个偶数放置的数字不会变得奇怪...您必须通过看N才能找到该数字,至少我做了一样...这只是一个提示,如果不清楚,我可以在评论中解释更多的逻辑...Upvote😝

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM