简体   繁体   English

这个C程序在Windows中可以正常工作,但在Linux中不能正常工作

[英]This c program works fine in windows but not working in Linux

This c program is working fine in windows but showing segment fault in Linux. 此C程序在Windows中运行良好,但在Linux中显示段错误。

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

void comb(long int *arr,long int n,long int r,long int stick)
{
    long int check=1,sum =0;
    int poscheck = 0,status = 0;
    long int *temp = malloc(r * sizeof(long int));
    long int *pos = malloc(r * sizeof(long int));
    long int *rept = malloc(r * sizeof(long int));
    memset(pos, 0, r*sizeof(long int));
    memset(rept, 0, r*sizeof(long int));

    while (check <= pow(n,r))
    {
        for (long int i = 0; i < r; i++) //for making the number of array
        {
            for(long int j = 0; j < r; j++) //For checking that no number is repeating.
            {
                if(i == j) continue; //for skip checking of the same element
                else if(pos[i] == pos[j])
                {
                    poscheck = 1;
                    break;
                }
            }
            if(poscheck == 1) break;
            temp[i] = arr[pos[i]];
            sum += temp[i];
        }
        if((sum == stick) && poscheck == 0)
        {
            for(long int i = 0 ; i< r ; i++)
            {
                printf("%ld ",temp[i]);
            }
            status = 1;
            printf("\n");
            break;
        }
        sum = 0,poscheck = 0;
        for (long int i = 0; i < r; i++)
        {
            if (pos[i] == n - 1)
            {
                rept[i]++; //To check how much time the number is repeated in a column
            }
            if ((pos[i] == n - 1) && (rept[i] == pow(n, r-i-1))) //If it is repeated a specific number of time then change the value of it's previous position
            {
                if (pos[i - 1] == n - 1) //if the previous number is the last number then it will start the series again 
                {
                    pos[i - 1] = 0;
                }
                else
                    pos[i - 1]++; //If the previous number is not the last number of series then go to the next number
                rept[i] = 0;
            }
        }
        if (pos[r - 1] < n - 1) //for go to the next number of series in the last line
        {
            pos[r - 1]++;
        }
        else
        {
            pos[r - 1] = 0; //if it is the last number of series then start form the first again
        }
        check++;
    }
    if(status == 0)
    {
        printf("-1\n");
    }
    free(pos); //Does not know why this is showing "double free or corruption (out)" in linux but working in windows.
    free(rept);
    free(temp);
} 
int main()
{
    long int n,data[3],j=0;
    scanf("%ld",&n);
    long int *arr = malloc(n*sizeof(long int));
    while(j < n)
    {
        for(long int i = 0; i< 3; i++)
        {
            scanf("%ld",&data[i]);
        }
        for(long int i = 0; i < data[1]; i++)
        {
            arr[i] = i+1;
        }
        comb(arr,data[1],data[2],data[0]);
        j++;
    }
    free (arr);
    return 0;
}

The given input is 给定的输入是

12 8 3
10 3 3
9 10 2
9 10 2

This is showing in linux 这在Linux中显示

1 3 8 
-1
munmap_chunk(): invalid pointer
Aborted (core dumped)

This is showing perfectly in windows 这在Windows中完美显示

2 3 7
-1
5 4
1 8

I used gcc and tcc both compiler in windows and Linux but both are giving same error in Linux. 我在Windows和Linux中都使用gcc和tcc编译器,但是在Linux中都给出了相同的错误。

Can't understand why the problem is showing in Linux. 无法理解为什么问题在Linux中显示。

If the input is 如果输入是

1 3 8 
-1

in main 在主要

n = 1

arr = memory for 1 long int

in for(long int i = 0; i< 3; i++) 在for(long int i = 0; i <3; i ++)中

data array = 3 8 -1

in: for(long int i = 0; i < data[1]; i++) 于:for(long int i = 0; i <data [1]; i ++)

arr[i] = some value    

But arr array will iterate 8 times and attempt to assign arr[i], even though arr has one element. 但是,即使arr有一个元素,arr数组也会迭代8次并尝试分配arr [i]。

for the lines starting with: 对于以以下内容开头的行:

if (pos[i - 1] == n - 1) 

`i' will be 0 at times (on the input of 10 3 3) and so at that point you are setting pos[-1] to values - ie: setting memory you shouldn't be to something which is then interfering with the free later on as malloc uses values before the pointer for free information. “ i”有时为0(在10 3 3的输入上),因此此时您将pos [-1]设置为值-即:设置内存时,您不应设置为干扰了稍后释放free,因为malloc使用指针之前的值获取空闲信息。

To validate,, if I added a print before the if compare and run your example: 为了验证,如果我在if比较之前添加了打印件并运行您的示例,请执行以下操作:

if(i==0) printf("bad I pos\n");

It prints out in a number places before having the error. 在出现错误之前,它会在许多地方打印出来。

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

相关问题 为什么这个C程序在Windows上崩溃并且在linux上工作正常? - Why is this C program crashes on windows and works fine on linux? 我的程序在Windows机器上崩溃,但在Linux上运行正常 - My program crashes on the Windows machine yet it works fine on the Linux C程序在Windows中表现出奇怪的值,但在Ubuntu中运行良好 - C program is behaving giving weird values in Windows but works fine in Ubuntu Windows上的C程序可以在Linux上执行段错误 - c program works on windows take segmantation fault on linux C code works fine when run on turbo c with windows but goes to infinite loop in gcc linux [on hold] - C code works fine when run on turbo c with windows but goes to infinite loop in gcc linux [on hold] C:Program可以正常运行,但是与gdb一起崩溃 - C:Program works fine but with gdb it crashes 为什么使用基本索引 1 C 程序可以正常工作? - Why with base index 1 C program works fine? 程序可以在Windows IDE中编译并正常运行,但在Linux中则不能-C语言 - Program Compiles and Runs Fine in Windows IDE, But Won't In Linux - C Language C:realloc在Linux上有效,但在Windows上无效 - C: realloc works on Linux, but not on Windows “ Web浏览器”程序可在Windows中运行,但不适用于Linux - 'web browser' program works in Windows but not Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM