2016-05-30 16 views
1

後に初期化されていない値(複数可)に依存します。私は途中で何か間違ったのsupsectedC(Linuxの) - valgrindの:条件付きジャンプや移動は、私は次の出力だ私のプログラムにvalgrindのを実行した後のrealloc

==17731== Thread 2: 
==17731== Conditional jump or move depends on uninitialised value(s) 
==17731== at 0x401CD8: poll_existing_connections (connmgr.c:112) 
==17731== by 0x401ACD: connmgr_listen (connmgr.c:69) 
==17731== by 0x40161A: connmgr (main.c:148) 
==17731== by 0x5545609: start_thread (in /usr/lib64/libpthread-2.22.so) 
==17731== Uninitialised value was created by a heap allocation 
==17731== at 0x4C2AB8B: realloc (vg_replace_malloc.c:785) 
==17731== by 0x401B64: poll_new_connection (connmgr.c:85) 
==17731== by 0x401AB9: connmgr_listen (connmgr.c:68) 
==17731== by 0x40161A: connmgr (main.c:148) 
==17731== by 0x5545609: start_thread (in /usr/lib64/libpthread-2.22.so) 
==17731== 

をI reallocを使用します。私はグーグルを始めて、他のユーザーのために働いていたソリューションを試しましたが、どちらのソリューションも私のために働いていませんでした。 私は別の方法(新しいメモリをmallocして新しい値で配列の古い値をコピー)を試みましたが、valgrindで同じ種類のエラーが発生しました。

何が間違っているかについてのご意見はありますか?

私のコード(connmgr.c:112):

sensor_conn_t * sensor_conn = dpl_get_element_at_index(sensor_sockets, i); 
poll_action = poll_list[i].revents == POLLIN; 
if(poll_action == POLLIN) { 
    //The sensor sent some data 
    read_data(sensor_conn, i, buffer); 
} else { 
    //No data received from the sensor 
    check_timeout(); 
} 

私のコード(connmgr.c:85):あなたはのreallocを呼び出しているが、あなたは内容を初期化していない

//Add the new connection to an array so that it is pollable 
struct pollfd * new_poll_list = realloc(poll_list, (nb_connections + 1) * sizeof(struct pollfd)); 
assert(new_poll_list != NULL); 
poll_list = new_poll_list; 

tcp_get_sd(client, &poll_list[nb_connections].fd); 
poll_list[nb_connections].events = POLLIN; 
+0

'poll_list'を大きくしましたが、どこで新しい要素を初期化しますか? 'poll_list [i] .revents'はどこで初期化されていますか? – Schwern

+1

@Schwern私は、これらの行ですべてを初期化したことを教えました:tcp_get_sd(client、&poll_list [nb_connections] .fd); poll_list [nb_connections] .events = POLLIN; 私はpoll_list [i]を初期化するのを忘れました.revents ありがとう、これは私の問題を解決しました。 – TrueStory

答えて

0

元のバッファよりも大きいバッファの基本的には、new_poll_listに格納されたすべてのメモリは初期化されていません。

reallocを呼び出した後は、元のバッファのサイズの後ろにある領域を必ず初期化してください。

+0

速い応答をありがとう、私はこれらのtzoの行でそれを初期化したことを教えた: tcp_get_sd(クライアント、&poll_list [nb_connections] .fd); poll_list [nb_connections] .events = POLLIN; 明らかに私は何か間違っています。私はそれをどのように初期化することを提案しますか? (私はCの初心者です) – TrueStory

+0

@Jonas明示的にpoll_list [nb_connections]構造体の各要素をアサーション後の値に設定します。 – bodangly

+0

あなたと@Schwernが提案したように、私はすべてを初期化しなかった(poll_list [i] .reventを忘れてしまった)。 – TrueStory

0

ここでは、フィールドを初期化されています

poll_action = poll_list[i].revents == POLLIN; 
    if(poll_action == POLLIN) 

すべて非常に良い、しかし、彼らは同じフィールドではありません。

poll_list[nb_connections].events = POLLIN; 

ここでは、フィールドの内容をテストしています。

+0

ありがとう、それは確かに問題でした – TrueStory

関連する問題