2016-08-19 2 views
0

앨리어싱에 문제가 있습니다. 나는 전에 그것과 결코 대면하지 않았다. Eclipse CDT를 사용하고 있습니다. 다른 해결책을 읽었지만 나에게 적합한 해결책을 찾을 수 없었습니다.경고! dereferencing type-punned 포인터가 엄격한 앨리어싱 규칙을 위반합니다 [-Wstrict-aliasing]

나는 경고를 가지고 엄격한 앨리어싱 규칙 [-Wstrict 앨리어싱]

끊어집니다 타입 punned 포인터를 역 참조

그래서 다음 코드 평화 있습니다

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에는 잘못된 값이 있습니다.

주세요! 이 문제를 해결하도록 도와주세요!

{ 
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

포장 된 구조체 (또는 endianess의 경우 2)가 더 좋을지 궁금합니다. 아니면 구조체 데이터에서 버퍼를 채우기 위해 함수를 만드는 것입니다 ... –

+0

BTW, 어떻게 값이 잘못되었다는 것을 알 수 있습니까? –

답변

1

당신은 엄격한 앨리어싱 규칙을 아프게하지 할 다음과 같은 것을 사용해야합니다 :

편집 나는 ... 구조의 my_rtppacket는 다음과 같은 방법으로 정의된다 잘못 이해하려는

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); –

관련 문제