Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Segmentation faults on "new"

Reply
Thread Tools

Segmentation faults on "new"

 
 
ZillionDollarSadist
Guest
Posts: n/a
 
      01-17-2007
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.

 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-17-2007
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
 
Reply With Quote
 
 
 
 
ZillionDollarSadist
Guest
Posts: n/a
 
      01-17-2007

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.

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-17-2007
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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-17-2007
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.
 
Reply With Quote
 
Larry Smith
Guest
Posts: n/a
 
      01-17-2007
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.
>

 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      01-18-2007
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.
 
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
Debugging segmentation faults George Sakkis Python 4 03-08-2007 04:45 AM
Segmentation faults using threads Mathias Python 8 02-14-2007 08:12 PM
using exceptions to catch segmentation faults? Digital Puer C++ 18 12-28-2005 01:31 PM
strcat() and segmentation faults. But malloc() already done. Stanley S C Programming 16 12-22-2005 08:43 PM
YAML custom load: Segmentation faults Kaspar Schiess Ruby 2 06-25-2004 01:01 PM



Advertisments