具有奇怪行为的结构向量c ++

时间:2016-03-12 19:53:39

标签: c++ vector struct stream

我有一项任务有问题。我必须读取.ts文件,读取内部的数据包并从每个数据包中提取头信息。 我创建了一个struct Packet,它将保存标头的所有信息,我还有一个向量,我将推送每个数据包。 问题是for循环因某种原因在163rd循环中停止。如果我循环直到让我们说i = 160,那么代码转义结束循环,但是当我打印vector.size()时,我得到一个非常大的数字,这是没有意义的。我猜它应该是一个整数值,与Packets的推回数一样高。这是我到目前为止的代码:

int main() {


FILE *ts_file = NULL;
ts_file = fopen64("/home/ddd/Desktop/Assignment/Streams/ddd.ts", "rb");

if (ts_file == NULL){
    cout << "No file detected on this path, try again" << endl; // prints !!!Hello World!!!
}

TS_Analyzer *ts_analyzer;
ts_analyzer->parse_file(ts_file);


cout << "Finished main" << endl;
return 0;

}

void TS_Analyzer :: parse_file(FILE * ts_file){

cout << "Inside parser" << endl;

fseek(ts_file,0,SEEK_END);
long file_size = ftell(ts_file);
rewind (ts_file);

number_of_packets = file_size/PACKET_SIZE;
unsigned int current_header_add = 0;
unsigned int i=0;


for (unsigned int j=1; i<number_of_packets; j++)
{
    i++;
    unsigned char TS_raw_header[4];

    cout  << "current position " << int(current_header_add) << endl;

    current_header_add = ftell(ts_file);

    fread(&TS_raw_header, sizeof(TS_raw_header), 1, ts_file);

    Packet current_packet;

    current_packet.sync_byte = TS_raw_header[0];
    current_packet.transport_error_indicator = (TS_raw_header[1] & 0x80) >> 7;
    current_packet.payload_start_indicator = (TS_raw_header[1] & 0x40) >> 6;
    current_packet.transport_priority = (TS_raw_header[1] & 0x20) >> 5;
    current_packet.PID = ((TS_raw_header[1] & 31) << 8) | TS_raw_header[2];
    current_packet.transport_scrambling_control = (TS_raw_header[3] & 0xC0);
    current_packet.adaption_field_control = (TS_raw_header[3] & 0x30) >> 4;
    current_packet.continuity_counter = (TS_raw_header[3] & 0xF);


    stream_packets.push_back(current_packet);

    //cout  << hex << int(current_packet.PID) << endl;
    //cout  << dec << "continuity counter " << int(current_packet.continuity_counter) << endl;
    cout  << " i " << int(i) << endl;

    fseek(ts_file, 184, SEEK_CUR);
 }
cout  << "@@" << endl;
cout << stream_packets.size() << endl;

}

class TS_Analyzer:public Analyzer {

公共:     TS_Analyzer();     〜TS_Analyzer();

struct Packet {
    unsigned char sync_byte;
    unsigned char transport_error_indicator;
    unsigned char payload_start_indicator;
    unsigned char transport_priority;
    unsigned int PID;
    unsigned char transport_scrambling_control;
    unsigned char adaption_field_control;
    unsigned char continuity_counter;
};

std::vector<Packet>stream_packets;

int number_of_packets = 0;


void parse_file(FILE *);

};

为什么矢量push_back打破了for循环以及为什么我无法获得正确的矢量大小的任何想法?

1 个答案:

答案 0 :(得分:1)

如果我把这段代码放在clang编译器中,我会在下面的代码中出错:

TS_Analyzer *ts_analyzer;
ts_analyzer->parse_file(ts_file);

>> variable 'ts_analyzer' is uninitialized when used here

我猜您遇到了未定义的行为:由于ts_analyzer作为ptr是任意随机值,其成员中的数据也非常随机。

我真的很惊讶这段代码在没有崩溃的情况下运行,尽管你总是很幸运。

如果您想解决此问题,请尝试通过在堆栈中创建对象来避免指针:

TS_Analyzer ts_analyzer;
ts_analyzer.parse_file(ts_file);

或者如果你真的需要分配内存,至少要填写指针:

auto ts_analyzer = std::make_unique<TS_Analyzer>();
ts_analyzer->parse_file(ts_file);