Array bounds checking with C????

Paul SPEE spee at qmfl.jrdc.go.jp
Mon Aug 27 14:18:59 AEST 1990


In article <7611 at ucdavis.ucdavis.edu> kuan at iris.ucdavis.edu (Frank [Who me?] Kuan) writes:
>	Why is it that most C compilers don't seem to support this
>	nifty little feature?

To be able to check the array boundaries, the C compiler must now the
array size. However, in most important cases the C compiler does not
have this information. This can be either be the case when an array
is passed as a function parameter or is allocated as a dynamic array.
It would have been convenient if ANSI would have allowed 'pointers
to variable size arrays'. For example,

int	array[10];

int *
f(n, a)
int	n;
int	(*a)[n];
{
	register i;
	int	(*b)[n] = (void *) malloc(n * sizeof(int));

	for (i = 0; i < n; i++)
		(*b)[i] = (*a)[i];

	return (int *) b;
}

main()
{
	f(sizeof(array), array);
}

(Note: gcc allows this.)

The same problem is true not only for runtime checking but also for
parallizing scientific code. See

%A Randy Allen
%A Steve Johnson
%T Compiling C for Vectorization, Parallelization, and Inline Expandsion
%J Proceedings of the SIGPLAN '88 Conference on Programming Language Design and Implementation
%D June 22-24, 1988
%C Atlanta, Georgia
%P 241-249
%L ALLEN88

Paul Spee



More information about the Comp.lang.c mailing list