尝试运行通过命令行输入输入的排序C程序(Mac终端)

时间:2018-07-21 12:39:09

标签: c sorting terminal

尝试运行排序C程序,该程序通过命令行(Mac Terminal)输入。如果我手动输入数据,程序将运行。如果我从命令行输入数据(即时间./hw2 mergesort <10000.txt),则会收到错误消息:

  

hw2(1368,0x7fffcf79b3c0)malloc: * mach_vm_map(size = 18446744065119617024)失败(错误代码= 3)   * 错误:无法分配区域   ***在malloc_error_break中设置断点以进行调试   错误:malloc的大小失败:-2147483648   真正的1m41.341s   用户1m38.316s   sys 0m2.406s

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


    #define DEFAULT_SIZE    1024

int merge_sort(int arr[],int low,int high);
int merge(int arr[],int l,int m,int h);


    int* read_input(int* size)
// read input from stdin into array a; return size of array in size parameter

{
    int *a = NULL;
    int next = 0;
    int sz = DEFAULT_SIZE;

    a = malloc(sizeof(int) * sz);
    if (a == NULL)
    {
        fprintf(stderr, "ERROR: malloc failed for size: %d\n", sz);
        exit(1);
    }

    while (!feof (stdin))
    {
        int i = 0;
        if (scanf ("%d", &i) == EOF)
        {
            break;
        }

        a[next++] = i;

        // reached end of array--double size and allocate again;
        if (next == sz)
        {
            sz = 2 * sz;
            a = realloc(a, sizeof(int) * sz);
            if (a == NULL)
            {
                fprintf(stderr, "ERROR: malloc failed for size: %d\n", sz);
                exit(1);
            }
        }
    }

    *size = next;

    printf("READ %d elements into array\n", next);

    return a;
}


int merge_sort(int arr[],int low,int high)
{
    int mid;
    if(low<high)
    {
        mid=(low+high)/2;
        // Divide and Conquer
        merge_sort(arr,low,mid);
        merge_sort(arr,mid+1,high);
        // Combine
        merge(arr,low,mid,high);
    }

    return 0;
}

int merge(int arr[],int l,int m,int h)
{
    int arr1[10],arr2[10];  // Two temporary arrays to
    //  hold the two arrays to be merged
    int n1,n2,i,j,k;
    n1=m-l+1;
    n2=h-m;

    for(i=0;i<n1;i++)
    arr1[i]=arr[l+i];
    for(j=0;j<n2;j++)
    arr2[j]=arr[m+j+1];

    arr1[i]=9999;  // To mark the end of each temporary array
    arr2[j]=9999;

    i=0;j=0;
    for(k=l;k<=h;k++)  //process of combining two sorted arrays
    {
        if(arr1[i]<=arr2[j])
        arr[k]=arr1[i++];
        else
        arr[k]=arr2[j++];
    }

    return 0;
}


int do_merge_sort(int a[], int size)
{

    printf("BEGIN merge_sort...\n");

    merge_sort(a,0, size);

    printf("END merge_sort...\n");

    return 0;
}






    int do_heap_sort(int a[], int size)
// heapsort driver function

{

    printf("BEGIN heap_sort...\n");

    // TO BE FILLED IN

    printf("END heap_sort...\n");

    return 0;
}

// qiocksort driver function
    int do_quick_sort(int a[], int size)


    {

    printf("BEGIN quick_sort...\n");

    // TO BE FILLED IN

    printf("END quick_sort...\n");

    return 0;
}


    int usage()

{

    char *usage_str =
    "./hw2 [-h] mergesort|heapsort|quicksort\n"
    "\n"
    "Driver program to test different sort algorithn performance.\n"
    "\n"
    "Example\n"
    "\n"
    "./hw2 mergesort\n"
    "\n"
    "will test mergesrt\n"
    ;

    fprintf(stderr, "%s\n\n", usage_str);

    exit(1);
}


    int main(int argc, char *argv[])
    // driver function

{
    int *a = NULL;
    int size;
    int ret = 0;

    if (argc < 2)
    {
        fprintf(stderr, "ERROR: at least one argument needed\n");
        usage();
    }

    // read the input into array;
    a = read_input(&size);

    if (strcmp(argv[1], "mergesort") == 0) {
        do_merge_sort(a, size);
    }
    else if (strcmp(argv[1], "heapsort") == 0) {
        do_heap_sort(a, size);
    }
    else if (strcmp(argv[1], "quicksort") == 0) {
        do_quick_sort(a, size);
    }
    else {
        fprintf(stderr, "ERROR: BAD argument\n");
        usage();
    }

    // free allocated memory
    if (a) {
        free(a);
    }

    exit(0);
}

我使用以下python代码生成随机数数据:

#! usr/env/bin python
# to generate random data:
# python ./gen_data.py 1 1000000 > 1000000.dat
#
# you can verify that data by
# cat 1000000.dat | sort -g >1000000s.dat
# vi 1000000s.dat
#

import sys
import random

start_num = int(sys.argv[1])
end_num = int(sys.argv[2])

data = range(start_num, end_num)
random.shuffle(data)

for x in range(len(data)):
#    print(str(data[x]) + '\n')
    print(data[x])

2 个答案:

答案 0 :(得分:0)

您的循环的结束条件有可能不再得到满足。这会导致sz反复翻倍,直到作为一个Int循环回到负值为止。然后,您的内核抱怨说它无法执行分配占用负空间的内存块的请求。

答案 1 :(得分:0)

malloc调用告诉您给定值(如果将其强制转换为带符号类型,因为显然-1表示红色,因为无符号,所以给出了一个超大长号),您应该使用{{ 1}},当调用请求size_t的函数时。

考虑使用size_t调试程序并查看“奇怪的调用”在哪里,我们不能仅仅通过说“它不起作用请帮助”来帮助您。

因此,我认为您给malloc的值有点超出了它的类型大小,您执行的循环过多,导致valgrind过多。该值越来越大,在某些时候,malloc无法请求这么多的内存。