As usual while waiting for the next release - don't forget to check the nightly builds in the forum.
I have a base class, which, for simplicity's sake, we'll just call Base. Not surprisingly, Derived is derived from Base. Base has a bunch of member variables and Derived adds some more. So far so good, right?Now at some point my program just segfaults. At first I was completely puzzled as to what caused the crash. For 2 days I kept stumbling completely in the dark. Then I noticed that the crash only happened when I had previously made an assignment to Base's member m_pExceptionResult (which is a boost::shared_ptr). I started looking for possible out-of-bound array accesses and other such things that tend to corrupt a program's memory but couldn't find any place I did anything like that. So I took a look at the memory addresses of the members of Base and Derived respectively, and this is what I found out:- m_pExceptionResult (boost::shared_ptr), the last member of Base, starts at address 0x1857268 ... sizeof( pExceptionResult ) gives 8 bytes- m_value (an std::basic_string< wchar_t >), the first member of Derived, starts at address 0x1857264 ... sizeof( m_value ) gives 32 bytesAs you can see from that, the memory occupied by m_pExceptionResult partially overlaps with that of m_value!! So my question to that amounts to a simple WTF???P.S. I'm using Visual C++ 2005.
It isn't nice to laugh at someone else's misfortune, but I can't help it... still rolling on the floor. See, stuff like that is why I'd never use one of Microsoft's compilers.
Add(T object){ if(m_Count == 0) { m_Data = new T[1]; m_Data[0] = object; m_Count = 1; } else { T tempData = new T[m_Count]; for(int i=0; i<m_Count; ++i) { tempData[i] = m_Data[i]; } delete[] m_Data; m_Data = new T[++m_Count]; for(int i=0; i<m_Count; ++i) { m_Data[i] = tempData[i]; } m_Data[m_Count-1] = object; delete[] tempData; }}
...the error wasn't on my code...
m_Data = new T[++m_Count]; for(int i=0; i<m_Count; ++i) { m_Data[i] = tempData[i]; }
m_Data = new T[++m_Count]; for(int i=0; i< (m_Count-1); ++i) //dont go out of bounds { m_Data[i] = tempData[i]; }
T tempData = new T[m_Count+1];
Im truely just a hobbyist noob but i think it is in your code. Its also really early for me and I might be misreading a part.