473,395 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Segmentation faults on "new"

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.

Jan 17 '07 #1
6 1705
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
Jan 17 '07 #2

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.

Jan 17 '07 #3
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.

Jan 17 '07 #4
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.
Jan 17 '07 #5
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.
Jan 17 '07 #6
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.
Jan 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
15
by: b83503104 | last post by:
Hi, class MyClass{ int array_size; HisClass **hisObject; }; I want hisObject to point to an array of HisClass objects. The size of the array is given by array_size.
6
by: peter.xiau | last post by:
I found that std::vector<int> v(10) ; will automatically initiallize every elem to 0 in the vector, I check the source code (VS.NET2003), and I found a line of code like this *T = new T() ; ...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
1
by: parvtb | last post by:
I know STL vector works. But in case STL is not available, what can one do to allocate large memory size in c++ through operator "new"? For instance, I am writing a sort algorithm, and here's...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.