FAQ - malloc array - clarify

James Darrell McCauley jdm5548 at diamond.tamu.edu
Sat Sep 8 02:57:29 AEST 1990


Could someone lend a hand? Topic is FAQ #19, allocating memory
for an array. The problem is that 'lint' doesn't like what I'm 
doing.
Here's some source:
#include<stdio.h>
#include<malloc.h>
main()
{
  int i, nrows=10, ncolumns=10;
  double **array;
 
  array = (double **)malloc(nrows * ncolumns * sizeof(double *));
  for(i = 0; i < nrows; i++)
    array[i] = (double *)malloc(ncolumns * sizeof(double));
}
/* 
Code from answer to #19 in FAQ list (dated Aug 3 by 'ls -l'):

int **array = (int **)malloc(nrows * ncolumns * sizeof(int *));
         for(i = 0; i < nrows; i++)
                 array[i] = (int *)malloc(ncolumns * sizeof(int));

*/
/* output from lint:
test.c(8): warning: possible pointer alignment problem
test.c(10): warning: possible pointer alignment problem
malloc, arg. 1 used inconsistently	llib-lc(383)  ::  test.c(8)
malloc, arg. 1 used inconsistently	llib-lc(383)  ::  test.c(10)
*/

Why am I getting these warnings?  I get them using lint that 
(I assume) is bundled with the OS.  SunOS Release 4.0.3c on a Sparc.
If I run them on my Sun 3/60 with SunOS Release 4.0.3, I don't
get the warnings. ('diff "4.0.3" "4.0.3c"' ?)  Do I have a broken
lint on the Sparc?

Should the argument to malloc be cast (unsigned) (and FAQ list changed)?
If I make this change, lint doesn't scream about arguments being used
incorrectly.

Darrell McCauley



More information about the Comp.lang.c mailing list