LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   odd behaviour of array elements in c++ (http://www.linuxquestions.org/questions/programming-9/odd-behaviour-of-array-elements-in-c-301425/)

markhod 03-14-2005 09:03 AM

odd behaviour of array elements in c++
 
Hi,

I am using someones class and they define a data member:

double** m_patl;

and in the constructor they do:

m_patl = new double*[376000];

for (int i = 0; i < 376000; i++){
m_patl[i] = new double [5];
}


I had some problem with the code getting stuck in this class and eventually found some elements of this array are nans. And eventually I tracked down a part of the code where

m_patl[0][2]==170

then the code does: m_patl[7][2]=m_patl[0][2]

and a cout shows that m_patl[7][2] is nan instead of 170. (on the next line - no code is inbetween) Any ideas on how to debug this from here?

btw the original author is not contactable so I cant just ask them why their code doesnt work...

Cheers for any pointers (no pun intended),

Mark

Hivemind 03-14-2005 09:15 AM

The above code allocates a two-dimensional array, a matrix if you will, of doubles. Like a vector<vector<double> >. And it's very big. Since it doesn't set the elements in the matrix to any values it's no wonder that some elements are NANs. I'm not sure what the problem is though because your post isn't very clear, but giving each element a value, say 0.0, is probably a good idea.

markhod 03-14-2005 09:16 AM

Yeah, I already tried that and the problem persists even if I initialise all the values...

Hivemind 03-14-2005 09:20 AM

Try to make a small, self-contained example exhibiting the problem, using no platform specific libraries if possible and I will have a look at it. But you will probably find the problem when doing this. Or change the code to use vectors instead.

markhod 03-14-2005 10:58 AM

I tried a small self-contained example. Unfortunately it works perfectrly well! I tried building it up to be more and more complex but the problem is not manifesting itself. So I am at a loss as to the problem at the moment. I guess I'll try vectors in the actual code and see if that makes a difference..

Thanks for triyng to help,

Mark


All times are GMT -5. The time now is 04:56 PM.