473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

struct losing it's value when in a global array

MrPickle
100 100+
I have a struct like so defined like so:
Expand|Select|Wrap|Line Numbers
  1. //vector3.h
  2. struct Vector3
  3. {
  4.     float x, y, z;
  5.     Vector3();
  6.     Vector3(float _x, float _y, float _z);
  7.     Vector3(const Vector3 &v);
  8. };
  9.  
  10. //vector3.cpp
  11. #include "vector3.h"
  12.  
  13. Vector3::Vector3()
  14. {
  15.     x = y = z = 0.0f;
  16. }
  17.  
  18. Vector3::Vector3(float _x, float _y, float _z)
  19. {
  20.     x = _x;
  21.     y = _y;
  22.     z = _z;
  23. }
  24.  
  25. Vector3::Vector3(const Vector3 &v)
  26. {
  27.     x = v.x;
  28.     y = v.y;
  29.     z = v.z;
  30. }
  31.  
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:
Expand|Select|Wrap|Line Numbers
  1. const Vector3 octagon[] = { Vector3(-0.5f, -1.5f, 0.0f),
  2.                       Vector3(0.5f, -1.5f, 0.0f),
  3.                       Vector3(-1.5f, -0.5f, 0.0f),
  4.                       Vector3(-1.5f, 0.5f, 0.0f),
  5.                       Vector3(-0.5f, 1.5f, 0.0f),
  6.                       Vector3(0.5f, 1.5f, 0.0f),
  7.                       Vector3(1.5f, 0.5f, 0.0f),
  8.                       Vector3(1.5f, -0.5f, 0.0f),
  9.                       Vector3(0.5f, -1.5f, 0.0f),
  10.                       Vector3(-0.5f, 0.5f, 0.0f),
  11.                       Vector3(-0.5f, -0.5f, 0.0f),
  12.                       Vector3(-1.5f, -0.5f, 0.0f) };
  13.  
However if I create a Vector3 outside of an array everything remains intact.
Nov 13 '09 #1
6 3920
weaknessforcats
9,208 Expert Mod 8TB
It looks like everything is there in my debugger.
Nov 13 '09 #2
MrPickle
100 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Octagon::Octagon(Vector3& pos, Vector3& rot)
  2. {
  3.     for(int i = 0; i < 12; i++)
  4.     {
  5.         vertices.push_back(octagon[i]);
  6.     }
  7.  
  8.     for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++)
  9.     {
  10.         indices.push_back(octagon_index[i]);
  11.     }
  12.  
  13.     position = pos;
  14.     rotation = rot;
  15. }
Nov 13 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
I'm soory but this looks odd:

Expand|Select|Wrap|Line Numbers
  1. Octagon::Octagon(Vector3& pos, Vector3& rot) 
  2.     for(int i = 0; i < 12; i++) 
  3.     { 
  4.         vertices.push_back(octagon[i]); 
  5.     } 
  6.  
  7.     for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++) 
  8.     { 
  9.         indices.push_back(octagon_index[i]); 
  10.     } 
  11.  
  12.     position = pos; 
  13.     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.
Nov 13 '09 #4
MrPickle
100 100+
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:
Expand|Select|Wrap|Line Numbers
  1. //main.cpp
  2. #include "octagon.h"
  3.  
  4. Octagon oct(Vector3(0.0f, 0.0f, -5.0f));
  5.  
  6. //octagon.h
  7. #include <vector>
  8. #include "vector.h"
  9.  
  10. struct Octagon
  11. {
  12.     Octagon(Vector3& pos, Vector3& rot = Vector3());
  13.     void draw();
  14.  
  15.     std::vector<Vector3> vertices;
  16.     std::vector<int> indices;
  17.  
  18.     Vector3 position, rotation, colour;
  19. };
  20.  
  21. //octagon.cpp
  22. #include "octagon.h"
  23.  
  24. const Vector3 octagon[] = { Vector3(-0.5f, -1.5f, 0.0f),
  25.                             Vector3(0.5f, -1.5f, 0.0f),
  26.                             Vector3(-1.5f, -0.5f, 0.0f),
  27.                             Vector3(-1.5f, 0.5f, 0.0f),
  28.                             Vector3(-0.5f, 1.5f, 0.0f),
  29.                             Vector3(0.5f, 1.5f, 0.0f),
  30.                             Vector3(1.5f, 0.5f, 0.0f),
  31.                             Vector3(1.5f, -0.5f, 0.0f),
  32.                             Vector3(0.5f, -1.5f, 0.0f),
  33.                             Vector3(-0.5f, 0.5f, 0.0f),
  34.                             Vector3(-0.5f, -0.5f, 0.0f),
  35.                             Vector3(-1.5f, -0.5f, 0.0f) };
  36.  
  37. const int octagon_index[] = { 0, 10, 2,
  38.                               2, 10, 3,
  39.                               3, 10, 9,
  40.                               3, 9, 4,
  41.                               0, 1, 4,
  42.                               4, 1, 5,
  43.                               5, 8, 6,
  44.                               6, 8, 7,
  45.                               8, 11, 7,
  46.                               1, 7, 11 };
  47.  
  48. Octagon::Octagon(Vector3& pos, Vector3& rot)
  49. {
  50.     for(int i = 0; i < 12; i++)
  51.     {
  52.         std::cout << octagon[i] << "\n";
  53.         vertices.push_back(octagon[i]);
  54.     }
  55.  
  56.     std::cout << sizeof(octagon_index)/sizeof(const int) << "\n";
  57.     for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++)
  58.     {
  59.         indices.push_back(octagon_index[i]);
  60.     }
  61.  
  62.     position = pos;
  63.     rotation = rot;
  64. }
  65.  
  66. void Octagon::draw()
  67. {
  68.     glTranslatef(position.x, position.y, position.y);
  69.     glRotatef(rotation.x, 1.0f, 0.0f, 0.0f);
  70.     glRotatef(rotation.y, 0.0f, 1.0f, 0.0f);
  71.     glRotatef(rotation.z, 0.0f, 0.0f, 1.0f);
  72.  
  73.     glBegin(GL_TRIANGLES);
  74.     for(int i = 0; i < 24; i++)
  75.     {
  76.         glNormal3f(0.0f, 0.0f, 1.0f);
  77.         glVertex3f(vertices[indices[i]].x, vertices[indices[i]].y, vertices[indices[i]].z);
  78.     }
  79.     glEnd();
  80. }
The output from the constructor being:
0 0 0 (for 12 lines)
30
Nov 13 '09 #5
RRick
463 Expert 256MB
When you run a simple main with the following:
Expand|Select|Wrap|Line Numbers
  1. int main ( )
  2. {
  3.     Vector3 v1( 0.0f, 0.0f, -5.0f);
  4.     Vector3 v2( 0.0, 0.0, 0.0);
  5.     Octagon oct( v1, v2);
  6. }
It will print out the correct values form the global structure.

It looks like something else is changing the values.
Nov 14 '09 #6
MrPickle
100 100+
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
Nov 15 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott Lyon | last post by:
I'm maintaining (read: I didn't write it, nor do I have the time to spend to rewrite it) an application that is suddenly giving me grief. The reason I say suddenly, is because we're in the...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
2
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code: In global--> public struct point { int x; string y; } static public point point;
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.