2016-08-19 29 views
0

我有混淆問題。我從來沒有面對過它。 我正在使用Eclipse CDT。 我讀了不同的解決方案,但我無法找到合適的解決方案。警告!解引用打字指針會打破嚴格別名規則[-Wstrict-aliasing]

我警告

提領型punned指針將打破嚴格走樣規則[-Wstrict走樣]

所以,我有以下的代碼和平:

timestamp = st0 % 100000000; 

for (std::list<struct trace *>::iterator it = frame_list.begin(); 
    it != frame_list.end(); 
    ++it) { 
    struct my_rtppacket *packet = NULL; 
    packet = new struct my_rtppacket; 

    packet->ts = (int32_t)((*it)->time * 1000000); 
    packet->seq = count_seq; 

    //In the following two strings I have the warnings :(

    *(uint16_t*) (packet->buf + 2) = htons(packet->seq); 
    *(int32_t*) (packet->buf + 4) = htonl(packet->ts + timestamp); 

    insert_data_to_list(packet); // This function inserts packets to list} 
} 

結果在packet->buf + 2packet->buf + 4中存在錯誤的值。

請!幫我解決這個問題!

編輯 我想知道什麼是錯的...結構my_rtppacket是這樣定義的:

{ 
struct my_rtppacket 
{ 
public: 
    my_rtppacket():dump_ts(0), payloadlen(0),packetlen(0),ts(0), seq(0), seq_fr(0), frame_number(0), 
    erase(NUM, INIT), path(NUM, INIT), create_time(0), alloc_time(0), before_create(0), sent_flag(0){} 
    uint32_t dump_ts; /*timestamp of RTP dump. It is similar to timestamp of packet generation from the application*/ 
    int payloadlen; 
    int packetlen; 
    int32_t ts;   /*timestamp in RTP file*/ 
    uint16_t seq;  /* Sequеnce number in video sequence*/ 
    int seq_fr;   /* Sequеnce number in a frame*/ 
    int frame_number; 
    char buf[1600]; 
    std::vector <path_t> erase; 
    std::vector <path_t> path;  //Declare a vector of path_type elements 
    char frame_type[10]; 
    int64_t create_time; 
    int64_t alloc_time; 
    int64_t before_create; 
    int sent_flag; 

}; 

} 
+3

那個指針算術,重新解釋演員等肯定看起來陰暗。但是,您需要向我們提供「struct my_rptpacket」的定義。 –

+0

我不知道一個壓縮結構(或2爲endianess)會更好。或者可能創建一個函數來填充結構數據中的緩衝區... –

+0

順便說一句,你如何看待這些值是錯誤的? –

回答

1

你應該使用類似下面要不要打破嚴格走樣規則:

const uint16_t nseq = htons(packet->seq); 
const uint32_t nts = htonl(packet->ts + timestamp); 
memcpy(packet->buf + 2, &nseq, 2); 
memcpy(packet->buf + 4, &nts, 4); 
+0

這個解決方案沒問題,但是可以檢查寫入packet-> buf + 2的內容嗎? –

+0

@EkaterinaPakulova如果你仍然想測試寫入'packet-> buf'的方法,使用建議的方法來回讀並驗證:'memcpy(&nseq_test_var,packet-> buf + 2,2);' –

相關問題