www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/23/00:57:45

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: compile error: char *frame_ptr = new char[20][20] ('X')
Date: Sat, 22 Feb 1997 20:50:53 -0800
Organization: Alcyone Systems
Lines: 42
Message-ID: <330FCCAD.4C5264FF@alcyone.com>
References: <3316832f DOT 19841747 AT news DOT erols DOT com>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hades wrote:

> whats the difference between delete blah and delete blah[]
> ...one book i have says to deallocate an array with delete blah
> ...another book mentions delete blah[]
> 
> any help or references to places with help on this subject would
> be appreciated

You're trying to create a double-dimensioned array in one shot.  You can't
do that.  You're also trying to initialize an array of objects with a
non-default constructor.  You also can't do that.

If you want to allocate a two-dimensional array, you need to do it in two
passes, just like in C:

    char **pp = new char *[20];

    for (int i = 0; i < 20; i++)
        pp[i] = new char[20];

If you want to initialize it, you have to do it after the objects have been
allocated:

    for (int i = 0; i < 20; i++)
    for (int j = 0; j < 20; j++)
        pp[i][j] = 'X';

To delete them, you should use

    for (int i = 0; i < 20; i++)
        delete[] pp[i];

    delete[] pp;

-- 
       Erik Max Francis, &tSftDotIotE / email:  max AT alcyone DOT com
                     Alcyone Systems /    web:  http://www.alcyone.com/max/
San Jose, California, United States /    icbm:  37 20 07 N  121 53 38 W
                                   \
              "I am become death, / destroyer of worlds."
                                 / J. Robert Oppenheimer (quoting legend)

- Raw text -


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