我有两个结构,“link_time”和“vehicle_information”,我在交通模拟插件中使用它们。我很难为我创建的两个自定义结构正确释放(和分配?)内存。
//global vardeclarations
//GLOBAL_VEHICLE_INFO contains all of the "vehicle_information" structs
//representing all of the vehicles within the simulation
vehicle_information_template* GLOBAL_VEHICLE_INFO;
int lengthGLOBALVEHICLE;
vehicle_information_template* TEMP_VEHICLE_INFO;
//struct definitions
typedef struct link_time{
float link_duration;
float timestamp;
}link_time_template;
typedef struct vehicle_information{
int id;
int current_link_id;
float start_time;
link_time_template* ltt; //individual vehicle link travel times array
}vehicle_information_template;
//function to add new "vehicle_information_template" to GLOBAL_VEHICLE_ARRAY
vehicle_information_template* addToGlobalVehicleArray(vehicle_information_template* target_array, int array_length, vehicle_information_template new_vinfo){
int k;
//allocate space for temp holding array, both the 'vehicle_information' struct and the 'link_time' struct within 'vehicle_information' struct
TEMP_VEHICLE_INFO = malloc( sizeof(vehicle_information_template) * (array_length+1) );
for(k=0;k<array_length+1;k++){
TEMP_VEHICLE_INFO[k].ltt = malloc( sizeof(link_time_template) * NUM_LINKS);
}
//copy contents so that target_array can be free'd and resized
for(k=0;k<array_length;k++){
TEMP_VEHICLE_INFO[k] = target_array[k];
}
//add the new vehicle_info to the end of the TEMP_VEHICLE_INFO(newly resized)
TEMP_VEHICLE_INFO[array_length] = new_vinfo;
//****BREAKS HERE *****
//free target_array (GLOBAL_VEHICLE_ARRAY) so that it can be resized to one element larger
//free the struct in the reverse order, freeing 'ltt' first, as it is a member of the larger struct 'vehicle_info'
for(k=0;k<array_length;k++){
free(target_array[k].ltt);
}
free(target_array);
//reallocate GLOBAL_VEHICLE to 1 size larger to accomodate new element
target_array = malloc(sizeof(vehicle_information_template) * (array_length+1) );
//allocate space for "link_time" struct within "vehicle_information" struct
for(k=0;k<array_length+1;k++){
target_array[k].ltt = malloc(sizeof(link_time_template) * NUM_LINKS);
}
//copy contents from temp array to global array
for(k=0;k<(array_length+1);k++){
target_array[k] = TEMP_VEHICLE_INFO[k];
}
//free temp array struct
for(k=0;k<array_length;k++){
free(TEMP_VEHICLE_INFO[k].ltt);
}
free(TEMP_VEHICLE_INFO);
//return newly sized global array
return target_array;
}
“vehicle_information”包含一个用于分配动态大小的“link_time”数组的指针。我在其他堆栈帖子中读到,如果我将指针声明为结构的最后一个成员,那么我可以控制该成员,就好像它是一 block 动态内存(malloc、realloc、free)一样。我还知道我需要为每个 malloc 分配一个 free,我相信我做得正确。
我为“vehicle_info”结构分配内存,然后为“vehicle_info”内的“link_time”结构“ltt”分配内存。我假设我以相反的顺序释放,因为“ltt”在内存中链接到“vehicle_info?”但是当我到达函数“vehicle_information_template* addToGlobalVehicleArray()”中的以下行时,HEAP 提示并且我的程序崩溃了:
//free target_array (GLOBAL_VEHICLE_ARRAY) so that it can be resized to one element larger
//free the struct in the reverse order, freeing 'ltt' first, as it is a member of the larger struct 'vehicle_info'
for(k=0;k<array_length;k++){
free(target_array[k].ltt);
}
free(target_array);
每次车辆进入模拟时,都应将其添加到“GLOBAL_VEHICLE_INFO”数组中。伪代码如下:
...
//***vehicle enters network
vehicle_information_template temp_v;
int k;
//set temp_v to the newly released vehicle's information so it can be added to GLOBAL_VEHICLE_INFO
temp_v.id = qpg_VHC_uniqueID(v);
temp_v.current_link_id = qpg_LNK_index(qpg_VHC_link(v));
temp_v.start_time = -1.0;
//allocate memory for temp_v.ltt
temp_v.ltt = malloc(sizeof(link_time_template) * NUM_LINKS);
//set link_duration and timestamp to dummy values
for(k=1;k<=NUM_LINKS;k++){
temp_v.ltt[k].link_duration = -1.0;
temp_v.ltt[k].timestamp = -1.0;
}
//add temp_v to "GLOBAL_VEHICLE_INFO" using function
GLOBAL_VEHICLE_INFO = addToGlobalVehicleArray( GLOBAL_VEHICLE_INFO, lengthGLOBALVEHICLE, temp_v);
lengthGLOBALVEHICLE++;
//free allocated temp_v.ltt (link_travel_time)
free(temp_v.ltt);
如果有人对我如何处理指针或内存有任何解决方案或建议,我会尽力保持对称,我将非常感激。
最佳答案
我在 addToGlobalVehicleArray
函数的第一行中看到了问题 - 您分配了 TEMP_VEHICLE_INFO[k].ltt
内存,但随后您通过 TEMP_VEHICLE_INFO[ 覆盖了它k] = target_array[k]
代码行。
还建议让您的代码更加结构化 - 引入单独的函数create_link_time_array()
/free_link_time_array()
、create_veh_info_array()
/free_veh_info_array()
将服务代码移离主控制流,使代码更加清晰易读。这将极大地帮助您避免此类问题。
或者更好 - 使用 C++ 并忘记这个手动内存控制 hell 。
关于C 在 struct free() 中动态分配结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12059065/