www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/03/21:35:22

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: 2d matrix class/c++ problems
Date: Thu, 31 Jul 1997 20:37:40 -0700
Organization: Alcyone Systems
Lines: 34
Message-ID: <33E15A04.7AE25020@alcyone.com>
References: <Pine DOT SGI DOT 3 DOT 91 DOT 970729164813 DOT 27212A-100000 AT neutrino DOT phys DOT laurentian DOT ca>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Timothy Robb wrote:

> I'm trying to get a 2d matrix class to work which seems to compile fine
> but crashes whenever I try to access data elements.

I took a quick scan, and immediately came across a fundamental problem (I
stopped looking there; there may be more).  The problem is in
Matrix::Matrix:

    data = new Complex* [rows];

    assert(data != NULL);

    for(int i=0; i<rows; i++);
    {
                data[rows] = new Complex[columns];
            assert(data[rows] != NULL);
    }

There are two serious problems here.  First, there's a semicolon after the
for statement, which means that you have a null for loop.

The second is that, even if the semicolon weren't there, you're accessing
the data array with data[rows], which is not only wrong (you meant
data[i]), but also will crash, because it exceeds the bounds of the array
(which has rows elements, from 0 .. rows - 1).

-- 
       Erik Max Francis, &tSftDotIotE / email / mailto: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
                                   \
   "Love is not love which alters / when it alternation finds."
                                 / William Shakespeare, _Sonnets_, 116

- Raw text -


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