Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!usc!ucselx!crash!kevin@crash.cts.com From: kevin@crash.cts.com (Kevin Hill) Newsgroups: comp.lang.c Subject: Memory Allocation Problem Message-ID: <7156@crash.cts.com> Date: 23 Jan 91 09:07:18 GMT Sender: root@crash.cts.com Lines: 83 I am still having problems with the malloc use in allocating memory for a two dimensional array. I thank everyone that has been attempting to help me with this. As of now, these are the variables outside of the function to follow: int **track; int **map; char *bigmap; int cursor,x1 = 0,y1 = 0,gx,gy; WindowPtr thewind,thewind1; WindowRecord windowmemory,windowmemory1; BitMap themap1,store; char iconmap[128]; Rect oldbox; int **track; int **map; char *bigmap; Initialize() { int x,y; gx = gy = 0; store.rowBytes = 4; SetRect(&store.bounds,0,0,32,32); if ( (bigmap = (char *)( malloc( (unsigned)(100 * 100 * 128)) )) == NULL) ExitToShell(); track = (int **)malloc( (unsigned)(100 * sizeof(int *)) ); track[0] = (int *)malloc( (unsigned)( 100 * 100 * sizeof(int)) ); if (track[0] == NULL) ExitToShell(); map = (int **)malloc( (unsigned)(100 * sizeof(int *)) ); map[0] = (int *)malloc( (unsigned)(100 * 100 * sizeof(int)) ); if (map[0] == NULL) ExitToShell(); for (x = 1; x < 100; x++) { track[x] = track[x-1] + (100 * sizeof(int)); map[x] = map[x-1] + (100 * sizeof(int)); for (y = 0; y < 100; y++) { map[x][y] = -1; track[x][y] = -1; } } } } So that is the extent of the stuff that I have attempted. Now the problem is that when the computer allocates using malloc, it returns very low address' or very high address'. The low ones, which are the most common are standard, and they are 0x000012A, is that an address to a master pointer list, and I need to dereference that also? Also, when I complete the malloc to issue the 100 pointers for either of them, if I use the debugger to find the address of track[0], track[1], it is still low. Is malloc buggy, or am I. I am betting that I am the one that is buggy! Also, yes I did read the FAW sheet that Steve Summit sent me, (FAQ not FAW, sorry) and question #20 is the only one that applies to my question, and if you look at the code above, you will see that I did follow it, and I think that I understand it. For example, when track is allocated by the malloc statement, I am giving memory space for the pointers that will point to the integers that I am trying to store. Then after that, I put into track[0] the beginning of the space of the actual 100x100 space for the integers of the two dimensional array. The for loop below sets up the pointers to point to the correct places in memory, and initializes the values of the pointers. So, far, the machine crashes at the malloc points, sometimes. Or always, when it gets to the actual initialization of the array. Yes, I know that if I have the wrong address' I am overwriting areas that the system could be using, but malloc should be sending back to me the correct address' and I believe that I am using the right coding to create the two-dimensional arrays that I am using. Thanks.