struct losing it's value when in a global array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    struct losing it's value when in a global array

    I have a struct like so defined like so:
    Code:
    //vector3.h
    struct Vector3
    {
    	float x, y, z;
    	Vector3();
    	Vector3(float _x, float _y, float _z);
    	Vector3(const Vector3 &v);
    };
    
    //vector3.cpp
    #include "vector3.h"
    
    Vector3::Vector3()
    {
    	x = y = z = 0.0f;
    }
    
    Vector3::Vector3(float _x, float _y, float _z)
    {
    	x = _x;
    	y = _y;
    	z = _z;
    }
    
    Vector3::Vector3(const Vector3 &v)
    {
    	x = v.x;
    	y = v.y;
    	z = v.z;
    }
    But when I try to define a global array of the structs all the values are lost and instead it is treated as if I didn't give it any values.

    I define the array like this:
    Code:
    const Vector3 octagon[] = { Vector3(-0.5f, -1.5f, 0.0f),
    					  Vector3(0.5f, -1.5f, 0.0f),
    					  Vector3(-1.5f, -0.5f, 0.0f),
    					  Vector3(-1.5f, 0.5f, 0.0f),
    					  Vector3(-0.5f, 1.5f, 0.0f),
    					  Vector3(0.5f, 1.5f, 0.0f),
    					  Vector3(1.5f, 0.5f, 0.0f),
    					  Vector3(1.5f, -0.5f, 0.0f),
    					  Vector3(0.5f, -1.5f, 0.0f),
    					  Vector3(-0.5f, 0.5f, 0.0f),
    					  Vector3(-0.5f, -0.5f, 0.0f),
    					  Vector3(-1.5f, -0.5f, 0.0f) };
    However if I create a Vector3 outside of an array everything remains intact.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It looks like everything is there in my debugger.

    Comment

    • MrPickle
      New Member
      • Jul 2008
      • 100

      #3
      In my debugger the values are there until I call the constructor for another function but that function doesn't change the variable at all, it just reads from it. It couldn't change the values because they're defined as const, also if I output the values they're all 0, but I put that in the first for loop. Here's the function in which the values are set to 0.

      Code:
      Octagon::Octagon(Vector3& pos, Vector3& rot)
      {
      	for(int i = 0; i < 12; i++)
      	{
      		vertices.push_back(octagon[i]);
      	}
      
      	for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++)
      	{
      		indices.push_back(octagon_index[i]);
      	}
      
      	position = pos;
      	rotation = rot;
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I'm soory but this looks odd:

        Code:
        Octagon::Octagon(Vector3& pos, Vector3& rot) 
        { 
            for(int i = 0; i < 12; i++) 
            { 
                vertices.push_back(octagon[i]); 
            } 
          
            for(int i = 0; i < [B]sizeof(octagon_index)/sizeof(const int); [/B]i++) 
            { 
                indices.push_back(octagon_index[i]); 
            } 
          
            position = pos; 
            rotation = rot; 
        }
        octagon_index is a pointer (4 bytes) and a const_int is 4 bytes. So octagon_index)/sizeof(const int) is always 1.

        The loop goes one cycle.

        Maybe that's where your array has disappeared.

        Comment

        • MrPickle
          New Member
          • Jul 2008
          • 100

          #5
          That line works fine, it returns 30 like expected, octagon_index is a global array like octagon but I understand what you're saying.

          Here's the entire relevant part:
          Code:
          //main.cpp
          #include "octagon.h"
          
          Octagon oct(Vector3(0.0f, 0.0f, -5.0f));
          
          //octagon.h
          #include <vector>
          #include "vector.h"
          
          struct Octagon
          {
          	Octagon(Vector3& pos, Vector3& rot = Vector3());
          	void draw();
          
          	std::vector<Vector3> vertices;
          	std::vector<int> indices;
          
          	Vector3 position, rotation, colour;
          };
          
          //octagon.cpp
          #include "octagon.h"
          
          const Vector3 octagon[] = { Vector3(-0.5f, -1.5f, 0.0f),
          							Vector3(0.5f, -1.5f, 0.0f),
          							Vector3(-1.5f, -0.5f, 0.0f),
          							Vector3(-1.5f, 0.5f, 0.0f),
          							Vector3(-0.5f, 1.5f, 0.0f),
          							Vector3(0.5f, 1.5f, 0.0f),
          							Vector3(1.5f, 0.5f, 0.0f),
          							Vector3(1.5f, -0.5f, 0.0f),
          							Vector3(0.5f, -1.5f, 0.0f),
          							Vector3(-0.5f, 0.5f, 0.0f),
          							Vector3(-0.5f, -0.5f, 0.0f),
          							Vector3(-1.5f, -0.5f, 0.0f) };
          
          const int octagon_index[] = { 0, 10, 2,
          							  2, 10, 3,
          							  3, 10, 9,
          							  3, 9, 4,
          							  0, 1, 4,
          							  4, 1, 5,
          							  5, 8, 6,
          							  6, 8, 7,
          							  8, 11, 7,
          							  1, 7, 11 };
          
          Octagon::Octagon(Vector3& pos, Vector3& rot)
          {
          	for(int i = 0; i < 12; i++)
          	{
          		std::cout << octagon[i] << "\n";
          		vertices.push_back(octagon[i]);
          	}
          
          	std::cout << sizeof(octagon_index)/sizeof(const int) << "\n";
          	for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++)
          	{
          		indices.push_back(octagon_index[i]);
          	}
          
          	position = pos;
          	rotation = rot;
          }
          
          void Octagon::draw()
          {
          	glTranslatef(position.x, position.y, position.y);
          	glRotatef(rotation.x, 1.0f, 0.0f, 0.0f);
          	glRotatef(rotation.y, 0.0f, 1.0f, 0.0f);
          	glRotatef(rotation.z, 0.0f, 0.0f, 1.0f);
          
          	glBegin(GL_TRIANGLES);
          	for(int i = 0; i < 24; i++)
          	{
          		glNormal3f(0.0f, 0.0f, 1.0f);
          		glVertex3f(vertices[indices[i]].x, vertices[indices[i]].y, vertices[indices[i]].z);
          	}
          	glEnd();
          }
          The output from the constructor being:
          0 0 0 (for 12 lines)
          30

          Comment

          • RRick
            Recognized Expert Contributor
            • Feb 2007
            • 463

            #6
            When you run a simple main with the following:
            Code:
            int main ( )
            {
                Vector3 v1( 0.0f, 0.0f, -5.0f);
                Vector3 v2( 0.0, 0.0, 0.0);
                Octagon oct( v1, v2);
            }
            It will print out the correct values form the global structure.

            It looks like something else is changing the values.

            Comment

            • MrPickle
              New Member
              • Jul 2008
              • 100

              #7
              Okay, I've found the problem. It was to do with the static initialization.

              Read here: http://www.parashift.com/c++-faq-lit...html#faq-10.12

              Comment

              Working...