Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Function to allocate memory for a 2D array in C

  1. #1
    Join Date
    May 2007
    Location
    Crete, Greece
    Beans
    582

    Function to allocate memory for a 2D array in C

    Hi all,

    I am trying to make a function that allocates memory for a 2 dimensional integer array. Here is what I have written:

    Code:
    int**  allocate_matrix(int cols, int rows)
    {
    	int i;
    	int ** matrix;
    	
    	matrix = (* int) malloc (rows*sizeof(int *));
    	if(matrix == NULL){
    		free(matrix); 
    		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){
    			free(matrix[i]); 
    			printf("Memory allocation failed while allocating for matrix[x][].\n"); 
    			exit(-1);
    		}
    	}
    	
    	return matrix;
    }
    Could you please tell me the mistake above?
    The message I get when I compile is
    Code:
    functions.c: In function ‘allocate_matrix’:
    functions.c:24: error: expected expression before ‘int’
    functions.c:24: error: expected ‘;’ before ‘malloc’
    make: *** [functions.o] Error 1
    The 24th line is
    Code:
    matrix = (* int) malloc (rows*sizeof(int *));
    Thanks in advance..
    Last edited by geo909; October 15th, 2009 at 03:48 PM.

  2. #2
    Join Date
    May 2006
    Beans
    1,787

    Re: Function to allocate memory for a 2D array in C

    Quote Originally Posted by geo909 View Post
    Hi all,

    I am trying to make a function that allocates memory for a 2 dimensional integer array. Here is what I have written:

    Code:
    int**  allocate_matrix(int cols, int rows)
    {
    	int i;
    	int ** matrix;
    	
    	matrix = (* int) malloc (rows*sizeof(int *));
    	if(matrix == NULL){
    		free(matrix); 
    		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){
    			free(matrix[i]); 
    			printf("Memory allocation failed while allocating for matrix[x][].\n"); 
    			exit(-1);
    		}
    	}
    	
    	return matrix;
    }
    Could you please tell me the mistake above?
    The message I get when I compile is
    Code:
    functions.c: In function ‘allocate_matrix’:
    functions.c:24: error: expected expression before ‘int’
    functions.c:24: error: expected ‘;’ before ‘malloc’
    make: *** [functions.o] Error 1
    The 24th line is
    Code:
    matrix = (* int) malloc (rows*sizeof(int *));
    Thanks in advance..
    You want (int *), not (* int).

    Besides, you should not be freeing things when in fact nothing was allocated. "if (matrix==NULL) free(matrix)" is at best a no-op, and will probably crash instead (I haven't looked it up). But if you have succeeded in partly allocating the matrix, freeing the already allocated rows is a good idea.

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: Function to allocate memory for a 2D array in C

    EDIT: Looks like Arndt beat me to the answer.

    -----------------------------------------------

    See snip below for changes:
    Code:
    #include <stdlib.h>
    
    ...
            matrix = (int **) malloc (rows*sizeof(int *));
    ...
    	if(matrix == NULL){
    		/* free(matrix); */   /* Not Needed! */
                    ...
    Note... you do not need to cast the return value of malloc() to suit your object's type. Second, do you not have to free a NULL pointer when malloc() fails.
    Last edited by dwhitney67; October 15th, 2009 at 04:42 PM.

  4. #4
    Join Date
    May 2007
    Location
    Crete, Greece
    Beans
    582

    Re: Function to allocate memory for a 2D array in C

    Hello, thanks a lot for the reply.
    I replaced (* int) with (int **):

    Code:
    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;
    }
    Now I get:

    Code:
    jorge@flamingo:~/Documents/Academic/Courses/Coding Theory$ make
    gcc -c functions.c
    functions.c: In function ‘allocate_matrix’:
    functions.c:24: error: expected expression before ‘int’
    functions.c:24: error: expected ‘;’ before ‘malloc’
    make: *** [functions.o] Error 1
    This time line 24 is
    Code:
    matrix = (** int) malloc (rows*sizeof(int *));
    Can you see the mistake here, too?
    Oh well, I'm still confused with pointers I guess

  5. #5
    Join Date
    Nov 2005
    Location
    Almeria, Spain
    Beans
    276
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to allocate memory for a 2D array in C

    I'd advise this to avoid a cast warning:

    matrix = (int **) malloc (rows*sizeof(int *));

    Edit: note it's "(int **)" NOT "(** int)"
    Francisco Camenforte Torres
    http://fmct.blogspot.com

  6. #6
    Join Date
    May 2007
    Location
    Crete, Greece
    Beans
    582

    Re: Function to allocate memory for a 2D array in C

    Many thanks for the quick responses. I really appreciate it..

    note it's "(int **)" NOT "(** int)"
    Yes, it compiles now!

    Thanks again

  7. #7
    Join Date
    May 2006
    Beans
    1,787

    Re: Function to allocate memory for a 2D array in C

    Quote Originally Posted by geo909 View Post
    Many thanks for the quick responses. I really appreciate it..


    Yes, it compiles now!

    Thanks again
    Type expressions like these are just like pointer expressions, except that the variable denoting the pointer isn't there. So if you want to be able to say "int x = **p;" you declare p as "int **p;" and then its type is (int **).

    Chris Torek once wrote a program which could explain complex type expressions, maybe it still exists somewhere.

  8. #8
    Join Date
    May 2007
    Location
    Crete, Greece
    Beans
    582

    Re: Function to allocate memory for a 2D array in C

    Thanks for the info.. I'll check this out.

  9. #9
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Function to allocate memory for a 2D array in C

    The simplest way to work with 2D arrays, is with a 1D array and just pretend that it's a 2D array, with macros, function, whatever.

    That way you don't need an array filled with pointers.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  10. #10
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Function to allocate memory for a 2D array in C

    Quote Originally Posted by Can+~ View Post
    The simplest way to work with 2D arrays, is with a 1D array and just pretend that it's a 2D array, with macros, function, whatever.

    That way you don't need an array filled with pointers.
    A "flat" array? Why haven't I ever thought about that?? With a little of linear algebra that idea seems really easy to implement!

Page 1 of 2 12 LastLast

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
  •