下面是我的結構,我暴露給用戶使用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 *並且使用它未初始化導致*未定義的行爲*。您需要明確初始化結構變量或面對未定義行爲的後果。 –