简体   繁体   English

在 memset 之后未写入结构数组的值

[英]Values not written to struct array after memset

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.我编写了一个程序,将值写入 c 中的结构数组,但似乎没有任何值写入结构数组。

In the first code block, I instantiate an array of structs and set the memory in that location to 0:在第一个代码块中,我实例化了一个结构数组并将该位置的 memory 设置为 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:我在 main 之前声明的 struct s_prob 如下:

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.这是因为第二个 print 语句打印的值根本不是调用 memset 之前应该有的值。 Now, no values at all are being written.现在,根本没有写入任何值。

Here's the output I was receiving before I implemented memset:这是我在实施 memset 之前收到的 output:

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.尽管所有这些值在 memset 中都是 0,即使在嵌套的 for 循环中设置了这些值之后也是如此。 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.这些值应该写入结构,但似乎它们不会持续存在,只是 go 在打印前回到 0。 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.代码的 rest 不需要任何解释,因为我认为我正在编写的值的计算与错误无关。 The program always exits with no errors, so I'm not sure why this isn't working properly.该程序始终没有错误地退出,所以我不确定为什么它不能正常工作。 Thanks!谢谢!

You're incrementing prob_count , then printing.您正在增加prob_count然后打印。 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.您在没有 memset 的情况下看到的是不确定的值。 After memset, you see the 0 values.在 memset 之后,您会看到 0 值。

First print what you updated, then increment.首先打印您更新的内容,然后增加。

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) Intead int使用size_tssize_t作为索引(如果索引可以为负)

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.由于 prob_count++ 出现在 print 语句之前,我只是把它放在之后,问题似乎已经消失了,一切都按照它应该的方式打印了。 Very strange that it did not throw an error, though!很奇怪,它没有抛出错误,虽然!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM