unexplainable segfault

Discussion in 'C++' started by ZillionDollarSadist, Nov 3, 2009.

  1. 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.
     
    ZillionDollarSadist, Nov 3, 2009
    #1
    1. Advertising

  2. 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=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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 3, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian ward
    Replies:
    10
    Views:
    612
    Roma Efremov
    Jun 28, 2005
  2. Replies:
    1
    Views:
    724
    Benjamin Todd
    Dec 19, 2006
  3. Andrey Tarasevich
    Replies:
    15
    Views:
    920
    Ben C
    Apr 27, 2007
  4. Marc R.

    unexplainable segfaults

    Marc R., May 21, 2007, in forum: C++
    Replies:
    4
    Views:
    383
    Howard
    May 21, 2007
  5. Andrey Vul
    Replies:
    8
    Views:
    698
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page