Why does my value change without assignement?

I have an array of a struct with another array of struct as a field.
The fields get initialized, but when in the third for loop the value of why[j].number_of_sub_structs gets outrageously high even though all I do is access it.
Can anyone tell me why the number of sub structs changes randomly from my input?
If anyone has any suggestions to clean up the code that would also be helpful :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <stdlib.h>
#include <math.h>

const float DEG2RAD = 3.14159/180;
int number_of_main_structs;
using namespace std;

struct SubStruct
{
    float xPosition, yPosition, zPosition;	
		
};


struct MainStruct
{
	int number_of_sub_structs;
	
	struct SubStruct sub[];

	float xPosition,
	      yPosition,
	      zPosition,
	      xMotion,
	      yMotion,
	      zMotion;
};


void initStructs(MainStruct why[])
{
	cout << "Enter the  # of Main Structs \n";
	cin >> number_of_main_structs;
		
	for (int i=0; i< number_of_main_structs; i++) {
		cout << "Enter # of SubStructs " ;
		cin >> why[i].number_of_sub_structs;

		cout << "Enter initial y of Main struct \n";
		cin >> why[i].yPosition;
		
	}
	for (int j = 0; j < number_of_main_structs; j++) {
	        float initialYposition = why[j].yPosition;
		
		for (int k =0; k < (why[j].number_of_sub_structs) ; k++) {
			
			float degInRad = (k*10)*DEG2RAD;

			why[j].sub[k].xPosition = sin(degInRad)*5;
		
			why[j].sub[k].yPosition = initialYposition;

			why[j].sub[k].zPosition = cos(degInRad)*5;

			initialYposition = initialYposition -.1;
		}
	}
}


struct MainStruct mainstruct[10];

int main(int argc, char** argv) 
{
	
    initStructs(mainstruct);
}







Last edited on
Because you didn't initialize it.

PS: please use [code][/code] tags ( http://www.cplusplus.com/articles/firedraco1/ )
I do initialize them in ine 38(unless thats not initializing?). It even gives the correct output for the fist two passes through the last for loop but after that it goes haywire.
Actually i do not even know if that is the reason the program crashes with the exc_bad_access error.
I'm guessing it is because i put in cout statements and that was the only value that came up wrong.
Last edited on

The problem is your not allocating the array.
If you choose more than 1 substruct, your third loop is now accessing memory where xPosition, yPosition, etc exist.
I did 20 max as a test, and it did not bleed.

SubStruct sub[20];

I used a test of 7 main, then used a combo of 1,2 3,4 5,6 7,8 9,10 etc...
No bleeding occurred when I used 1 substruct.

0 1 2
1 1071309960 4.69846
2 1071309960 4.69846
3 1071309960 4.69846
4 1071309960 4.69846
5 1071309960 4.69846
6 1071309960 4.69846

with sub[20]

0 1 2
1 3 4
2 5 6
3 7 8
4 9 10
5 11 12
6 13 14






I thought about it a bit more (teaching myself c++), I think what you were looking for was dynamic arrays in a struct.

What you need to change:

struct SubStruct *sub; // assigns a new address for each main struct you create


In your code after you update the number of sub structs, you need to allocate memory for them.

cin >> why[i].number_of_sub_structs;

cout << &why[i].sub << endl; // this shows that each iteration you are using a different location for your sub struct

// this allocates the memory for your values
why[i].sub = (SubStruct*)malloc( why[i].number_of_sub_structs * sizeof( SubStruct ) );

I ran the test using 7 main structs, and then 1,2 3,4 5,6 etc..

0 1 2
1 3 4
2 5 6
3 7 8
4 9 10
5 11 12
6 13 14








I was looking for dynamic arrays in the struct.
Thank you much.
Sure - I was curious about using malloc in C++ then I found the way to properly do it in C++ using new and delete.

I updated your code a bit more - mainly trying to understand dynamic structures myself. :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#include <iostream>
#include <stdlib.h>
#include <math.h>
const float DEG2RAD = 3.14159/180;
using namespace std;

struct SubStruct
{
    float xPosition, yPosition, zPosition;  
    ~SubStruct() { cout << "SubStruct destroyed\n"; }
};


struct MainStruct
{
    int number_of_sub_structs;

    struct SubStruct *sub;

    float xPosition,
          yPosition,
          zPosition,
          xMotion,
          yMotion,
          zMotion;
    ~MainStruct() { cout << "MainStruct destroyed\n"; }
};


void initStructs(MainStruct* why, int number_of_structs)
{
    
  for (int i=0; i < number_of_structs; i++) {
        cout << i << ") Enter # of SubStructs: " ;
        cin >> why[i].number_of_sub_structs;
	//cout << &why[i].sub << endl;
	//why[i].sub = (SubStruct*)malloc( why[i].number_of_sub_structs * sizeof( SubStruct ) );
	delete [] (SubStruct*) why[i].sub;
	why[i].sub = new SubStruct[why[i].number_of_sub_structs];
	
        cout << "Enter initial y of Main struct: ";
        cin >> why[i].yPosition;
	//cout << "Values: " << why[i].number_of_sub_structs << " " << why[i].yPosition << endl;
	
  }
  
  for (int j = 0; j < number_of_structs; j++) {
        float initialYposition = why[j].yPosition;
	for (int k = 0; k < why[j].number_of_sub_structs ; k++) {
            float degInRad = (k*10)*DEG2RAD;
            why[j].sub[k].xPosition = sin(degInRad)*5;
            why[j].sub[k].yPosition = initialYposition;
            why[j].sub[k].zPosition = cos(degInRad)*5;
            initialYposition = initialYposition - 0.1;
        }
  }
    
}

void removeStructs(MainStruct* why, int number_of_structs)
{
  for (int j = 0; j < number_of_structs; j++) {
    delete [] (SubStruct*) why[j].sub;
  }
}


struct MainStruct mainstruct[10];

int main(int argc, char* argv[]) 
{
  int number_of_main_structs = 0;
  
  cout << "Enter the # of Main Structs: ";
  cin >> number_of_main_structs;

  initStructs(mainstruct, number_of_main_structs);
    
  for (int i = 0; i < number_of_main_structs; i++) 
  {
    cout << "MainStruct " << i << endl;
    cout << "  Y=" << mainstruct[i].yPosition << endl;
    for (int j = 0; j < mainstruct[i].number_of_sub_structs ; j++) 
    {
        cout << "  SubStruct " << j << endl;
	cout << "    X=" << mainstruct[i].sub[j].xPosition;
	cout << "    Y=" << mainstruct[i].sub[j].yPosition;
	cout << "    Z=" << mainstruct[i].sub[j].zPosition;
	cout << endl;
    }
  }
  
  removeStructs(mainstruct, number_of_main_structs);

  return 0;
  
}


0 3 1
1 5 4
2 8 3
3 4 2
MainStruct 0
Y=1
SubStruct 0
X=0 Y=1 Z=5
SubStruct 1
X=0.86824 Y=0.9 Z=4.92404
SubStruct 2
X=1.7101 Y=0.8 Z=4.69846
MainStruct 1
Y=4
SubStruct 0
X=0 Y=4 Z=5
SubStruct 1
X=0.86824 Y=3.9 Z=4.92404
SubStruct 2
X=1.7101 Y=3.8 Z=4.69846
SubStruct 3
X=2.5 Y=3.7 Z=4.33013
SubStruct 4
X=3.21394 Y=3.6 Z=3.83022
MainStruct 2
Y=3
SubStruct 0
X=0 Y=3 Z=5
SubStruct 1
X=0.86824 Y=2.9 Z=4.92404
SubStruct 2
X=1.7101 Y=2.8 Z=4.69846
SubStruct 3
X=2.5 Y=2.7 Z=4.33013
SubStruct 4
X=3.21394 Y=2.6 Z=3.83022
SubStruct 5
X=3.83022 Y=2.5 Z=3.21394
SubStruct 6
X=4.33012 Y=2.4 Z=2.5
SubStruct 7
X=4.69846 Y=2.3 Z=1.71011
MainStruct 3
Y=2
SubStruct 0
X=0 Y=2 Z=5
SubStruct 1
X=0.86824 Y=1.9 Z=4.92404
SubStruct 2
X=1.7101 Y=1.8 Z=4.69846
SubStruct 3
X=2.5 Y=1.7 Z=4.33013

It also frees up memory too. :) I learned a lot from your question.

SubStruct destroyed
SubStruct destroyed
SubStruct destroyed
MainStruct destroyed
MainStruct destroyed
MainStruct destroyed
Topic archived. No new replies allowed.