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

时间:2018-11-02 16:03:39

标签: c linux windows gcc tcc

此c程序在Windows中运行正常,但在Linux中显示段错误。

function myArrayMax(arr) {
var len = arr.length;
var max = -Infinity;
while (len--) { // here itself you are decrementing the value of len
  if (arr[len-1] > max) { // here also you are checking the previous one. Then you won't get chance to check the last array element.
    max = arr[len-1];
  }
}
return max;
}

给定的输入为

import Foundation import PlaygroundSupport let headers = ["content-type": "application/json", "Authorization": "Bearer Valid-Github-Personal-Access-Token"] let parameters = [ "query" : "query { viewer { login } }"] as [String : Any] let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) let request = NSMutableURLRequest(url: NSURL(string: "https://api.github.com/graphql/")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData as Data let session = URLSession.shared let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if (error != nil) { print(error) } else { let httpResponse = response as? HTTPURLResponse print(httpResponse) } }) dataTask.resume() PlaygroundPage.current.needsIndefiniteExecution = true

这在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 elemnt
                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 coloumn
            }
            if ((pos[i] == n - 1) && (rept[i] == pow(n, r-i-1))) //If it is repeated a specific number of time then chnage 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;
}

这在Windows中完美显示

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

我在Windows和Linux中都使用gcc和tcc编译器,但是在Linux中都给出了相同的错误。 无法理解为什么问题在Linux中显示。

2 个答案:

答案 0 :(得分:0)

如果输入为

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

arr[i] = some value    

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

答案 1 :(得分:0)

对于以以下内容开头的行:

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

`i'有时为0(在10 3 3的输入上),因此此时将pos [-1]设置为值-即:设置内存时不应设置为稍后会干扰free,因为malloc使用指针之前的值获取空闲信息。

为了验证,如果我在if compare之前添加了打印并运行您的示例:

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

它会在出现错误之前在多个位置打印出来。