简体   繁体   English

如何在我的 do-while 循环中解决此分段错误?

[英]How to resolve this segmentation fault inside my do-while loop?

You have to transport a maximum of 15 different loads from one port to another.您必须将最多 15 种不同的货物从一个港口运输到另一个港口。 Carrying capacity of a cargo ship, which will transport those loads, is 50 tons.运输这些货物的货船的承载能力为 50 吨。 Loads are enumerated and the information about the weight of every load is given as input.枚举负载,并给出有关每个负载重量的信息作为输入。

Suppose that the weight of every load is smaller than or equal to 50 tons and greater than 0.假设每件货物的重量小于等于 50 吨且大于 0。

You will read the weight of every load from the input in a single line.您将在一行中从输入中读取每个负载的重量。 Your input will end with a -1.您的输入将以 -1 结尾。 You will print the number of trips necessary.您将打印必要的旅行次数。

Sample Input:样本输入:

 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 -1

Output: Output:

 15

But I get this error and whenever I give input to program但我得到这个错误,每当我给程序输入

./vpl_execution: line 5: 15093 Bus error               (core dumped) ./main

By the way I seached about this situation, I think I am not out of array index or wrong using wrong pointers.顺便说一句,我对这种情况进行了搜索,我认为我并没有超出数组索引或使用错误的指针错误。 Also, I saw same question in here with some solution but I want to know why code is not working and then solve the question on my own.另外,我在这里看到了同样的问题和一些解决方案,但我想知道为什么代码不起作用,然后自己解决问题。 Thank you so much for your help, all.非常感谢大家的帮助。

#include <stdio.h>
int main()
{   int w,i,t,sum,index;
    int list[16];
    w = 1;
    do
    {   
        scanf("%d",&w);
        list[index] = w;
        index++;
    }while(w >= 0);
    t = 0;
    for(i = 0;i < ((sizeof(list)/sizeof(list[0]))+1);i++)
    {
        sum =0;
        if(sum <= 50)
        {sum += list[i];}
        else
        {t++;}
    }

    printf("%d",t);
    return 0;
}

I get a segmentation error on my do while loop我的 do while 循环出现分段错误

The first segmentation fault happens at this line:第一个分段错误发生在这一行:

list[index] = w;

What do you think is the value of index ?您认为index的价值是多少?

You did not initialize it with a value, which probably should have been 0 .您没有使用值初始化它,该值可能应该是0

Therefore, accessing list[index] is undefined behaviour.因此,访问list[index]是未定义的行为。 In your case, it caused a segmentation fault.在您的情况下,它导致了分段错误。

Then inside for(i = 0;i < ((sizeof(list)/sizeof(list[0]))+1);i++)然后在for(i = 0;i < ((sizeof(list)/sizeof(list[0]))+1);i++)

Accessing list[i] here can cause another segfault at the final value of i .在此处访问list[i]可能会导致i的最终值出现另一个段错误。 You should remove the +1 from ((sizeof(list)/sizeof(list[0]))+1) .您应该从((sizeof(list)/sizeof(list[0]))+1)中删除+1

Solution:解决方案:

  1. Do index = 0;index = 0; once (initialization) before doing list[index] = w;在执行list[index] = w;

  2. Remove the +1 from ((sizeof(list)/sizeof(list[0]))+1)((sizeof(list)/sizeof(list[0]))+1)删除+1

But it would be better to change但是最好改一下

for(i = 0;i < ((sizeof(list)/sizeof(list[0])));i++) to for(i = 0;i < ((sizeof(list)/sizeof(list[0])));i++)

for (i = 0; i < 15; ++i)

Because you already know the size of your list and list[15] is guaranteed to be -1 if there are 15 weights.因为您已经知道list的大小,如果有 15 个权重,则list[15]保证为-1 So, you just need to traverse upto list[14] .所以,你只需要遍历到list[14]

This just removes the segfault, but there are still other problems in your code.这只是删除了段错误,但您的代码中仍然存在其他问题。

I want to know why code is not working and then solve the question on my own.我想知道为什么代码不起作用,然后自己解决问题。

The logic inside your for-loop is wrong.你的 for 循环中的逻辑是错误的。

sum =0;
if(sum <= 50)

This condition is always true, and your else block which increments the value of t is never executed.此条件始终为真,并且永远不会执行增加t值的 else 块。 Therefore, the output is always the initial value which you assign to t .因此, output 始终是您分配给t的初始值。

first you have used an uninitialized variable index use index=0 .首先,您使用了未初始化的变量index使用index=0

also as question said a maximum of 15 different , but you are using ((sizeof(list)/sizeof(list[0]))+1) which aside from +1 at the end of it,which will cause passing boundaries of your array which will lead to undefined behavior, is wrong, because you will traverse array completely while it's possible that you have less elements in array .也正如问题所说a maximum of 15 different ,但是您使用的是((sizeof(list)/sizeof(list[0]))+1) ,除了末尾的+1之外,这将导致您的边界通过将导致未定义行为的数组是错误的,因为您将完全遍历数组,而array中的元素可能较少。 you should add a counter while you are scanning data and your second loop should be based on that.您应该在扫描数据时添加一个计数器,并且您的第二个循环应该基于此。

also your code is different from question, if input is always 0 < input <= 50 you will never enter else statement ( t++ ) and you will always print 0 .您的代码也与问题不同,如果 input 始终为0 < input <= 50您将永远不会输入else statementt++ )并且您将始终打印0 you are finding sum of weights and not count of them, and with the input explained in question you will always print t=0 .您正在找到权重的sum而不是它们的计数,并且通过所解释的输入,您将始终打印t=0

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

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