指针在foor循环的最后一次迭代中发生变化

时间:2018-09-12 11:06:36

标签: c++ c++11 pointers

我正在尝试用C ++实现流式ACN接收器。为此,我创建了一个套接字,用于接收来自互联网但在本地运行的测试源的数据。我收到的数据很好。但是,当我遍历根层中向量的各个字节时,我使用的指针在for循环的最后一次迭代中突然改变。

我用于检查根层中字段的代码如下。

// Locations for each field of the root layer in the packet in the raw data
char *preamble_size_p    = &this->raw[0];
char *postamble_size_p   = &this->raw[2];
char *acn_pid_p          = &this->raw[4];
char *flags_and_length_p = &this->raw[16];
char * const vector_p    = &this->raw[18];

int i;
uint8_t acn_pid_byte;

// get preamble size and check it
memcpy(&this->packet.root_layer.preamble_size, preamble_size_p, 2);
this->packet.root_layer.preamble_size = htons(this->packet.root_layer.preamble_size);
if (this->packet.root_layer.preamble_size != PREAMBLE_SIZE)
    return PACKET_ERROR_INVALID_PREAMBLE_SIZE;

// get postamble size and check it
memcpy(&this->packet.root_layer.postamble_size, postamble_size_p, 2);
this->packet.root_layer.postamble_size = htons(this->packet.root_layer.postamble_size);
if (this->packet.root_layer.postamble_size != POSTAMBLE_SIZE)
    return PACKET_ERROR_INVALID_POSTAMBLE_SIZE;

// get ACN pid and check if valid
for (i = 0; i < ACN_PID_SIZE; i++) {
    memcpy(&acn_pid_byte, acn_pid_p + i, 1);
    if (acn_pid_byte != ACN_PID[i]) {
        return PACKET_ERROR_INVALID_ACN_PID;
    }
    this->packet.root_layer.acn_pid[i] = acn_pid_byte;
}

// get flags and length
memcpy(&this->packet.root_layer.flength, flags_and_length_p, 2);
this->packet.root_layer.flength = htons(this->packet.root_layer.flength);

/* ERROR HAPPENS IN LOOP BELOW */

// get vector and check if valid
uint32_t vector_bytes;
for (int k = 0; k < 4; k++) {
    memcpy(&vector_bytes + k, vector_p + k, 1);
}

如您所见,我将向量指针设为常数。它曾经像其他指针一样是常规指针。即使它是一个常数,它仍然会改变值。

raw只是char *raw,它在类的构造函数中分配,并且包含我的程序从源接收的字节。

char *raw;  
int size;

Packet(char *raw, int size) {
    this->raw = raw;
    this->size = size;
}

用于接收数据的代码。

char buffer[MAXLINE] = {0}; // MAXLINE is 1024
n = recvfrom(sock_fd, (char *)buffer, MAXLINE, MSG_WAITALL, ( struct sockaddr *) &cli_addr, &len);
Packet packet = Packet(buffer, n);

packet.process()被调用后立即开始运行顶部代码块中的代码。

我尝试在gdb中设置观察点,这就是结果。

Breakpoint 1, Packet::processRootLayer (this=0x7fffffffd5e0) at server.cpp:45
45          memcpy(&this->packet.root_layer.preamble_size, preamble_size_p, 2);
(gdb) watch vector_p
Hardware watchpoint 2: vector_p
(gdb) c
Continuing.

Hardware watchpoint 2: vector_p

Old value = 0x7fffffffdcc2 ""
New value = 0x7fffffffdc00 ""
Packet::processRootLayer (this=0x7fffffffd5e0) at server.cpp:71
71          for (int k = 0; k < 4; k++) {
(gdb) 

任何帮助将不胜感激,如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:3)

您的vector_bytes变量的类型为uint32_t,因此&vector_bytes + k不会将k字节添加到&vector_bytes中,而是会添加k*sizeof(uint32_t)个字节。结果,您的memcpy会覆盖程序中的其他变量。

您可以像这样添加演员

uint32_t vector_bytes;
for (int k = 0; k < 4; k++) {
    memcpy((char*)&vector_bytes + k, vector_p + k, 1);
}

或者您可以不做循环。

uint32_t vector_bytes;
memcpy(&vector_bytes, vector_p, 4);

简单得多。

相关问题