c - 这个 c 程序在 windows 中工作正常但在 Linux 中不工作

标签 c linux windows gcc tcc

此 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;
}

给定的输入是

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

这是在 linux 中显示

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

这在窗口中显示完美

2 3 7
-1
5 4
1 8

我在 Windows 和 Linux 中都使用了 gcc 和 tcc 编译器,但在 Linux 中都给出了相同的错误。

无法理解为什么问题会出现在 Linux 中。

最佳答案

如果输入是

1 3 8 
-1

主要

n = 1

arr = memory for 1 long int

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

data array = 3 8 -1

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

arr[i] = some value    

但是 arr 数组将迭代 8 次并尝试分配 arr[i],即使 arr 只有一个元素。

关于c - 这个 c 程序在 windows 中工作正常但在 Linux 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53122062/

相关文章:

c - 为什么我的 shell 不能启动 firefox?

c - scanf 的四边形和参数类型

捕获我们有 wifi 连接的时刻

javascript - IE10 JavaScript 无法在计算机锁定时重新绘制窗口?

c - 条件表达式中的指针/整数类型不匹配

c - 多个生产者/消费者和临界区代码问题

C Linux USB 驱动程序 |打印 URB 缓冲区内容的最佳方式

linux - Dockerfile CMD `command not found`

excel - 打开多个Excel实例时,如何设置Excel实例打开手动打开的文件?

python - 如何使用mingw-w64,Python和pybind11手动构建C++扩展?