www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/25/06:00:27

From: "Ren" <waxweazle69 AT yahoo DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Dynamically Allocated Arrays in C
Lines: 56
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
Message-ID: <iaxF6.15340$ff.107947@news-server.bigpond.net.au>
Date: Wed, 25 Apr 2001 09:55:26 GMT
NNTP-Posting-Host: 203.45.131.31
X-Complaints-To: news AT bigpond DOT net DOT au
X-Trace: news-server.bigpond.net.au 988192526 203.45.131.31 (Wed, 25 Apr 2001 19:55:26 EST)
NNTP-Posting-Date: Wed, 25 Apr 2001 19:55:26 EST
Organization: BigPond Internet Services (http://www.bigpond.net.au)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi there,

I have a slight problem with implementing a dynamically allocated 2D array.
I was originally using 10 declerations of arrays in the following form:
char claimsTagArray[40][9]; but it seemed a slight waste of memory and time.

So i wanted to implement it dynamically so i only need to allocate memory to
it when i need it.

The following code is what i tried to use so far:

/****** CODE STARTS HERE ******/
char **createArray(int firstDim, int secondDim)
{
 char **newArray;
 int i;

 newArray = (char **)malloc(sizeof(char *) * firstDim);
 if(newArray == NULL)
 {
  printf("Unable to allocate memory\n");
  exit(0);
 }

 for(i = 0; i < firstDim; i ++)
 {
  newArray[i] = (char *)malloc(sizeof(char) * secondDim);
  if(newArray[i] == NULL)
  {
   printf("Unable to allocate memory\n");
   exit(0);
  }
 }
 return newArray;
}

void freeArray(char **array, int firstDim, int secondDim)
{
 int i, j;

 /*for(i = 0; i < secondDim; i ++)
  free(array[i][j]);
 */

 /*free(array);*/
}
/***** CODE ENDS HERE *****/

But it keeps crashing, if there are any ideas please contact me as soon as
possible.

Thank you in advance
Ren



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019