-2

I am working on a school project which fills integers arrays with random numbers then sorts them and records the times. I've gotten the sorting functions to work but I'm running into an issue when making the arrays. The project instructions tell us to declare the array with:

int * array = (int *) malloc (arraySizes[i] * sizeof(int));

arraySizes is an array with the lengths of the arrays we are making in them i.e. [1000, 2000, 3000]

When I run the program it does make a random number with the fill function but no matter the data size, the array is initialized with a length of 8. Is there anything I am doing wrong with my array initialization?

Attached below is the function that initializes the array and the function that fills the array with random numbers. The main function is just used for picking between int arrays and string arrays, which I have a group partner working on string arrays.

    void Int_Arrays(){

    //printf("Size          Trial[0]        Trial[1]        Trial[2]        Trial[3]        Trial[4]     Average\n");
       Node *head = NULL;    // for storing times
        int i;
        int j;
        int m;
        for(j = 0; j < numTrials; j++){
            int dataSize;
            dataSize = arraySizes[j];
            printf("DataSize: %d\n", dataSize);// for debugging 
            int * array = (int *) malloc (arraySizes[j] * sizeof(int));
            printf("Inital array size: %d\n", sizeof(array));// for debugging
            printf("%d  ", arraySizes[j]);
        fillArray_int(array, arraySizes[j]);  
        int n = sizeof(array)/sizeof(array[0]);
        //printf("array size: %d\n", sizeof(array));
        for (i = 0; i < sizeof(array); i++){     
                  printf("%d ", array[i]);
        }
        printf("\n");
        for (m = 1; m <= 5; m++){     
        //runSort_Int(array, n); // runs sorting functions
        }
       double avg = avgTime(head); // calculates time averages
      // printf("     %lf", &avg);
       // printf("\n");
        free(array);

   }

}

    void fillArray_int(int arr[], int dataSize)
    {
        printf("Data Size: %d\n", dataSize); // for debugging
        printf("array size: %d\n", sizeof(arr)); // for debugging
        int i = 0;
        srand(time(0));
        while (i < dataSize)
        {
            arr[i] = 0;

             int r = (randNum()% 9) + 1;

            arr[i] = r;
            i++;
        } 

    }
sannic19
  • 1
  • 2

2 Answers2

3

The type of array is int *, so sizeof(array) evaluates to the size of an int *, not what it points to.

The size of the memory block that was allocated it exactly what you passed to malloc, namely arraySizes[j] * sizeof(int). That's something you need to keep track of yourself.

For the same reason, sizeof(array)/sizeof(array[0]); won't do what you expect. This will only work if array has array type, not pointer type.

dbush
  • 205,898
  • 23
  • 218
  • 273
-1
  1. void fillArray_int(int arr[], int dataSize) It is not possible to do it this way. Arrays are passed as reference - ie pointers. And the pointer size in your system is 8 bytes.

2. array is just the pointer - and sizeof of pointer is in your case 8 (64 bits system)

0___________
  • 60,014
  • 4
  • 34
  • 74