add/assign problem, part 2.

DCH david@carter-hitchin.clara.co.uk
Sun Dec 31 17:56:00 GMT 2006


Hi David,

I think you've got a coding error, not a compiler error.  Make sure 
you're not using any optimizations (i.e. no -01,2 or 3 switches) as 
these switches can often "magnify" errors such as the one you're 
getting.  Obviously it's not a solution to just leave optimization 
off, but it might help you track down where the problem is.

I haven't actually compiled up your program, but immediately alarm 
bells are ringing when I see things like

a = *(vec + i)

Are you 100% sure this isn't going to wander into unallocated 
memory?  Why not use std::vector instead of arrays?  If you did then 
you could call vector.size() and be safe.  I think you're getting an 
overrun somewhere because of your use of arrays - just check each 
block of code for that.  What is "a" set to just before ret gets set to zero?

Never put "using namespace std" in header files - it's very bad 
practice.  I can't find any use of "const" in your code - this 
prevents many of the kind of errors you're seeing - apply it to all 
variables and functions as appropriate.

You're not using Numerical Recipies for ideas are you?  Although this 
book is a classic it is full of horrid programming.  Take a look at 
the C++ book recommendations on www.accu.org for ideas for good books 
(unfortunately in the field of numerical computing, there isn't much 
to rival Press et. al).

HTH,
David.

At 17:25 31/12/2006, you wrote:

>Upon receiving some advice, I have attached here the entire piece of code I'm
>trying to run.  It's a Newton solver; LSMbdnew is the generic solver, and
>bulkfunc contains the function (f) and Jacobian (jac).  The whole 
>thing may look
>a little ugly to the experienced programmers on this list -- sorry.
>
>Anyway, this will compile and run, but it won't do what it's supposed to.  I
>found out with gdb that the reason for this is that the function
>LSMbdnew::norm(double *vec1, long num) is not executing the 
>add/assign properly.
>  For some reason, it sets the collecting variable _ret_ to zero 
> after one or two
>iterations.  Why?  I am stumped.
>
>Thanks,
>-David
>
>
>
>
>




More information about the Gcc-help mailing list