0

交通シミュレーションプラグインで使用している「link_time」と「vehicle_information」の2つの構造体があります。作成した2つのカスタム構造体のメモリを適切に解放(および割り当て?)するのに問題があります。

//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」の動的なサイズの配列を割り当てるためのポインタが含まれています。他のスタック投稿で、構造体の最後のメンバーとしてポインターを宣言すると、このメンバーを動的なメモリー(malloc、realloc、free)であるかのように制御できることを読みました。また、mallocごとに無料が必要であることも知っています。これは正しく行ったと思います。

'vehicle_info'構造体をmallocしてから、'vehicle_info'内の'link_time'構造体'ltt'をmallocします。'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);

ポインタやメモリの処理方法について誰かが解決策や提案を持っている場合、私は可能な限り対称にしようとしました。私は多くの義務を負います。

4

1 に答える 1

2

関数の最初の行に問題があることがわかります。メモリ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++ を使用して、この手動のメモリ制御地獄を忘れてください。

于 2012-08-21T16:44:57.413 に答える