FAQ - malloc array - clarify

Conor P. Cahill cpcahil at virtech.uucp
Sat Sep 8 12:20:34 AEST 1990


In article <8056 at helios.TAMU.EDU> jdm5548 at diamond.tamu.edu (James Darrell McCauley) writes:
>  int i, nrows=10, ncolumns=10;
>  double **array;
> 
>  array = (double **)malloc(nrows * ncolumns * sizeof(double *));
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
While this has nothing to do with your lint problems, I think this should be:

   array = (double **)malloc(nrows * sizeof(double *));

>  for(i = 0; i < nrows; i++)
>    array[i] = (double *)malloc(ncolumns * sizeof(double));
>}
>/* 
>/* output from lint:
>test.c(8): warning: possible pointer alignment problem
>test.c(10): warning: possible pointer alignment problem

This you will always get from lint.  The problem is that malloc is defined
as returning a pointer to char which has looser alignment requirements than
pointer to pointer.  Since malloc guarrantees that the result is suitably 
alligned for all data types, you can ignore this message.

>malloc, arg. 1 used inconsistently	llib-lc(383)  ::  test.c(8)
>malloc, arg. 1 used inconsistently	llib-lc(383)  ::  test.c(10)

This is caused by the argument to malloc being unsigned, not signed.  A
cast in your code will fix this.




-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.lang.c mailing list