Board index » delphi » dynamic memory allocation for a two-dimensional array of int

dynamic memory allocation for a two-dimensional array of int

I have been using Borland TURBO C++ for a long, long time (286s, remember).
I liked it. Even when I got a 386, I continued to use it (with a boot up
that didn't load EMM386 with which the IDE was unhappy). I flirted briefly
with MS Visual C++, but when I found that a command line C++ compiler was
available free, I got it along with the on-line (=on disk) doumentation. I
like it, too... fast, easy to use.

I have come to this new group with a little problem...
I am wanting to read an (initially) unknown number of rows of 8 integers
from a text file into a 2d array. The file is read through on a first pass
to determine the number of rows. I then attempt to allocate memory for the
array now that I know how many rows there are.

The article in the "C++Builder Language Guide" ( > Language Structure >
Arrays > Introduction to arrays in C) gives an example of how to do exactly
that. I saved the code as "dmamdo.c".
First, a closing brace was missing. ( In an example? Well, OK, easy to spot
and fixed.)
Second, when this is compiled with the command "bcc32 dmamdo.c" I got these
compile errors...
(Compiling as a .cpp does not give the Type mismatch error. ... I usually
compile as a C++ but I do not use classes.)

Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
dmamdo.c:
Error E2060 dmamdo.c 20: Illegal use of floating point in function main
Error E2062 dmamdo.c 23: Invalid indirection in function main
Error E2062 dmamdo.c 28: Invalid indirection in function main
Warning W8012 dmamdo.c 37: Comparing signed and unsigned values in function
de_allocate
Error E2342 dmamdo.c 38: Type mismatch in parameter '__block' (wanted 'void
*', got 'long double') in function de_allocate
*** 4 errors in Compile ***

I have annotated the example code with the compile errors below...

/* DYNAMIC MEMORY ALLOCATION FOR A MULTIDIMENSIONAL OBJECT. */
#include <stdio.h>
#include <stdlib.h>

typedef long double TYPE;
typedef TYPE *OBJECT;
unsigned int rows = 3, columns = 5;

void de_allocate(OBJECT);

int main(void) {
  OBJECT matrix;
  unsigned int i, j;

  /* STEP 1: SET UP THE ROWS. */
  matrix = (OBJECT) calloc( rows, sizeof(TYPE *));

  /* STEP 2: SET UP THE COLUMNS. */
  for (i = 0; i < rows; ++i)
    matrix[i] = (TYPE *) calloc( columns, sizeof(TYPE));  /*  # 20 Illegal
use of floating point */
    for (i = 0; i < rows; i++)
      for (j = 0; j < columns; j++)
        matrix[i][j] = i + j;    /* INITIALIZE #23 Invalid indirection */

  for (i = 0; i < rows; ++i) {
    printf("\n\n");
    for (j = 0; j < columns; ++j)
      printf("%5.2Lf", matrix[i][j]);  /* #28 Invalid indirection*/
    }  /* this closing brace was missing in the original */

  de_allocate(matrix);
  return 0;
  }

void de_allocate(OBJECT x) {
  int i;
  for (i = 0; i < rows; i++)    /* STEP 1: DELETE THE COLUMNS  #37 Comparing
signed and unsigned */
    free(x[i]);  /* #38 Type mismatch */
  free(x);    /* STEP 2: DELETE THE ROWS. */
  }

So, does any one have any comments about compile errors in illustrative
examples?
and, how do you do this successfully?

Cheers
Ross M

Good. Quick. Cheap.
Pick any two.

 

Re:dynamic memory allocation for a two-dimensional array of int


Ross,

Welcome back but this newsgroup (.BORLANDCPP) is for the pre-Builder
series of compilers. The free download compiler is actually the
compiler from Builder. You will reach a more appropriate and more
focused audience if you direct your questions to the appropriate
CPPBUILDER newsgroup section. I suspect that either
.CPPBUILDER.COMMANDLINETOOLS or .CPPBUILDER.LANGUAGE would be your
best bet.

Andrue Cope
[Bicester, UK]

Re:dynamic memory allocation for a two-dimensional array of int


Andrue,
Thanks, and sorry.
Ross.

Re:dynamic memory allocation for a two-dimensional array of int


Quote
On Wed, 7 Feb 2001 22:49:03 +1300, "Ross Marsden" <rmars...@xtra.co.nz> wrote:
>Error E2060 dmamdo.c 20: Illegal use of floating point in function main
>Error E2062 dmamdo.c 23: Invalid indirection in function main
>Error E2062 dmamdo.c 28: Invalid indirection in function main

Try changing this:
Quote
>typedef TYPE *OBJECT;

to this:
typedef TYPE **OBJECT;

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Other Threads