Board index » cppbuilder » TList

TList


2005-03-30 08:36:09 AM
cppbuilder106
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
 
 

Re:TList

"Paulo Dias" < XXXX@XXXXX.COM >wrote in message
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
 

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.
 

{smallsort}