简体   繁体   English

在struct free()中动态分配struct

[英]C dynamic allocate struct within struct free()

I have two structs, "link_time" and "vehicle_information" which I am using in a traffic simulation plugin. 我有两个结构,“link_time”和“vehicle_information”,我在流量模拟插件中使用它。 I am having difficulty properly freeing (and allocating?) memory for the two custom structs I have created. 我很难为我创建的两个自定义结构正确释放(和分配?)内存。

//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" contains a pointer for the allocation of a dynamically sized array of "link_time". “vehicle_information”包含用于分配动态大小的“link_time”数组的指针。 I've read in other stack posts that if I declare a pointer as the last member of my struct, then I can control this member as if it were a dynamic piece of memory (malloc, realloc, free). 我已经在其他堆栈帖子中读到,如果我将指针声明为我的struct的最后一个成员,那么我可以控制这个成员,就好像它是一个动态的内存片段(malloc,realloc,free)。 I also know that I need a free for every malloc, which I believe I have done correctly. 我也知道我需要一个免费的每一个malloc,我相信我已经做得正确。

I malloc for the 'vehicle_info' struct and then the 'link_time' struct 'ltt' within 'vehicle_info'. 我在'vehicle_info'结构中使用malloc,然后在'vehicle_info'中使用'link_time'结构'ltt'。 I would assume I free in the reverse order, since 'ltt' is linked in memory to 'vehicle_info? 我会假设我以相反的顺序自由,因为'ltt'在内存中链接到'vehicle_info? But the HEAP complains and my program crashes when I get to the following line in the function 'vehicle_information_template* addToGlobalVehicleArray()': 但当我到达'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);

Every time a vehicle enters the simulation, it should be added to the "GLOBAL_VEHICLE_INFO" array. 每次车辆进入模拟时,都应将其添加到“GLOBAL_VEHICLE_INFO”阵列中。 Pseudo code below: 下面的伪代码:

    ...
    //***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);

If anybody has any solutions or suggestions about how I'm handling my pointers or memory, I tried to be as symmetric as possible, I would be much obliged. 如果有人对我如何处理我的指针或内存有任何解决方案或建议,我试图尽可能对称,我会非常感激。

I see the problem in the first lines of addToGlobalVehicleArray function - you allocate TEMP_VEHICLE_INFO[k].ltt memory but then you lost it overriding by TEMP_VEHICLE_INFO[k] = target_array[k] code line. 我在addToGlobalVehicleArray函数的第一行看到了这个问题 - 你分配了TEMP_VEHICLE_INFO[k].ltt内存但是你丢失了它覆盖了TEMP_VEHICLE_INFO[k] = target_array[k]代码行。

Also suggest to make you code more structurized - introduce separate functions create_link_time_array() / free_link_time_array() , create_veh_info_array() / free_veh_info_array() to move service code away from main control flow and make code more clear and readable. 还建议使代码更结构化 - 引入单独的函数create_link_time_array() / free_link_time_array()create_veh_info_array() / free_veh_info_array()以使服务代码远离主控制流并使代码更清晰和可读。 This will help you a lot to avoid such problems. 这将帮助您避免此类问题。

Or better - use C++ and forget this manual memory control hell. 或者更好 - 使用C ++并忘记这个手动内存控制地狱。

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

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