Question 7.16

I'm allocating a large array for some numeric work, using the line

	double *array = malloc(256 * 256 * sizeof(double));
malloc isn't returning null, but the program is acting strangely, as if it's overwriting memory, or malloc isn't allocating as much as I asked for, or something.


Notice that 256 x 256 is 65,536, which will not fit in a 16-bit int, even before you multiply it by sizeof(double). If you need to allocate this much memory, you'll have to be careful. If size_t (the type accepted by malloc) is a 32-bit type on your machine, but int is 16 bits, you might be able to get away with writing 256 * (256 * sizeof(double)) (see question 3.14). Otherwise, you'll have to break your data structure up into smaller chunks, or use a 32-bit machine, or use some nonstandard memory allocation routines. See also question 19.23.


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback