I've written a program that writes values to an array of structs in c, but none of the values seem to be writing into the struct array.
In the first code block, I instantiate an array of structs and set the memory in that location to 0:
struct s_prob prob_values[(int) pow(n, 2)];
memset(prob_values, 0, sizeof(prob_values));
The struct s_prob, which I declare before main, is as follows:
struct s_prob {
int index;
float probability;
unsigned long count;
};
And the code I use to write values to the struct array is as follows:
int prob_count = 0;
for (int i = 0; i < n; i ) {
for (int j = 0; j < n; j ) {
printf("%d, %d, ", prob_count, prob_values[prob_count].index);
prob_values[prob_count].index = (int) prob_count;
prob_values[prob_count].probability = (float) pow((1.0 - p_x_error), n - i) * pow((1.0 - p_z_error), num_data_qubits - j) * pow(p_x_error, i) * pow(p_z_error, j);
prob_values[prob_count].count = (unsigned long) pascal_triangle[n][i j];
prob_count ;
printf("At index %d: prob: %e, count: %lu\n", prob_values[prob_count].index, prob_values[prob_count].probability, prob_values[prob_count].count);
}
}
The first print statement is formatted the way it is because I wanted to compare the values of the index to the counter to make sure they were the same. This is because the values being printed by the second print statement were not at all what they were supposed to be prior to calling memset. Now, no values at all are being written.
Here's the output I was receiving before I implemented memset:
0, 713, At index 0: prob: 0.000000e 00, count: 456
1, 0, At index 0: prob: 0.000000e 00, count: 0
2, 0, At index -890830848: prob: 6.235778e-43, count: 1914664608552
3, -890830848, At index 0: prob: 0.000000e 00, count: 0
4, 0, At index 0: prob: 0.000000e 00, count: 4112
5, 0, At index 0: prob: 0.000000e 00, count: 1914664608560
6, 0, At index 0: prob: 0.000000e 00, count: 1914664591506
7, 0, At index -890805472: prob: 6.235778e-43, count: 3372220617
8, -890805472, At index 0: prob: 0.000000e 00, count: 0
9, 0, At index -890830512: prob: 6.235778e-43, count: 0
10, -890830512, At index 73: prob: 0.000000e 00, count: 1023
11, 73, At index -890805472: prob: 6.235778e-43, count: 2533359767
12, -890805472, At index 201: prob: 0.000000e 00, count: 0
13, 201, At index 0: prob: 0.000000e 00, count: 0
14, 0, At index 0: prob: 0.000000e 00, count: 257
15, 0, At index 8: prob: 0.000000e 00, count: 0
16, 8, At index 4112: prob: 0.000000e 00, count: 4096
17, 4112, At index 2: prob: 0.000000e 00, count: 1914664583168
18, 2, At index 129: prob: 0.000000e 00, count: 140727915623501
19, 129, At index -890830848: prob: 6.235778e-43, count: 10
20, -890830848, At index 4096: prob: 0.000000e 00, count: 0
21, 4096, At index -890819760: prob: 6.235778e-43, count: 105920832884
22, -890819760, At index 0: prob: 4.203895e-45, count: 0
23, 0, At index 0: prob: 0.000000e 00, count: 4112
24, 0, At index 0: prob: 0.000000e 00, count: 0
25, 0, At index 0: prob: 0.000000e 00, count: 1914664591506
26, 0, At index 0: prob: 0.000000e 00, count: 3405775306
27, 0, At index 0: prob: 0.000000e 00, count: 2533359767
28, 0, At index 0: prob: 0.000000e 00, count: 0
29, 0, At index 0: prob: 0.000000e 00, count: 0
30, 0, At index 0: prob: 0.000000e 00, count: 2533359767
31, 0, At index 0: prob: 0.000000e 00, count: 0
32, 0, At index 4: prob: 0.000000e 00, count: 4294967295
33, 4, At index -919681840: prob: 4.590514e-41, count: 105920833784
34, -919681840, At index 0: prob: 0.000000e 00, count: 4096
35, 0, At index 1: prob: 0.000000e 00, count: 140727914045745
36, 1, At index 0: prob: 0.000000e 00, count: 4096
37, 0, At index 129: prob: 0.000000e 00, count: 140727915577344
38, 129, At index -890830848: prob: 6.235778e-43, count: 10
39, -890830848, At index 4096: prob: 0.000000e 00, count: 140727914045119
40, 4096, At index 0: prob: 0.000000e 00, count: 105920833188
41, 0, At index 0: prob: 4.203895e-45, count: 140727915775920
42, 0, At index 5: prob: 0.000000e 00, count: 140727914280046
43, 5, At index -983893456: prob: 4.591354e-41, count: 140727914527280
44, -983893456, At index 1: prob: 0.000000e 00, count: 4096
45, 1, At index 0: prob: 0.000000e 00, count: 140727914188698
46, 0, At index -983893456: prob: 4.591354e-41, count: 140727914179194
47, -983893456, At index 131: prob: 0.000000e 00, count: 1914664585312
48, 131, At index 10: prob: 0.000000e 00, count: 140727914254959
49, 10, At index 72: prob: 0.000000e 00, count: 140727914527280
50, 72, At index -983893456: prob: 4.591354e-41, count: 10
51, -983893456, At index 0: prob: 0.000000e 00, count: 4294967295
52, 0, At index 24576: prob: 0.000000e 00, count: 140702208911160
53, 24576, At index -919683005: prob: 4.590514e-41, count: 140727914527280
54, -919683005, At index -919683010: prob: 4.590514e-41, count: 140727914045745
55, -919683010, At index 0: prob: 0.000000e 00, count: 140727914188698
56, 0, At index -983893360: prob: 3.082857e-44, count: 0
57, -983893360, At index 0: prob: 0.000000e 00, count: 0
58, 0, At index 55: prob: 0.000000e 00, count: 140727914254959
59, 55, At index 0: prob: 0.000000e 00, count: 0
60, 0, At index -983893456: prob: 4.591354e-41, count: 18446744069414608896
61, -983893456, At index -1: prob: nan, count: 140702208924636
62, -1, At index -1453387905: prob: 1.254162e-42, count: 4611686018427387904
63, -1453387905, At index -1453348608: prob: 3.363116e-44, count: 4611686018427387904
64, -1453348608, At index -983893456: prob: 4.591354e-41, count: 140702208902446
65, -983893456, At index -1453348656: prob: 3.363116e-44, count: 140727914527280
66, -1453348656, At index -919683010: prob: 4.590514e-41, count: 5
67, -919683010, At index 16: prob: 0.000000e 00, count: 140702208901562
68, 16, At index 24576: prob: 0.000000e 00, count: 140727913971049
69, 24576, At index -1453348616: prob: 3.363116e-44, count: 20
70, -1453348616, At index -1453348616: prob: 3.363116e-44, count: 0
71, -1453348616, At index -1453348616: prob: 3.363116e-44, count: 4611686018427387904
72, -1453348616, At index 0: prob: 0.000000e 00, count: 0
73, 0, At index 0: prob: 0.000000e 00, count: 0
74, 0, At index 0: prob: 0.000000e 00, count: 20
75, 0, At index 20: prob: 0.000000e 00, count: 16
76, 20, At index 0: prob: 0.000000e 00, count: 1296
77, 0, At index 0: prob: 1.875000e 00, count: 140702208892796
78, 0, At index -919683010: prob: 4.590514e-41, count: 1
79, -919683010, At index 1074790400: prob: 0.000000e 00, count: 0
80, 1074790400, At index -61569: prob: nan, count: 64
While all of these values are 0 with memset, even after setting these values from within the nested for loops. Does anyone know what I'm doing wrong? The values should be writing to the struct, but it seems that they don't persist and just go back to 0 before printing. The rest of the code shouldn't need any explanation since I don't think the calculation of the values I'm writing has anything to do with the error. The program always exits with no errors, so I'm not sure why this isn't working properly. Thanks!
CodePudding user response:
The problem is here:
prob_count ;
printf("At index %d: prob: %e, count: %lu\n", prob_values[prob_count].index, prob_values[prob_count].probability, prob_values[prob_count].count);
You do not print the struct you have just assigned only the next one which is zeroed. It also leads to Undefined Behaviour on the last iteration.
It has to be:
printf("At index %d: prob: %e, count: %lu\n", prob_values[prob_count].index, prob_values[prob_count].probability, prob_values[prob_count].count);
prob_count ;
Additionally:
(int) pow(n, 2)
Never use floating point functions when dealing with integers. Simply do:
n * n
Use the correct type for sizes. Intead of int
use size_t
or ssize_t
for indexes (if index can be negative)
CodePudding user response:
You're incrementing prob_count
, then printing. So you're not actually printing the values you just set but the contents of the next member which hasn't been set. What you're seeing without memset are indeterminate values. After memset, you see the 0 values.
First print what you updated, then increment.
CodePudding user response:
It seems as though it was an indexing error. Since prob_count came before the print statement, I just put it after, and the issue seems to have gone away and everything prints the way it should. Very strange that it did not throw an error, though!