C++ 使用其中的字符数组字段数设计结构
我的程序有以下结构C++ 使用其中的字符数组字段数设计结构,c++,C++,我的程序有以下结构 #define WIFI_DEVICE_NAME 100 #define WIFI_SERIAL_NO 13 #define WIFI_PROD_NAME 7 typedef struct WiFiDeviceInfo { char name[WIFI_DEVICE_NAME]; char fullname [WIFI_DEVICE_NAME]; char productn
#define WIFI_DEVICE_NAME 100
#define WIFI_SERIAL_NO 13
#define WIFI_PROD_NAME 7
typedef struct WiFiDeviceInfo
{
char name[WIFI_DEVICE_NAME];
char fullname [WIFI_DEVICE_NAME];
char productname[WIFI_PROD_NAME];
char serialnumber[WIFI_SERIAL_NO];
};
这个结构在不同的地方使用。有时,某些字段可能会保持为空。因此,当使用strcpy_s()进行复制时,它会失败。因此,在执行任何复制操作之前,我尝试了strlen()
我修改了结构并提出了以下设计
typedef struct WiFiDeviceInfo
{
char name[WIFI_DEVICE_NAME];
unsigned short nLenName;
char fullname [WIFI_DEVICE_NAME];
unsigned short nLenFullName;
char productname[WIFI_PROD_NAME];
unsigned short nproductname;
char serialnumber[WIFI_SERIAL_NO];
};
我不能在这里使用STL,因为它的遗留代码不使用STL。
有没有更好的方法来设计这个结构。这个修改很好。由于您现在使用成员变量存储字符串的实际长度,我建议您在运行时使用指针来char和分配所需的内存。
从记忆的角度来看,这将是有用的。我是说你应该这么做
typedef struct WiFiDeviceInfo
{
char* name;
unsigned short nLenName;
char* fullname;
unsigned short nLenFullName;
char* productname;
unsigned short nproductname;
char* serialnumber; // Convert the serialnumber to a pointer as well
unsigned short nserialnumber;
};
向结构添加构造函数和析构函数以处理内存,如设置为NULL、删除指针等。此修改很好。由于您现在使用成员变量存储字符串的实际长度,我建议您在运行时使用指针来char和分配所需的内存。
从记忆的角度来看,这将是有用的。我是说你应该这么做
typedef struct WiFiDeviceInfo
{
char* name;
unsigned short nLenName;
char* fullname;
unsigned short nLenFullName;
char* productname;
unsigned short nproductname;
char* serialnumber; // Convert the serialnumber to a pointer as well
unsigned short nserialnumber;
};
向结构添加构造函数和析构函数以处理内存,如设置为NULL、删除指针等。如果要重构结构,也可以使用代码重构。为什么不能使用STL?我不认为“其他代码不使用它”是一个足够好的理由。@DeadMG我不需要问这个问题,这里是否可以使用STL。如果你重构结构,你也可以重构使用它的代码。你为什么不能使用STL呢?我不认为“剩下的代码没有使用它”是一个足够好的理由。@DeadMG如果这里可以使用STL,我不需要问这个问题。