下面是我的结构,我暴露给用户使用malloc给它一些大小来填充它。 使用通过我的指针这种结构C++ /结构指针/验证成员
typedef struct ServerConfiguration {
wchar_t *IPAddress;
USHORT PortNo;
wchar_t *Title;
int repeatCount;
int timeout;
} ServerConfig;
ServerConfig *serverconfig = (ServerConfig*)malloc(sizeof(ServerConfig));
dcmServerconfig->IPAddress = L"localhost";
dcmServerconfig->Title = L"DVTK_MW_SCP";
dcmServerconfig->PortNo = 8080;
用户DOE不分配重复计数// 指向一些垃圾地址LOC //示例repeatCount = 380090700
我有具有结构的另一种结构,
typedef struct CommonParameters {
//other members;
int repeatCount
} commonParams;
我要验证SERVERCONFIG值,然后将其分配给CommonParameters如示于下
if (serverConfig->opt_repeatCount > 1) {
commonParams.repeatCount = serverConfig->repeatCount;
}
如果未由用户分配,serverConfig->repeatCount
的值为某些垃圾(380090700)。并且在我的情况下大于1。我需要验证这个serverConfig->repeatCount
是否具有有效值,然后只通过if条件
最终,我的问题是验证一个结构变量,它是一个适当值的整数。
你是否想要标记这个C++? – Bathsheba
'malloc'函数不初始化它分配的内存,它的内容是* indeterminate *并且使用它未初始化导致*未定义的行为*。您需要明确初始化结构变量或面对未定义行为的后果。 –