Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calloc --- help!!!!! 1

Status
Not open for further replies.

i42

Programmer
May 5, 2004
1
US

i use C to do some numeric, so very large data
storage is required, with multiple (4-5) dimensional array.
i allocate them dynamically.

i got a problem of 2 pointers pointing to the same
memory address.
1. does it has something to do with big memory required.
2. ???

please comment.
 
Without seeing the code, I'd guess you made a mess of allocating the memory in the first place.
Most people seem to either forget to allocate something completely, or forget to scale the size of what they're allocating when calling malloc.

This is a 2D array - add more stars and more loops to create higher dimensions. Each extra level you add to this needs an extra loop to deal with it.
Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
    double **array;
    int row;
    int nrows = 10, ncols = 20;

    /* create array[nrows][ncols] */
    array = malloc( nrows * sizeof *array );
    for ( row = 0 ; row < nrows ; row++ ) {
        array[row] = malloc( ncols * sizeof *array[row] );
    }

    /* use it as you would an array, with array[r][c] notation */

    /* free the array */
    for ( row = 0 ; row < nrows ; row++ ) {
        free(array[row]);
    }
    free( array );
    return 0;
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top