我有一些旧代码,这些代码使用的指针旨在沿char数组缓冲区“遍历”。 struct指针用于通过网络发送数据,但是在发送前计算校验和并将其存储在struct中。但是,发生段错误,似乎是在使用ftree-vectorize
的时候。在gcc 4.4.7
中使用提供的相同标志进行编译时,这似乎起作用。正在为32-bit
进行编译。
我让代码在没有ftree-vectorize
的情况下运行,我不确定矢量在低层如何工作。我还添加了ftree-vectorizer-verbose=6
来尝试通读日志,但我确实没有多大意义。
我试图删除大量的#defines,并使代码简洁明了。.
以下标志也用于编译:
GCC_OPTIMIZATIONS ?= -march=i686 -mmmx -msse -msse2 -O1 -ftree-parallelize-loops=8 -fpredictive-commoning -ftree-vectorize -finline-functions
// a header for information about data
typedef struct Record_Hdr {
struct {
unsigned long message_id: 12;
unsigned long res: 2;
unsigned long fr_flag: 1;
unsigned long cr_flag: 1;
unsigned long message_length: 16;
} wrd0;
unsigned long sync_id;
struct {
unsigned long sequence_number: 8;
unsigned long app_flag2: 4;
unsigned long app_flag1: 4;
unsigned long destination_id: 8;
unsigned long source_id: 8;
} wrd2;
unsigned long timestamp;
unsigned long checksum;
} Record_Hdr;
// Header and data
typedef struct {
Record_Hdr hdr;
unsigned char data[1004];
} RECORD;
// method defined in another class used below
unsigned char sgf_pkt[1400];
int sgf_pkt_len = 0;
inline char* get_sgf_pkt_msg_ptr() {
return (char*)&sgf_pkt[sgf_pkt_len];
}
问题代码:
RECORD *out_msg = NULL;
int msg_length = 85;
out_msg = (RECORD*)mdt_io.get_pkt_msg_ptr();
// set the necessities
out_msg->hdr.wrd0.message_id = 0x25;
out_msg->hdr.wrd0.message_length = msg_length;
out_msg->hdr.wrd2.source_id = 104;
out_msg->hdr.wrd2.destination_id = 0;
out_msg->hdr.sync_id = 0x5555AAAA;
out_msg->hdr.timestamp = timeInTicks;
out_msg->hdr.checksum = 0;
// checksum calculation
unsigned long chkSum, *datap;
int i;
chkSum = 0;
datap = (unsigned long *) &(out_msg->hdr.wrd0);
// runs 85 times...
for (i=0; i < msg_length; i++) {
chkSum ^= *datap++; // SEG FAULTS HERE, usually at 84th index
}
out_msg->hdr.checksum = chkSum;
尝试使用gdb调试器查看sgf_pkt数组是否超出范围,但并非超出范围。总的来说,我试图了解是否有任何内存对齐问题正在发生,我应该意识到。
谢谢