2014-10-28 60 views
-4

我一直在做这个编程任务,看起来像现在一样,我还没有弄清楚我的错误。每次我编译程序,然后输入一个文件,我得到一个分段错误(核心转储)错误。我假设我的一个指针指向未分配的空间,但我不知道哪一个。有人能把我引向正确的方向吗?我的程序段错误

#include <stdio.h> 
#include <stdlib.h> 
#include <strings.h> 
#include <sys/time.h> 
#define FALSE 0 
#define TRUE !FALSE 
int problem_size; 

int Merge (long long int *numbers, int first, int last, int firstOne, int lastOne) 
{ 
    long long int arrayOne[problem_size]; 
    long long int arrayTwo[problem_size]; 
    int x,y; 

    for (x=0; x<last; x++) 
     arrayOne[x] = numbers[x + first]; 

    for (y= last; y< lastOne; y++) 
     arrayTwo[y - firstOne]; 

    int i = 0; 
    int j = 0; 
    int k = first; 

    while ((i< last-first) && (j< lastOne - firstOne)) 
    { 
     if (arrayOne[i] <= arrayTwo[j]) 
     { 
      numbers[k] = arrayOne[i]; 
      i = i+1; 
     } 
     else 
     { 
      numbers[k] = arrayTwo[j]; 
      j = j+1; 
     } 
     k = k+1; 
    } 

    while (i < last-first) 
    { 
     numbers[k] = arrayOne[i]; 
     i = i+1; 
     k = k+1; 
    } 

    while(j < lastOne - firstOne) 
    { 
     numbers[k] = arrayTwo[j]; 
     j = j + 1; 
     k = k + 1; 
    } 
} 

int MergeSort (long long int *numbers, int low, int high) 
{ 
    if (high-low > 1) 
    { 
     int lowest = low; 
     int highest = ((high-low)/2) + low; 
     int lowestOne = highest; 
     int highestOne = high; 

     MergeSort(numbers, lowest, highest); 
     MergeSort(numbers, lowestOne, highestOne); 
     Merge(numbers, lowest, highest, lowestOne, highestOne); 
    } 
} 

int unique3(long long int *numbers, int count, long long int *operations) { 
    long long int basic_operations = 0; 
    int i; 

    MergeSort(numbers,0,count); 

    for (i=0; i < count-2; i++) 
     if(numbers[i] == numbers[i+1]) 
      return FALSE; 
    *operations = basic_operations; 
    return TRUE; 
} 


void read_array(char *file_name, long long int **return_numbers, int *return_size) { 
    FILE *file; // file pointer used to read in file_name 

    int count; 
    int ii; 
    long long int *numbers; 

    file = fopen(file_name, "r"); // open the file for reading 
    if (file == NULL) { 
     // error openting the file, print error message 
     perror(file_name); 

     // set pointers to show failed state 
     *return_numbers = NULL; 
     *return_size = -1; 
     return; 
    } 

    // read in the first line to determine how many numbers are in the list 
    if (fscanf(file, "%d", &count) != 1) { 
     // error openting the file, print error message 
     perror("HERE"); 
     perror(file_name); 

     fclose(file); 

     // set pointers to show failed state 
     *return_numbers = NULL; 
     *return_size = -1; 
     return; 
    } 

    // dynamically allocate an array to contain count number of long long ints 
    numbers = (long long int *)malloc(sizeof(long long int)*count); 

    // read in count numbers 
    for(ii=0; ii < count; ii++) { 
     // read in a number delimited by a comma 
     if (fscanf(file, "%lld,", &(numbers[ii])) != 1) { 
      // if char_count == -1, there was an error reading 
      perror(file_name); 

      free(numbers); 
      fclose(file); 

      // set pointers to show failed state 
      *return_numbers = NULL; 
      *return_size = -1; 
      return; 
     } 
    } 

    fclose(file); 

    // set up pointer values 
    *return_numbers = numbers; 
    *return_size = count; 
} 

void timing(long long int *numbers, int count, int (*algorithm)(long long int *, 
     int, long long int *), long long int *operations, int *unique, 
     long long int *ms_time) { 
    struct timeval start_tv; 
    struct timeval end_tv; 

    gettimeofday(&start_tv, NULL); 
    *unique = algorithm(numbers, count, operations); 
    gettimeofday(&end_tv, NULL); 

    *ms_time = (end_tv.tv_sec - start_tv.tv_sec)*1000000L + (end_tv.tv_usec - 
     start_tv.tv_usec); 
} 

int main(int argc, char *argv[]) { 
    if(argc != 2) { 
     printf("Invalid Number of Arguments\n"); 
     printf("Usage:\n"); 
     printf("%s <file_name>\n", argv[0]); 
     printf("\tfile_name - name of the input file\n"); 
     printf("It is assumed that the first line of the input file contains\n"); 
     printf("the count of the numbers in the file. The second line is assume\n"); 
     printf("to be a comma separated list of integers.\n"); 
    } 
    else { 
     int count; 
     long long int *numbers; 
     long long int operations = 0; 
     int is_unique; 
     long long int ms_time; 
     read_array(argv[1], &numbers, &count); 

     if(count > 0){ 
      printf("Here"); 
      printf("%d ", count); 
      timing(numbers, count, unique1, &operations, &is_unique, &ms_time); 
      printf("(%d,%lld,%lld) ", is_unique, operations, ms_time); 
     } 
    } 
    return 0; 
} 
+4

您是否尝试过使用调试工具?像GDB一样? – 2014-10-28 00:26:33

+1

注意:'int MergeSort()'和'int Merge()'缺少返回'int'。 – chux 2014-10-28 00:27:09

+0

这是C代码还是C++代码? – 2014-10-28 00:37:42

回答

1

您使用了数组大小problem_size,但从未定义。由于您的数组是静态分配的,这也应该是一个编译时间常数(例如#定义,枚举文字(如25),constexpr)

尝试:

#define problem_size 1024 
+0

进行修正后,它不再立即崩溃,而是经过第一个输入,然后崩溃。有任何想法吗? – Andrew 2014-10-28 00:58:40

+1

是的 - 使用调试器。 – 2014-10-28 03:22:34