写入结构变量时发生strcpy访问冲突
strcpy access violation writing to a struct variable
我有一个名为record的结构,它包含键、值对:
struct Record{
char* key=new char();
TYPE value=NULL;
Record(){
key = "default";
value = 10;
}
Record(const char* key_, TYPE value_){
strcpy(key, key_);
value = value_;
}
const Record<TYPE>& operator=(const Record<TYPE>& other){
key = other.key;
value = other.value;
return *this;
}
};
此外,我有一个类"SimpleTable",它包含这些记录的数组:
class SimpleTable:public Table<TYPE>{
struct Record<TYPE> *table;
public:
当我试图将日期放入这些记录时,问题就来了。我的strcpy给了我"访问违规写入位置"。(在类构造函数中初始化Records数组的所有元素):
template <class TYPE>
bool SimpleTable<TYPE>::update(const char* key, const TYPE& value){
for (int i = 0; i < 10; i++){
if (table[i].key == ""){
strcpy(table[i].key , key); // <-------- crash right here
table[i].value = value;
}
}
return true;
}
char* key=new char();
只分配内存来容纳一个字符。
strcpy(table[i].key , key);
将导致未定义的行为,除非key
是空字符串。
使用std::string key
。如果不允许使用std::string
,则必须重新访问代码并修复与key
相关的内存问题。
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- C++尝试深度复制唯一指针时出现内存访问冲突
- C++ 中动态二维数组的访问冲突
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- 写入访问冲突异常
- 在类 12.exe 中0x7B37FF80 (ucrtbased.dll) 引发异常: 0xC0000005:访问冲突读
- 0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误
- 读取访问冲突.这0xCDCDCDCD
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- 插入数组时违反写访问冲突
- 使用 ReadProcessMemory 获取字符串值的访问冲突
- 尝试通过共享指针使用变量时读取访问冲突
- 堆栈上的 C++ 访问冲突写入异常
- 引发异常:写访问冲突. temp 为 nullptr
- 将静态字符数组中的字符分配给动态分配的字符数组 - 访问冲突
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 写入结构变量时发生strcpy访问冲突