| View previous topic :: View next topic |
| Author |
Message |
Paulo Dias Guest
|
Posted: Wed Mar 30, 2005 12:36 am Post subject: TList |
|
|
typedef struct mList
{
float motif[9];
}TmList;
typedef TmList* SmList;
TList *population = new TList();
SmList mStruct;
i make this declarations... then i use this code:
void __fastcall TBaseForm::GeneratePopulation(int nElements)
{
// delete elements in the list
for (int i=0; i<population->Count; i++)
{
mStruct=(SmList)population->Items[i];
delete mStruct;
}
// for each element of the population
for(int i=0; i<nElements; i++)
{
mStruct = new TmList;
<------------------------------------------------------------- After 2
iteration code explods in this line
// fill motif values
for(int j=0; j<10; j++)
mStruct->motif[j]= (float) rand()/RAND_MAX;
population->Add(mStruct);
}
}
please can someone help me...
Thanks in advance
Paulo Dias
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Mar 30, 2005 1:18 am Post subject: Re: TList |
|
|
"Paulo Dias" <dias.paulo (AT) gmail (DOT) com> wrote
| Quote: | // delete elements in the list
for (int i=0; i<population->Count; i++)
{
mStruct=(SmList)population->Items[i];
delete mStruct;
}
|
You are not clearing the TList. You are adding new items to the existing
list without removing the now-invalid items that you just deleted.
Gambit
|
|
| Back to top |
|
 |
Wiljo Guest
|
Posted: Wed Mar 30, 2005 11:31 am Post subject: Re: TList |
|
|
Paulo Dias wrote:
| Quote: | typedef struct mList
{
float motif[9];
}TmList;
...
------------------- After 2 iteration code explods in this line
// fill motif values
for(int j=0; j<10; j++)
mStruct->motif[j]= (float) rand()/RAND_MAX;
population->Add(mStruct);
}
}
please can someone help me...
Thanks in advance
Paulo Dias
|
Well, what I see here is that your motif float array contains 9 elements from 0
upto and including 8. You are iterating it with j from 0 upto and including 9
(j<10), thus you are accessing the motif array outside it's range.
Either enlarge the array to 10, because you are iterating 10 elements, or
decrease the limit of the loop.
float motif[ 9 ];
This line means that an array of 9 elements is reserved, but because arrays
start at index 0 the index of their last element is total number of elements
minus one. In this case the range is from 0 upto 8.
Wiljo.
|
|
| Back to top |
|
 |
|