我一直在为这个编程任务工作,现在看起来像是永远,我还没有弄清楚我的错误。每次我编译程序然后输入一个文件我得到一个分段错误(核心转储)错误。我假设我的一个指针指向未分配的空间,但我不知道哪一个。有人能引导我朝着正确的方向前进吗?
#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;
}
答案 0 :(得分:1)
您使用了数组大小problem_size
,但从未定义过。由于你的数组是静态分配的,它也应该是一个编译时常量(例如#define,enum,literal(例如25),constexpr)
尝试:
#define problem_size 1024