From: Jih-Shin Ho Subject: Re: Malloc To: john AT umdsun2 DOT umd DOT umich DOT edu (john hoeschele) Date: Sat, 10 Dec 94 18:44:42 WST Cc: djgpp AT sun DOT soe DOT clarkson DOT edu (djgpp) > > I am trying to use malloc to allocate a 2.7Mbyte chunk of memory. The > program compiles with no problems but when I try to execute, no memory is > allocated and it quits. Here is how the function is used: > > typedef struct sBuf { > UCHAR vmbuf[1728]; > }tBuf > > tBuf *Bptr[1600]; > > inside main: > > if(Bptr[0] = (tBuf *)malloc(1728*sizeof(tBuf))) > printf("There has been an error is allocating memory!!"); > exit(1); It should be 'if (!(Bptr[0] = (tBuf *)malloc(1728*sizeof(tBuf))))' You forget '!'. > > if it continues, I load Bptr[] with 1600 pointers to various locations in > allocated memory, but I never get there. I always get the printf statement. > > Am I using the malloc function wrong? I tried a test program not using a > structure but just putting in large numbers to equal out to 2.7Meg, and > it returned an address ,so there is enough memory available. > > I thought about just using a two dimensional array (array[1728][1600]) Your array is [1728][1728]. I suggest that you use another definition: UCHAR (*Bptr)[1600]; Bptr = (UCHAR (*)[1600])malloc(1728 * 1600 * sizeof(UCHAR)); You will get 2D array ([1728][1600]). -- Jih-Shin Ho