我使用的是Visual Studio 2015,并且在此环境中只有几个月的经验。我有很多关于C ++和C的经验。
当运行下面的代码,并通过调试器的监视窗口检查tmp类的成员时,我可以在char列和unsigned char类型成员的值列中看到一些垃圾。
当这个现象发生时,我附上了观察窗的屏幕截图。
示例:
clock_class 1 '\x1' unsigned char
priority2 248 '・' unsigned char
log_sync_interval -3 '' char
log_announce_interval 0 '\0' char
为什么会这样? 这是Visual Studio的规范还是我的代码有问题? 据我所知,C ++将char类型处理为8bit变量, 因此,我想知道为什么会这样。
-------------------------------------------------------
Code:
#include "stdafx.h"
#include "stdint.h"
struct test_class{
public:
uint16_t descriptor_index;
uint16_t localized_description;
uint64_t mac_address;
uint16_t interface_flags;
uint64_t clock_identity;
uint8_t priority1;
uint8_t clock_class;
uint16_t offset_scaled_log_variance;
uint8_t clock_accuracy;
uint8_t priority2;
uint8_t domain_number;
int8_t log_sync_interval;
int8_t log_announce_interval;
int8_t log_pdelay_interval;
uint16_t port_number;
};
int main()
{
test_class * tmp = new test_class;
tmp->descriptor_index = 0;
tmp->localized_description = 65535;
tmp->mac_address = 0x22970405b7;
tmp->interface_flags = 0x7;
tmp->clock_identity = 0x2297fffe0405b7;
tmp->priority1 = 0;
tmp->clock_class = 1;
tmp->offset_scaled_log_variance = 17258;
tmp->clock_accuracy = 32;
tmp->priority2 = 248;
tmp->domain_number = 0;
tmp->log_sync_interval = -3;
tmp->log_announce_interval = 0;
tmp->log_pdelay_interval = 0;
tmp->port_number = 1;
<- Break at here and watch members of tmp
delete tmp;
return 0;
}
-------------------------------------------------------
答案 0 :(得分:1)
uint8_t是unsigned char类型的别名,它可以(通常)存储0到255之间的值。这就是调试器在单引号之间显示char值的原因。或者更确切地说,尝试显示此值(数字)对应的字符。并非所有值都可以打印为字符,并且将显示为垃圾:
for (uint8_t i = 0; i < 255; i++)
{
std::cout << uint8_t(i);
}