简体   繁体   English

更新结构中的变量值会导致同一结构中的另一个变量被修改

[英]Updating a variable's value in a struct cause another variable from the same struct to be modified

I'm working on a code which handles traffic in intersections over time and returns how much traffic there is in an intersection at a given time. 我正在编写处理随时间推移交叉路口的交通并返回给定时间交叉路口有多少交通的代码。

Basically the problem I have is with an array of structs where each struct contains 2 vectors: one with intersection's coordinates (x, y) and the other one keeps track of how many cars passed at time t into that intersection (every slot from 0 to n represents time, the value inside is the number of cars that crossed at that time). 基本上,我遇到的问题是一个结构数组,其中每个结构包含2个向量:一个具有交叉点的坐标(x,y),另一个跟踪在时间t进入那个交叉点的车辆数量(每个插槽从0到n代表时间,其中的值是当时穿越的汽车数量)。

Here's the struct: 这是结构:

typedef struct intersection {
  int coords[2];
  int timeslots[];
} intersection;

which is contained in intersections array from this struct: 它包含在此结构的交集数组中:

typedef struct simulation {
  int h_streets; //row (horizontal streets)
  int v_streets; //column (vertical streets)
  int n_cars;    //cars number
  int n_trips;   //trips number

  car car_pos [1000];

  intersection intersections [];
} simulation;

Memory is then allocated with malloc to define actual array size for flexible arrays when needed data are read: 然后,在读取所需数据时,使用malloc分配内存以定义灵活数组的实际数组大小:

  struct simulation * this = malloc( sizeof(simulation) + (sizeof(intersection)*(h_streets*v_streets)*(sizeof(int)*(max_time+1001))) );

When reading data regarding a car's trip from one position to another, what I do is, as said before, incrementing timeslots[t] by one when the car crosses that intersection at that time. 如前所述,当读取有关汽车从一个位置到另一个位置的行程的数据时,我要做的是,当汽车在那个时间穿过交叉点时,将时隙[t]增加一个。

The issue is that, when I do so with this line of code contained in a while cycle 问题是,当我使用while周期中包含的这一行代码时,

this->intersections[curr_int].timeslots[starting_t+travel_t]++;

the value from coords array are modified. 来自coords数组的值被修改。

Here's an example when printing out just the intersections coordinates at each cycle: 这是在每个周期仅打印出相交坐标时的示例:

------------------
i: 0, h: 0, v: 0
i: 1, h: 0, v: 1
i: 2, h: 0, v: 2
i: 3, h: 0, v: 3
i: 4, h: 1, v: 0
i: 5, h: 1, v: 1
i: 6, h: 1, v: 2
i: 7, h: 1, v: 3
i: 8, h: 2, v: 0
i: 9, h: 2, v: 1
i: 10, h: 2, v: 2
i: 11, h: 2, v: 3
i: 12, h: 3, v: 0
i: 13, h: 3, v: 1
i: 14, h: 3, v: 2
i: 15, h: 3, v: 3
------------------
------------------
i: 0, h: 0, v: 0
i: 1, h: 1, v: 1
i: 2, h: 0, v: 2
i: 3, h: 0, v: 3
i: 4, h: 1, v: 0
i: 5, h: 1, v: 1
i: 6, h: 1, v: 2
i: 7, h: 1, v: 3
i: 8, h: 2, v: 0
i: 9, h: 2, v: 1
i: 10, h: 2, v: 2
i: 11, h: 2, v: 3
i: 12, h: 3, v: 0
i: 13, h: 3, v: 1
i: 14, h: 3, v: 2
i: 15, h: 3, v: 3
------------------
------------------
i: 0, h: 0, v: 0
i: 1, h: 1, v: 2
i: 2, h: 0, v: 2
i: 3, h: 0, v: 3
i: 4, h: 1, v: 0
i: 5, h: 1, v: 1
i: 6, h: 1, v: 2
i: 7, h: 1, v: 3
i: 8, h: 2, v: 0
i: 9, h: 2, v: 1
i: 10, h: 2, v: 2
i: 11, h: 2, v: 3
i: 12, h: 3, v: 0
i: 13, h: 3, v: 1
i: 14, h: 3, v: 2
i: 15, h: 3, v: 3
------------------
------------------
i: 0, h: 0, v: 0
i: 1, h: 1, v: 2
i: 2, h: 0, v: 2
i: 3, h: 0, v: 3
i: 4, h: 2, v: 0
i: 5, h: 1, v: 1
i: 6, h: 1, v: 2
i: 7, h: 1, v: 3
i: 8, h: 2, v: 0
i: 9, h: 2, v: 1
i: 10, h: 2, v: 2
i: 11, h: 2, v: 3
i: 12, h: 3, v: 0
i: 13, h: 3, v: 1
i: 14, h: 3, v: 2
i: 15, h: 3, v: 3
------------------
[...]

(i is the counter for the cycle which accesses intersections[] in position i, whereas h and v are horizontal and vertical coordinates of the intersection i contained in the coords[] array) (i是访问位置i处的交叉点[]的循环的计数器,而h和v是coords []数组中包含的交叉点i的水平和垂直坐标)

As you can notice, coordinates of some intersections are modified after every cycle even though I'm not even accessing that array with the incrementing function 如您所见,即使我甚至没有使用增量函数访问该数组,某些交点的坐标也会在每个循环后进行修改

this->intersections[curr_int].timeslots[starting_t+travel_t]++;

which is the one causing this problem. 这是导致此问题的原因。 How is this possible? 这怎么可能? Could it be a memory allocation issue? 可能是内存分配问题吗?

In this code that declares a member of simulation : 在这段代码中,它声明了simulation的成员:

intersection intersections [];

intersections is a structure with a flexible array member, so the code above attempts to create an array of structures with a flexible array member. intersections是具有柔性数组成员的结构,因此上面的代码尝试创建具有柔性数组成员的结构的数组。

This cannot work. 这行不通。 The size of a structure with a flexible array member is as if the flexible array member were omitted (except for a potential issue about padding for alignment). 具有柔性阵列构件的结构的大小就好像省略了柔性阵列构件(除了可能存在的有关对齐填充的问题)。 This compiler cannot account for the sizes of flexible arrays when calculating the structure size or performing array index calculations. 在计算结构大小或执行数组索引计算时,此编译器无法考虑柔性数组的大小。

This code attempts to use the array of structures with flexible array members: 此代码尝试将结构数组与灵活的数组成员一起使用:

this->intersections[curr_int].timeslots[starting_t+travel_t]++; this-> intersections [curr_int] .timeslots [starting_t + travel_t] ++;

In this code, the compiler cannot know how many elements the array this->intersections[curr_int].timeslots has. 在此代码中,编译器无法知道this->intersections[curr_int].timeslots具有多少个元素。 Since timeslots is a flexible array member, each instance of an intersection may have a different number of elements in its timeslots —the intent is for the programmer to provide whatever amount of space is desired for each instance. 由于timeslots是灵活的数组成员,所以intersection每个实例在其timeslots中可以具有不同数量的元素-程序员的目的是为每个实例提供所需的任何空间量。 Because this amount may vary with each instance, they cannot be arranged into an array of fixed-size elements, and the compiler cannot know how big each one is. 由于此数量可能随每个实例而变化,因此无法将它们排列成固定大小的元素数组,并且编译器无法知道每个元素的大小。

Since the compiler does not know how many elements the programmer intends there to be in each timeslots , it does not know how big each intersection is. 由于编译器不知道程序员打算在每个timeslots多少个元素,因此不知道每个intersection有多大。 When calculating the address for this->intersections[curr_int] , the compiler only uses the size of the structure as if defined without the flexible array member (except for the padding issue). 在计算this->intersections[curr_int]的地址时,编译器仅使用结构的大小,就好像没有柔性数组成员时定义的那样(填充问题除外)。 This calculation will not be correct when the programmer intends flexible array members to be present. 当程序员打算使用灵活的数组成员时,此计算将是不正确的。

Both Clang and GCC warn about this when the -pedantic switch is used. 使用-pedantic开关时,Clang和GCC均会发出警告。

An array of structures with flexible array members cannot work. 具有灵活数组成员的结构的数组无法工作。 You must use another design for your structures. 您必须为结构使用其他设计。 In each intersection , timeslots could be a pointer to space for the array of time slots instead of being a flexible array member. 在每个intersectiontimeslots可以是指向timeslots数组空间的指针,而不是灵活的数组成员。

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

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