unexplainable segfault

  • Thread starter ZillionDollarSadist
  • Start date
Z

ZillionDollarSadist

Hello,
I have an initialization function of a class going like this:

==========================
void innode::initAllOutput(set* train, set* test)
{


const int trainSize=train->getMolecules();
nodeValueTrainZ=new double[trainSize];

output=new double*[trainSize];
residual=new double [trainSize];
tmpRec=new double*[trainSize];



for (int i=0;i<trainSize;i++) {

const int localSize=train->getCurrentSize(i);
output=new double[localSize];

tmpRec=new double[localSize]; **** SEGFAULTS HERE

}

const int testSize=test->getMolecules();
nodeValueTestZ=new double[testSize];
toutput=new double*[testSize];

for (int i=0;i<testSize;i++) {

const int localSize=test->getCurrentSize(i);
toutput=new double[localSize];

}

trainOutvals=new double[trainSize];
testOutvals=new double[testSize];

}
=====================

What happens is this: it works of 3 or 4 times in a row, then it
segfaults at ***. The strange thing is that variable localSize should
be something between 12 and 25 (it means the number of nodes in a
graph), and at the segfault time it becomes something like 1061662914,
so I guess such an array breaks the stack.
What to do now? I really can't understand why this happens...
Thanks!
ZDS.
 
V

Victor Bazarov

ZillionDollarSadist said:
Hello,
I have an initialization function of a class going like this:

==========================
void innode::initAllOutput(set* train, set* test)
{


const int trainSize=train->getMolecules();
nodeValueTrainZ=new double[trainSize];

output=new double*[trainSize];
residual=new double [trainSize];
tmpRec=new double*[trainSize];



for (int i=0;i<trainSize;i++) {

const int localSize=train->getCurrentSize(i);
output=new double[localSize];

tmpRec=new double[localSize]; **** SEGFAULTS HERE


I don't see any particular reason for a segfault except that 'tmpRec'
has not been allocated properly, or the heap (freestore) has been corrupted.

Are you deallocating the memory when you don't need it any longer? Do
you do it correctly (using 'delete[]' and not 'delete')? Make sure
you're not writing outside the array bounds.
}

const int testSize=test->getMolecules();
nodeValueTestZ=new double[testSize];
toutput=new double*[testSize];

for (int i=0;i<testSize;i++) {

const int localSize=test->getCurrentSize(i);
toutput=new double[localSize];

}

trainOutvals=new double[trainSize];
testOutvals=new double[testSize];

}
=====================

What happens is this: it works of 3 or 4 times in a row, then it
segfaults at ***. The strange thing is that variable localSize should
be something between 12 and 25 (it means the number of nodes in a
graph), and at the segfault time it becomes something like 1061662914,
so I guess such an array breaks the stack.


"Breaks the stack"? You most likely stomp over your freestore
somewhere, and that's what causes an allocation to fail.
What to do now? I really can't understand why this happens...

You need to verify that you aren't going beyond any of your array
bounds. It's not as easy to do as it is to suggest, I realise that.
Why do you use dynamic arrays in the first place? Why not
'std::vector<double>'?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top