c - My program segfaults -
c - My program segfaults -
i've been working on programming assignment seems forever now, , have yet figure out error. every time compile programme input file segmentation fault (core dumped) error. i'm assuming 1 of pointers pointing unallocated space, have no thought one. steer me in right direction?
#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; (x=0; x<last; x++) arrayone[x] = numbers[x + first]; (y= last; y< lastone; y++) arraytwo[y - firstone]; int = 0; int j = 0; int k = first; while ((i< last-first) && (j< lastone - firstone)) { if (arrayone[i] <= arraytwo[j]) { numbers[k] = arrayone[i]; = i+1; } else { numbers[k] = arraytwo[j]; j = j+1; } k = k+1; } while (i < last-first) { numbers[k] = arrayone[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); (i=0; < count-2; i++) if(numbers[i] == numbers[i+1]) homecoming false; *operations = basic_operations; homecoming true; } void read_array(char *file_name, long long int **return_numbers, int *return_size) { file *file; // file pointer used read in file_name int count; int ii; long long int *numbers; file = fopen(file_name, "r"); // open file reading if (file == null) { // error openting file, print error message perror(file_name); // set pointers show failed state *return_numbers = null; *return_size = -1; return; } // read in first line determine how many numbers in list if (fscanf(file, "%d", &count) != 1) { // error openting file, print error message perror("here"); perror(file_name); fclose(file); // set pointers show failed state *return_numbers = null; *return_size = -1; return; } // dynamically allocate array 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 number delimited comma if (fscanf(file, "%lld,", &(numbers[ii])) != 1) { // if char_count == -1, there error reading perror(file_name); free(numbers); fclose(file); // set pointers show failed state *return_numbers = null; *return_size = -1; return; } } fclose(file); // set 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 input file\n"); printf("it assumed first line of input file contains\n"); printf("the count of numbers in file. sec line assume\n"); printf("to 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); } } homecoming 0; }
you array size problem_size
used, never defined. since arrays statically allocated, should compile-time constant (e.g. #define, enum, literal (e.g. 25) , constexpr)
try:
#define problem_size 1024
c gcc segmentation-fault
Comments
Post a Comment