Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unexplainable segfault

Reply
Thread Tools

unexplainable segfault

 
 
ZillionDollarSadist
Guest
Posts: n/a
 
      11-03-2009
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[i]=new double[localSize];

tmpRec[i]=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[i]=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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-03-2009
ZillionDollarSadist wrote:
> 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[i]=new double[localSize];
>
> tmpRec[i]=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[i]=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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
unexplainable segfaults Marc R. C++ 4 05-21-2007 04:40 PM
IE7 - annoying and unexplainable horzontal scroll Andrey Tarasevich HTML 15 04-27-2007 07:17 AM
unexplainable Problem on Spartan 3 thomas.neitzel@gmail.com VHDL 1 12-19-2006 10:20 AM
totally unexplainable tomcat mystery! ian ward Java 10 06-28-2005 07:56 AM



Advertisments