Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - unexplainable segfault

 
Thread Tools Search this Thread
Old 11-03-2009, 03:53 PM   #1
Default unexplainable segfault


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.


ZillionDollarSadist
  Reply With Quote
Old 11-03-2009, 04:42 PM   #2
Victor Bazarov
 
Posts: n/a
Default Re: unexplainable segfault
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


Victor Bazarov
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46