NULL pointer comparison fails
        Posted  
        
            by Ilya
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ilya
        
        
        
        Published on 2010-04-01T16:04:38Z
        Indexed on 
            2010/04/01
            16:13 UTC
        
        
        Read the original article
        Hit count: 228
        
Hello,
I'm initializing in a class a pointer to be NULL. Afterwards I check if it is NULL in the same class. But it's not always 0x0. Sometimes it's 0x8 or 0xfeffffff or 0x3f800000 or 0x80 or other strange stuff. In most case the pointer is 0x0 but sometimes it gets altered somehow.
I'm sure that I'm not changing it anywhere in my code. Is there a way it gets changed by "itself"?
Here's my code:
MeshObject::MeshObject()
{
    mesh.vertexColors = NULL;
}
MeshObject::MeshObject(const MeshObject &_copyFromMe)
{
    SimpleLog("vertexColors pointer: %p", _copyFromMe.mesh.vertexColors);
    if (_copyFromMe.mesh.vertexColors != NULL)
    {
        SimpleLog("vertexColors");
        this->mesh.vertexColors = new tColor4i[_copyFromMe.mesh.vertexCount];
        memcpy(this->mesh.vertexColors, _copyFromMe.mesh.vertexColors, _copyFromMe.mesh.vertexCount * sizeof(tColor4i) );
    }
}
My application crashes, because vertexColors wasn't initialized and is being copied. However it is NULL and shouldn't be copied.
Thanks.
© Stack Overflow or respective owner