+ Reply to Thread
Results 1 to 3 of 3

Thread: Reduce number of calls to malloc to allocate a 2D array

  1. #1
    admin's Avatar
    admin is offline Administrator
    Join Date
    Mar 2010
    Posts
    44

    Reduce number of calls to malloc to allocate a 2D array

    Reduce the no of calls to malloc, to allocate a 2-d array.

  2. #2
    jalajb2k7 Guest
    int** allocate_matrix(int cols, int rows)
    {
    int i;
    int ** matrix;

    matrix = ( int**) malloc (rows*sizeof(int *));
    if(matrix == NULL){
    printf("Memory allocation failed while allocating for matrix[].\n");
    exit(-1);
    }
    for(i = 0; i < rows; ++i){
    matrix[i] = (int *) malloc(cols * sizeof(int));
    if(matrix[i] == NULL){
    printf("Memory allocation failed while allocating for matrix[i][].\n");
    exit(-1);
    }
    }

    return matrix;
    }

  3. #3
    jalajb2k7 Guest
    int** allocate_matrix(int cols, int rows)
    {
    int i;
    int ** matrix;

    matrix = ( int**) malloc (rows*sizeof(int *));
    if(matrix == NULL){
    printf("Memory allocation failed while allocating for matrix[].\n");
    exit(-1);
    }
    for(i = 0; i < rows; ++i){
    matrix[i] = (int *) malloc(cols * sizeof(int));
    if(matrix[i] == NULL){
    printf("Memory allocation failed while allocating for matrix[i][].\n");
    exit(-1);
    }
    }

    return matrix;
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reduce number of comparisons
    By admin in forum Adobe
    Replies: 1
    Last Post: 1st July 2010, 00:28
  2. Allocate memory for 2D array
    By TopGun in forum Adobe
    Replies: 3
    Last Post: 3rd January 2009, 07:14
  3. Can you point out some differences between new & malloc?
    By TopGun in forum C++ Fundamentals
    Replies: 0
    Last Post: 5th June 2008, 15:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts