Thread: Array Memory Problem

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    3

    Array Memory Problem

    Hello In bellow code I sorted 4 different ways in several txt file but I can not define more than arr[100000] otherwise windows close program automaticaly. I try to use malloc() but I couldn't also. Maybe m algorithm can be wrong. Do you have any idea
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    
    int *selectionSort(int x[],int len){
        int j,k,temp;
        for (j=0; j<len; j++) {
            int min=x[j], ind=j;
            for (k=j+1; k<len; k++) {
                if(x[k]<min){
                    min=x[k];
                    ind=k;
                }
            }
            if(ind!=j) {
                temp=x[ind];
                x[ind]=x[j];
                x[j]=temp;
            }
        }
        return x;
    }
    
    
    int *bubbleSort(int x[],int len){
     int pass,k,temp;
        for (pass=0; pass<len-1; pass++) {
     for (k=0; k<(len-pass-1); k++) {
     if(x[k]>x[k+1]){
     temp=x[k];
     x[k]=x[k+1];
     x[k+1]=temp;
    
    
     }
      
     }
     
     }
     return x;
    }
    
    
    int *shellSort(int x[], int n){
        int temp,i,last,is_sorted;
        int jump=n/2;
        while(jump >=1){
            last= n-jump;
            is_sorted=0;
            while(!is_sorted){
                is_sorted=1;
                for(i=0; i<last; i++){
                    if(x[i]>x[i+jump]){
                        temp=x[i];
                        x[i]=x[i+jump];
                        x[i+jump]=temp;
                        is_sorted=0;
                    }
                }
            }
            jump=jump/2;
        }
        return x;
    }
    
    
    
    
    int *quicksort(int x[],int first,int last){
        int pivot,j,temp,i;
             if(first<last){
             pivot=first;
             i=first;
             j=last;
    
    
             while(i<j){
                 while(x[i]<=x[pivot]&&i<last)
                     i++;
                 while(x[j]>x[pivot])
                     j--;
                 if(i<j){
                     temp=x[i];
                      x[i]=x[j];
                      x[j]=temp;
                 }
             }
    
    
             temp=x[pivot];
             x[pivot]=x[j];
             x[j]=temp;
             quicksort(x,first,j-1);
             quicksort(x,j+1,last);
            
        }
        return x;
    }
    
    
    void printIntArr(int *arr, int len) {
        int i=0;
        for (i=0; i<len; i++){
            printf("%d ", arr[i]);
        }
        printf("\n");
    }
    
    
    void compare(int *arr1,int *arr2,int *arr3,int *arr4, int len) {
        int i=0;
        int check=0;
            for (i=0; i<len; i++){
            if (arr1[i] != arr2[i] && arr1[i] != arr4[i] && arr1[i] != arr3[i])        
                    check++;
                    break;
                
        }
        if (check !=0) printf("Sorted Arrays are not equal Difference Start at %i . variable in array",i);
        else printf("Sorted Arrays are equal");
    }
    
    
    int main(int argc, const char * argv[]){
        
        const char *fileName[10];
        FILE *fptr[10];         
        float timespent,begin,end;                
        int arr[100000];
        int arr1[100000];
        int arr2[100000];
        int arr3[100000];
        int arr4[100000];
        float quick[10];
        float bubble[10];
        float shell[10];
        float selection[10];
        int *selectionSortarr;
        int *quicksortarr;
        int *bubbleSortarr;
        int *shellSortarr;
        int i,x,z;
        for(i=1;i<argc;i++) {
            int j=0;
            fileName[i]=argv[i];
            fptr[i] = fopen(fileName[i], "r");
            if(fptr[i] == NULL){
                printf("Error!! Cannot open file: %s \n", fileName[i]);
                return EXIT_FAILURE;
            }
            else {
                printf("\nFile is opened successfully : %s \n", fileName[i]);
                while(!feof(fptr[i])) {   
                    if(1 != fscanf(fptr[i], "%i" ,&arr[j])) {
                        break;
                    }
                    ++j;
                } 
                
                for (z = 0; z < j; z++) {
                    
          arr1[z] = arr[z];
          arr2[z] = arr[z];
          arr3[z] = arr[z];
          arr4[z] = arr[z];
       }
            
                
                printf("%s 's number Of inputs:%i",fileName[i],j);
                printf("\n%s starts to sorting\n",fileName[i]);
                
            
                begin= clock();
                quicksortarr = quicksort(arr1,0,j-1);
                end= clock();
                timespent=(float)(end-begin)/CLOCKS_PER_SEC;
                quick[i]=timespent;
    
    
                
                begin= clock();
                shellSortarr = shellSort(arr2,j);
                end= clock();
                timespent=(float)(end-begin)/CLOCKS_PER_SEC;
                shell[i]=timespent;
    
    
    
    
                begin= clock();
                bubbleSortarr = bubbleSort(arr3,j);
                end= clock();
                timespent=(float)(end-begin)/CLOCKS_PER_SEC;
                bubble[i]=timespent;
    
    
    
    
                begin= clock();
                selectionSortarr = selectionSort(arr4,j);
                end= clock();
                timespent=(float)(end-begin)/CLOCKS_PER_SEC;
                selection[i]=timespent;
                printf("%s finished 4 different sorting\n",fileName[i]);
                
            
                compare(quicksortarr,shellSortarr,bubbleSortarr,selectionSortarr,j);
                printf("\n--------------------------------------------------------------------------------------------\n");
    /*//            
    printIntArr(arr, j);
        printIntArr(arr1, j);
        printIntArr(arr2, j);
        printIntArr(arr3, j);
        printIntArr(arr4, j);
        printIntArr(selectionSortarr, j);
        printIntArr(bubbleSortarr, j);
        printIntArr(shellSortarr, j);
        printIntArr(quicksortarr, j);
            compare(arr1,arr2,arr3,arr4,j);
    */
            }  
            
            
        }    
        printf("\nFilename");
        for(i=1;i<argc;i++) {
            printf("\t%s\t", fileName[i]);
        }
    
    
        printf("\nQuick Sort");
        for(i=1;i<argc;i++)
        {
           
           printf("\t%f\t", quick[i]);
            
        }
        printf("\nShell Sort");
        for(i=1;i<argc;i++)
        {
          
           printf("\t%f\t", shell[i]);
           
    
    
        }
        printf("\nBubble Sort");
        for(i=1;i<argc;i++)
        {
          
           printf("\t%f\t", bubble[i]);
           
    
    
        }
        printf("\nSelection Short");
        for(i=1;i<argc;i++)
        {
           
           printf("\t%f\t", selection[i]);
           
    
    
        }
        printf("\n");
    printf("\n--------------------------------------------------------------------------------------------\n");
    
    
    
    
    
    
        for(i=1;i<argc;i++){
            
             fclose(fptr[i]);
             
             printf("%s is closed\n",fileName[i]);
            
        }
        return 0;    
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    There's no point in "returning" the array from your sort functions. The input array is sorted in place so the modified array is available without "returning" it. So your sort functions should all have a void return type.

    And your program has too much repetition. You could organize it something like the following. quickSort needs the same signature as the others, so it needs a starter function. And simply checking that the array is properly sorted is as good as (better than) comparing all the sorted arrays to each other (which you're doing incorrectly, anyway).
    Code:
    void quickSort(int x[], int len) {
        quickSort2(x, 0, len - 1);
    }
    
    void check_sort(int *a, int len) {
        int i;
        for (i = 1; i < len; i++)
            if (a[i - 1] > a[i])
                break;
        if (i < len) printf("!!!! BAD SORT !!!!\n");
    }
    
    double sort(void (*func)(int*, int), int *a, int len) {
        time_t end, begin;
        int *b = malloc(len * sizeof *b);
        if (!b) { perror("sort: malloc"); return 0; }
        memcpy(b, a, len * sizeof b[0]);
        begin = clock();
        func(b, len);
        end = clock();
        check_sort(b, len);
        free(b);
        return (double)(end - begin) / CLOCKS_PER_SEC;
    }
    
    void do_sorts(FILE *f) {
        int *a, len = 0, input;
        while (fscanf(f, "%d", &input) == 1) len++;
        a = malloc(len * sizeof *a);
        if (!a) { perror("do_sorts: malloc"); return; }
        len = 0;
        rewind(f);
        while (fscanf(f, "%d", &input) == 1) a[len++] = input;
        printf("quickSort:     %f\n", sort(quickSort,     a, len));
        printf("shellSort:     %f\n", sort(shellSort,     a, len));
        printf("bubbleSort:    %f\n", sort(bubbleSort,    a, len));
        printf("selectionSort: %f\n", sort(selectionSort, a, len));
        free(a);
    }
    
    int main(int argc, char *argv[]) {
        int i;
        for (i = 1; i < argc; i++) {
            FILE *f = fopen(argv[i], "r");
            if (!f) { perror(argv[i]); continue; }
            printf("%s:\n", argv[i]);
            do_sorts(f);
            fclose(f);
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    3
    Quote Originally Posted by algorism View Post
    There's no point in "returning" the array from your sort functions. The input array is sorted in place so the modified array is available without "returning" it. So your sort functions should all have a void return type.

    And your program has too much repetition. You could organize it something like the following. quickSort needs the same signature as the others, so it needs a starter function. And simply checking that the array is properly sorted is as good as (better than) comparing all the sorted arrays to each other (which you're doing incorrectly, anyway).
    Code:
    void quickSort(int x[], int len) {
        quickSort2(x, 0, len - 1);
    }
    
    void check_sort(int *a, int len) {
        int i;
        for (i = 1; i < len; i++)
            if (a[i - 1] > a[i])
                break;
        if (i < len) printf("!!!! BAD SORT !!!!\n");
    }
    
    double sort(void (*func)(int*, int), int *a, int len) {
        time_t end, begin;
        int *b = malloc(len * sizeof *b);
        if (!b) { perror("sort: malloc"); return 0; }
        memcpy(b, a, len * sizeof b[0]);
        begin = clock();
        func(b, len);
        end = clock();
        check_sort(b, len);
        free(b);
        return (double)(end - begin) / CLOCKS_PER_SEC;
    }
    
    void do_sorts(FILE *f) {
        int *a, len = 0, input;
        while (fscanf(f, "%d", &input) == 1) len++;
        a = malloc(len * sizeof *a);
        if (!a) { perror("do_sorts: malloc"); return; }
        len = 0;
        rewind(f);
        while (fscanf(f, "%d", &input) == 1) a[len++] = input;
        printf("quickSort:     %f\n", sort(quickSort,     a, len));
        printf("shellSort:     %f\n", sort(shellSort,     a, len));
        printf("bubbleSort:    %f\n", sort(bubbleSort,    a, len));
        printf("selectionSort: %f\n", sort(selectionSort, a, len));
        free(a);
    }
    
    int main(int argc, char *argv[]) {
        int i;
        for (i = 1; i < argc; i++) {
            FILE *f = fopen(argv[i], "r");
            if (!f) { perror(argv[i]); continue; }
            printf("%s:\n", argv[i]);
            do_sorts(f);
            fclose(f);
        }
        return 0;
    }

    Thank you so much I understand malloc concept with you but In this code my purpose not checking sort is true or not I want to check 4 different sorting result array is same or not ın your code I can't edit for this because I am not pro like you

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Hello In bellow code I sorted 4 different ways in several txt file but I can not define more than arr[100000] otherwise windows close program automaticaly. I try to use malloc() but I couldn't also. Maybe m algorithm can be wrong. Do you have any idea
    Look at these
    Code:
        int arr[100000];
        int arr1[100000];
        int arr2[100000];
        int arr3[100000];
        int arr4[100000];
    The total size of these 5 arrays is 2,000,000 bytes.
    windows default stack size - Google Search

    The quick fix is to declare them all as static (which preserves the scope, but allocates them off the stack).
    Code:
        static int arr[100000];
        static int arr1[100000];
        static int arr2[100000];
        static int arr3[100000];
        static int arr4[100000];
    The malloc equivalent of
    int arr[size];
    is
    int *arr = malloc( size * sizeof(*arr) );

    You carry on using arr[i] and function(arr) as you would as if it were an array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 1
    Last Post: 03-26-2010, 12:50 AM
  3. 4D/5D array memory allocation problem
    By JonnyThewlis in forum C Programming
    Replies: 2
    Last Post: 11-13-2009, 06:28 AM
  4. problem with sending array of struct over shared memory
    By jet-plane in forum C Programming
    Replies: 26
    Last Post: 05-10-2008, 04:10 AM
  5. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 11:34 AM

Tags for this Thread