Segmentation faults on "new"

Discussion in 'C++' started by ZillionDollarSadist, Jan 17, 2007.

  1. Hello,
    I declared a class with some array of doubles as attributes. I have a
    function called init to initialize them through parameters passed from
    the outside, and here it is:

    void weights::init (int labels, int nnodes, int maxpool, int wcon, int
    npatterns, int npointers) {

    patterns_out=new double[npatterns];

    npats=npatterns;

    nodes=nnodes;

    LENGHT=labels+(npointers*(nodes-1));
    NPOINTERS=npointers;

    labelWeights=new double[LENGHT];
    oldLabelWeights=new double[LENGHT];

    ===================================== problems follow!
    recWeights=new double[npointers]; (1)
    oldRecWeights=new double[npointers];

    update_C=new double[LENGHT];
    oldUpdate_C=new double[LENGHT]; (2)

    rccDerivatives=new double[npointers];
    oldRccDerivatives=new double[npointers];

    deltaRccDerivatives=new double[npointers];
    oldDeltaRccDerivatives=new double[npointers];

    residual=new double[npatterns];

    tmpRcc=new double[npatterns];

    }

    Pretty average, it seems. Well, right after the line, there's always
    one of the following statements rising a segmentation fault, generally
    1 or 2.
    I don't understand why, these seem to be very basic operations.
    Thanks in advance.
    ZDS.
    ZillionDollarSadist, Jan 17, 2007
    #1
    1. Advertising

  2. ZillionDollarSadist a écrit :
    > Hello,
    > I declared a class with some array of doubles as attributes. I have a
    > function called init to initialize them through parameters passed from
    > the outside, and here it is:
    >
    > void weights::init (int labels, int nnodes, int maxpool, int wcon, int
    > npatterns, int npointers) {...
    >
    > LENGHT=labels+(npointers*(nodes-1));
    > NPOINTERS=npointers;
    >
    > labelWeights=new double[LENGHT];
    > oldLabelWeights=new double[LENGHT];
    >
    > ===================================== problems follow!
    > recWeights=new double[npointers]; (1)
    > oldRecWeights=new double[npointers];
    >
    > update_C=new double[LENGHT];
    > oldUpdate_C=new double[LENGHT]; (2)
    >
    >
    >
    > Pretty average, it seems. Well, right after the line, there's always
    > one of the following statements rising a segmentation fault, generally
    > 1 or 2.



    What are the values of npointers and LENGHT ?
    Perhaps should you at least assert that they are >=0 otherwise you
    should get a std::bad_alloc exception - try new (nothrow). What is your
    compiler ?

    Have you overloaded operator new ?

    Michael
    Michael DOUBEZ, Jan 17, 2007
    #2
    1. Advertising

  3. Michael DOUBEZ ha scritto:


    >
    > What are the values of npointers and LENGHT ?


    In the current tryout I'm doing, LENGHT =9 and npointers=1.

    > Perhaps should you at least assert that they are >=0 otherwise you
    > should get a std::bad_alloc exception - try new (nothrow). What is your
    > compiler ?


    The classic gcc, running on OpenSuse.

    > Have you overloaded operator new ?


    No, it's the standard one.
    Weird...
    ZDS.
    ZillionDollarSadist, Jan 17, 2007
    #3
  4. ZillionDollarSadist

    Grizlyk Guest

    ZillionDollarSadist wrote:

    > Hello,
    > I declared a class with some array of doubles as attributes. I have a
    > function called init to initialize them through parameters passed from
    > the outside, and here it is:


    What is "patterns_out", "npats" and other. Print the parts of class
    declaration, print in which place of code object of "weights" created
    and "init" called.
    Grizlyk, Jan 17, 2007
    #4
  5. ZillionDollarSadist

    Ron Natalie Guest

    ZillionDollarSadist wrote:
    > Hello,
    > I declared a class with some array of doubles as attributes. I have a
    > function called init to initialize them through parameters passed from
    > the outside, and here it is:
    >
    > void weights::init (int labels, int nnodes, int maxpool, int wcon, int
    > npatterns, int npointers) {


    All this silly new'ing makes my head swim.
    Have you considered vector? That would take care
    of most of the allocation/deallocation headaches?

    Chances are one of the following:

    npointers or LENGTH is either very large or negative.
    You omitted some lines that write off the end of
    the array.
    Ron Natalie, Jan 17, 2007
    #5
  6. ZillionDollarSadist

    Larry Smith Guest

    ZillionDollarSadist wrote:
    > Michael DOUBEZ ha scritto:
    >
    >
    >> What are the values of npointers and LENGHT ?

    >
    > In the current tryout I'm doing, LENGHT =9 and npointers=1.
    >
    >> Perhaps should you at least assert that they are >=0 otherwise you
    >> should get a std::bad_alloc exception - try new (nothrow). What is your
    >> compiler ?

    >
    > The classic gcc, running on OpenSuse.



    I hope you meant 'g++', rather than 'gcc'.
    Always compile AND link C++ code using 'g++'.


    >
    >> Have you overloaded operator new ?

    >
    > No, it's the standard one.
    > Weird...
    > ZDS.
    >
    Larry Smith, Jan 17, 2007
    #6
  7. ZillionDollarSadist wrote:
    > Michael DOUBEZ ha scritto:
    >
    >
    >> What are the values of npointers and LENGHT ?

    >
    > In the current tryout I'm doing, LENGHT =9 and npointers=1.
    >
    >> Perhaps should you at least assert that they are >=0 otherwise you
    >> should get a std::bad_alloc exception - try new (nothrow). What is your
    >> compiler ?

    >
    > The classic gcc, running on OpenSuse.
    >
    >> Have you overloaded operator new ?

    >
    > No, it's the standard one.
    > Weird...
    > ZDS.


    I'd say you've trashed the heap in some code you don't show
    and then a subsequent new segfaults. Try passing your code
    through valgrind, maybe it will tell you what's wrong.

    HTH,
    - J.
    Jacek Dziedzic, Jan 18, 2007
    #7
    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. Mathias

    Segmentation faults using threads

    Mathias, Feb 13, 2007, in forum: Python
    Replies:
    8
    Views:
    308
    John Nagle
    Feb 14, 2007
  2. Stanley S
    Replies:
    16
    Views:
    2,502
    Keith Thompson
    Dec 22, 2005
  3. Digital Puer
    Replies:
    18
    Views:
    700
    Ron Natalie
    Dec 28, 2005
  4. George Sakkis

    Debugging segmentation faults

    George Sakkis, Mar 7, 2007, in forum: Python
    Replies:
    4
    Views:
    379
    John Nagle
    Mar 8, 2007
  5. Kaspar Schiess

    YAML custom load: Segmentation faults

    Kaspar Schiess, Jun 25, 2004, in forum: Ruby
    Replies:
    2
    Views:
    131
    Kaspar Schiess
    Jun 25, 2004
Loading...

Share This Page