Thread: dynamic memory and double pointers

  1. #1
    Registered User
    Join Date
    Nov 2011

    dynamic memory and double pointers

    i've finished a project requiring me to complete a few simple tasks without having any statements in my main besides my function calls and my return 0; statement.

    My problem is that in my getArray function, the second to last line is a printf that prints a space. Regardless of what is inside this printf statement, if I attempt to delete it, my program will not run. I get a message from windows saying that myproject.exe has encountered a problem and needs to close. I have no idea what I am doing wrong. Is there someone that can help me? Thank you.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #define MAX_SIZE 1500
    #define BINDATA "E:/231A4bindata.dat"
    #define EPSILON .01
    void getArray (double **data, int *numValues);
    void sortArray (double **data, int *numValues);
    void printArray (double **data, double *average, int *numValues);
    void hiLoEqual (double **data, double *average, int *numValues);
    void stdDev (double **data, double *average, int *numValues);
    void getMedian (double **data, int *numValues);
    void getMode (double **data, int *numValues);
    int main() {
        double *data, average;
        int numValues;
        getArray(&data, &numValues);
        sortArray(&data, &numValues);
        printArray(&data, &average, &numValues);
        hiLoEqual (&data, &average, &numValues);
        stdDev (&data, &average, &numValues);
        getMedian (&data, &numValues);
        getMode (&data, &numValues);
        return 0; 
    /*Function getArray dynamically declares an array of 1500 doubles
      and fills by reading from a binary file. Array is resized to 
    void getArray (double **data, int *numValues) {
        FILE * readfile;
        int ndx = 0, rowCount = 0;
        int dblsize = sizeof(double);
        *data = (double *)malloc(MAX_SIZE * dblsize);
        readfile = fopen (BINDATA, "rb");
        *numValues = fread (*data, dblsize, MAX_SIZE, readfile);
        printf ("");
        *data  = (double *) realloc (*data, *numValues);
    /* function sortArray sorts double *data in descending order*/
    void sortArray (double **data, int *numValues) {
        int ndx, ndx2, max;
        double hold, sum = 0;
        for (ndx = 0; ndx <= *numValues - 2; ndx++) {
            max = ndx;
            for (ndx2 = ndx + 1; ndx2 <= *numValues - 1; ndx2++)
                if ((*data)[ndx2] > (*data)[max])
                    max = ndx2;
            hold = (*data)[max];
            (*data)[max] = (*data)[ndx];
            (*data)[ndx] = hold;
    /*prints array in descending order and prints out highest, lowest, and average value
    The sum of the data set is calculated in during the sort, and used to caluclate the average
    void printArray (double **data, double *average, int *numValues) {
        int ndx, rowCount = 0;
        double sum = 0;
        printf ("Christian Boman\n");
        printf ("Number of values is %d.\n", *numValues);
        printf ("\nSorted array\n\n");
        for (ndx = 0; ndx < *numValues; ndx++){
            printf("[%d]\t%6.2f\t", ndx, *(*data + ndx));
            if (++rowCount == 5){
                printf ("\n");
                rowCount = 0;
            sum += (*data)[ndx]; 
        *average = sum / *numValues;
        printf ("\nHigh is %13.2f\n", (*data)[0]);
        printf ("Low is %14.2f\n", (*data)[*numValues - 1]);
        printf ("Average is %10.2f\n", *average);
    //Displays how many values are above, below, and equal to the average
    void hiLoEqual (double **data, double *average, int *numValues) {
        int ndx, above = 0, below = 0, equal = 0;
        for (ndx = 0; ndx < *numValues; ndx++) {
            if ((*data)[ndx] > *average + EPSILON)
            else if ((*data)[ndx] < *average - EPSILON)
        printf ("\n= average: %6d\n", equal);
        printf ("\n> average: %6d\n", above);
        printf ("\n< average: %6d\n", below);
    /* Finds the standard deviation of the data set.*/
    void stdDev (double **data, double *average, int *numValues) {
        int ndx, sizeData = *numValues; 
        double sumSqrs = 0, variance, stdev;
        double mean = *average;
        for (ndx = 0; ndx < sizeData; ndx++) 
            sumSqrs += (*data)[ndx] * (*data)[ndx];
        variance = (sumSqrs / sizeData - 1) - (mean * mean);
        stdev = variance * variance;
        printf ("\nStandard Deviation: %.2f\n", stdev);
    //Calculates and displays the median of the data set
    void getMedian (double **data, int *numValues) {
        double median;
        if (*numValues % 2 == 0)
            median = (*data)[*numValues /2 - 1];
            median = (*data)[*numValues / 2];
        printf ("\nMedian :\t%.2f\n", median);
    /* Finds and prints the mode of the data set. This algorithm
    has been optimized for a sorted list*/
    void getMode (double **arr, int *numValues) {
        double mode;
        int numData = *numValues;
        int count = 1, modeCount = 0, ndx; 
        for (ndx = 1; ndx < numData; ndx++) {
            if ((*arr)[ndx] == (*arr)[ndx - 1])
            else {
                if (count > modeCount){
                    mode = (*arr)[ndx - 1];
                    modeCount = count;
                count = 1;
        printf ("\nThe number %.2f occurs %d time(s)\n", mode, modeCount);
    Last edited by lexitron; 11-10-2011 at 01:37 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    You are reallocing incorrectly.
        *data  = (double *) realloc (*data, *numValues);
    The value of the second argument is incorrect. To work out how to correct it, look at your malloc() call. Fix that, and you should find the printf() call is not needed.

    Since that realloc() call is incorrect, subsequent functions merrily tromp random areas of memory because they assume the dynamically allocated array is longer than it really is. The printf() call happens - for your compiler and system - to cause memory used by your program to be laid out differently, so the areas of memory subsequently being tromped result in no observable effect. That is pure luck.

    Incidentally, all functions other than getArray() can get away safely with an argument list (double *data, int numValues).

    It would also help if the last line of main() before returning was free(data);
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Ha! I must multiply numValues by dblsize. So now I can just use (double *data, int numValues)? Thank you so much for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and Dynamic memory allocation help needed
    By dave_the_bear10 in forum C Programming
    Replies: 3
    Last Post: 11-09-2011, 04:23 PM
  2. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  3. pointers and dynamic memory
    By tkansara in forum C Programming
    Replies: 1
    Last Post: 06-16-2009, 09:22 AM
  4. Link List,Structures,Dynamic Memory Allocation,Pointers
    By Tanuj_Tanmay in forum C Programming
    Replies: 2
    Last Post: 04-24-2009, 08:55 PM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM