简体   繁体   English

为什么在C中此程序存在分段错误错误?

[英]why Segmentation fault error in this program in c?

Why is dr segmentation fault in this code? 为什么在此代码中出现dr细分错误? Everything look correct here, syntax ... etc.The program is simple,just two sort content of two arrays into a third array; 程序看起来很简单,只是将两个数组的两个内容放入第三个数组; So I have taken two arrays array1 and array2 and the third one is array in which sorting is to be done. 因此,我选择了两个数组array1array2 ,第三个是要在其中进行排序的array

#include<stdio.h> 

int main()
{
    int array1[10] = {1, 2, 4,5,7,8,45,21,78,25};
    int array2[5] = {3, 6, 9,15,17};
    int array[20];
    int i,j,temp;
    int l1 = sizeof(array1)/sizeof(int);
    int l2 = sizeof(array2)/sizeof(int);
    int l3 = l1+l3;

    for (i = 0;i < l1; i++) 
    {
        array[i]=array1[i];
    }
    for (i = 0;i < l2; i++) 
    {
        array[i+l1]=array2[i];
    }
    for (i = 0;i < (l1+l2); i++) 
    {
        printf("%d\n", array[i]);
    }
    printf("\nSorted array:\n");

    for(i=0;i<l3;i++)
    {
        for(j=i;j<l3;j++)
        {
            if(array[i] > array[j])
            {
                temp=array[i];
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
    for (i = 0;i < l3; i++) 
    {
        printf("%d\n", array[i]);
    }

    return 0;
}

Because this is not what you want: 因为不是您想要的:

int l3 = l1 + l3;

It will simply add the known l1 to the arbitrary l3 , giving you a slightly larger arbitrary value. 它将简单地将已知的l1添加到任意l3 ,从而为您提供一个稍大的任意值。 Instead, it should be: 相反,它应该是:

int l3 = l1 + l2;

The other, though relatively minor, problem you have is the efficiency of the algorithm, specifically the start and end conditions of the loops. 另一个尽管相对较小的问题是算法的效率,特别是循环的开始和结束条件。 The code: 编码:

for (i = 0; i < l3; i++) {
    for (j = i; j < l3; j++) {

has two problems. 有两个问题。 First, the i loop goes too far since we know that when it's at l3 - 1 , there are no elements to the right. 首先, i循环太远了,因为我们知道当它位于l3 - 1 ,右边没有元素。 Secondly, the j loop starts at i and we know that array[x] > array[x] can never be true ( x because i == j ). 其次, j循环从i开始,我们知道 array[x] > array[x]永远不可能为真( x因为i == j )。

It would be better to use: 最好使用:

for (i = 0; i < l3 - 1; i++) {
    for (j = i + 1; j < l3; j++) {

to remove these inefficiencies. 消除这些低效率。

In the line: 在该行中:

// l3 will have garbage value which might be a greater than 20(max size of your array) // l3的垃圾值可能大于20(数组的最大大小)

int l3 = l1+l3; 整数l3 = l1 + l3;

When you are using this in the loops 当您在循环中使用它时

for(i=0;i<l3;i++)
{
    for(j=i;j<l3;j++)
    {
        if(array[i] > array[j])
        {
            temp=array[i];
            array[i]=array[j];
            array[j]=temp;
        }
    }
}
for (i = 0;i < l3; i++) 
{
    printf("%d\n", array[i]);
}

l3 value may be more than 20 say 30 when accessing the 30th location of the array will lead to segmentation fault. 当访问数组的第30个位置时,l3值可能大于20,例如30,这将导致分段错误。

Because you are exceeded the size of array 因为超出数组的大小

check this instruction: int I3 = I1+I3 you should replace it by int I3 = I1+I2 检查此指令: int I3 = I1+I3您应将其替换为int I3 = I1+I2

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

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