From: cziwkga AT ulcc DOT ac DOT uk (Kevin Ashley) Newsgroups: comp.os.msdos.djgpp Subject: Re: Dynamically allocated arrays Date: 4 Feb 1997 15:36:57 GMT Organization: University of London Computer Centre Lines: 56 Distribution: world Message-ID: <5d7l2p$qcs@calypso.ulcc.ac.uk> References: <32F51954 DOT 6B69 AT byu DOT edu> Reply-To: k DOT ashley AT ulcc DOT ac DOT uk NNTP-Posting-Host: silver-e.ulcc.ac.uk To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp In article <32F51954 DOT 6B69 AT byu DOT edu>, Christian Bird writes: |>Hello, |> Is there a limit to the size of an array that can be allocated? Right |>now I'm writing a maze generation program and I input the size of it |>from the user and dynamically allocate the array I need. Here is the |>code. |> |>struct cell |>{ |> int left, up, right, down, used; |>}; |> |>cell ** maze; |>int i; |> |>make_maze = (cell **) malloc(max_width*sizeof(cell *)); |>for (i = 0; i < max_width; i++) make_maze[i] = (cell *) |>malloc(max_height*sizeof(cell)); |> | [Christian then says that he has problems when max_width=198 and max_height approaches 100] You can have arrays far bigger than this with djgpp. The structure you are talking about would only need 400K with the values you describe. The problem may be to do with CWSDPMI and the maximum number of fragments it can keep track of when required to move the program break boundary (which internally is what malloc needs to do when it needs more heap.) If this is the case, it would be resolved by calling malloc() fewer times but asking for more each time. In your case, you can malloc() the whole thing at once, and if you still want maze to be an array of pointers to arrays of cells, you have: cell *celltemp; make_maze = (cell **) malloc(max_width*sizeof(cell *)); celltemp = (cell *) malloc(max_height*max_width*sizeof(struct cell)); for (i = 0; i < max_width; i++) { make_maze[i] = celltemp; celltemp += max_height; } That is assuming, of course, that your program dies on the malloc() call. You didn't actually say where the program died, and with what message. Since you aren't error trapping the malloc, it may be that your problem is elsewhere entirely. If malloc can't get memory, it just returns NULL. If you dereference that later, you have problems. ------------------------------------------------------------------------------ Kevin Ashley K DOT Ashley AT Ulcc DOT ac DOT uk Systems Development Group Manager http://www.ulcc.ac.uk/staff/Kevin+Ashley University of London Computer Centre. ...ukc!ncdlab!K.Ashley This is not a signature