[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
What's wrong with this dynamic matrix allocation
From: |
Paul E. Johnson |
Subject: |
What's wrong with this dynamic matrix allocation |
Date: |
Fri, 19 Mar 1999 16:08:27 -0600 |
Every now and then I try to extend my horizons.
All it gets me is aggravation. I found the C-FAQ for the usenet group
on C and some examples of how to allocate a dynamic matrix in which the
rows are of variable length.
In my example, there are NOFSPACES rows and the number of columns of
each is given by Dimensionality[s]. Whenever I try to write on one of
the elements in my matrix, such as minValue[0][0], I get a seg fault and
the bt points at the command that accesses it. HEre's my example
----------------interface ------------------------
@interface MultiDimSpace: SwarmObject {
float ** minValue;
float ** maxValue;
}
----------implementation-----------------------------
#import "MultiDimSpace.h"
#import "Point.h"
#import <misc.h>
@implementation MultiDimSpace
-createEnd
{
int s;
[super createEnd];
minValue = xmalloc(NOFSPACES * sizeof (float * ));
for(s = 0; s < NOFSPACES; s ++)
{
minValue[s] = xmalloc(Dimensionality[s] * sizeof(float));
}
maxValue = malloc(NOFSPACES * sizeof(float *));
for(s = 0; s < NOFSPACES; s ++)
maxValue[s] = xmalloc(Dimensionality[s] * sizeof(float));
}
-setMinSpace: (int) space Dim: (int) dim To: (float) value
{
minValue[space][dim]=value;
return self;
}
int NOFSPACES
and
int Dimensionality[NOFSPACES]
are Global variables defined elsewhere. I've put in "printf" statements
to make sure that the values of NOFSPACES and Dimensionality[s] do
exist when that createEnd method is run.
Here is an example of commands from ModelSwarm that crash and the bt
points at the setMinSpace message sent to world.
world = [MultiDimSpace createBegin: [self getZone]];
world = [world createEnd];
for(s=0;s<NOFSPACES;s++)
for(i=0; i < Dimensionality[s] ; i++)
{
[world setMinSpace: s Dim: i To: 0];
}
--
Paul E. Johnson email: address@hidden
Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn
University of Kansas Office: (785) 864-9086
Lawrence, Kansas 66045 FAX: (785) 864-5700
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
- What's wrong with this dynamic matrix allocation,
Paul E. Johnson <=
- Re: What's wrong with this dynamic matrix allocation, Marcus G. Daniels, 1999/03/19
- Re: What's wrong with this dynamic matrix allocation, Marcus G. Daniels, 1999/03/19
- Re: What's wrong with this dynamic matrix allocation, Paul E. Johnson, 1999/03/19
- Re: What's wrong with this dynamic matrix allocation, Marcus G. Daniels, 1999/03/19
- Interesting new GCC warning (was Re: What's wrong with this dynamic matrix allocation, Paul E. Johnson, 1999/03/19
- Re: Interesting new GCC warning, Ken Cline, 1999/03/19
- Re: Interesting new GCC warning (was Re: What's wrong with this dynamic matrix allocation, Marcus G. Daniels, 1999/03/19
- Re: Interesting new GCC warning (was Re: What's wrong with this dynamic matrix allocation, Theodore C. Belding, 1999/03/19