Segmentation faults on "new"

  • Thread starter ZillionDollarSadist
  • Start date
Z

ZillionDollarSadist

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.
 
M

Michael DOUBEZ

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
 
Z

ZillionDollarSadist

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.
 
G

Grizlyk

ZillionDollarSadist said:
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.
 
R

Ron Natalie

ZillionDollarSadist said:
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.
 
L

Larry Smith

ZillionDollarSadist said:
Michael DOUBEZ ha scritto:



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


The classic gcc, running on OpenSuse.


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

Jacek Dziedzic

ZillionDollarSadist said:
Michael DOUBEZ ha scritto:



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


The classic gcc, running on OpenSuse.


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top