memcpy() 在一个类中被调用以复制到另一个类变量中后会引发异常
memcpy() throws exception after it gets called in a class to copy in another class variable.
我有两个类,一个是JPEG_Server,另一个是JPEG_Client。在类JPEG_Server我有以下声明:
class JPEG_Server
{
public:
unsigned char recv_buf[6];
};
在JPEG_Client类中,我尝试在其发送函数中使用memcpy函数将*buf的内容复制到recv_buf中。
void JPEG_Client::send_data(char *buf, int len) //buf is coming from another class
{
memcpy(&JPEG_Server->recv_buf[0], &buf, len)
}
但它会抛出异常并进入其 .asm。
例外是这样的:
Exception thrown at 0x00C85579 in JPEG_Client.exe: 0xC0000005: Access violation writing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.
谁能帮助我或评论使用这样的函数有什么问题?
如果要使用memcpy
,则需要堆上有足够的内存(目标 - recv_buf(。
您的recv_buf
点是NULL
,这意味着它被初始化为NULL
。在堆上为他分配足够的内存:
void JPEG_Client::send_data(char *buf, int len) //buf is coming from another class
{
JPEG_Server->recv_buf = new char[len]
memcpy(&JPEG_Server->recv_buf[0], &buf, len)
}
相关文章:
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将父类的子类的数据复制到具有相同父类的另一个类
- C++ |DLL / EXE - 如何从导出的类调用另一个类方法?
- 如何使模板化类成为另一个模板化类的好友
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针
- 重构模板类,该类将其嵌套类用作另一个类的模板参数
- C++:成员不可访问-使用友元函数允许一个类修改另一个类的成员数据
- 当类是另一个模板的模板参数时,可以使用类模板参数推断吗?
- Vulkan-hpp 是reinterpret_casting非标准布局类到另一个类.这合法吗?
- 如何将私有整数从A类传递到C 类B类的另一个INT变量
- 为什么我不能在类中另一个函数的声明中使用静态 constexpr 的结果?
- 转换运算符从一个模板类到另一个相关的模板类
- 从模板类的另一个实例创建模板类的实例时省略模板参数
- 如何知道模板类中另一个模板类的类型
- 根据类中另一个对象 C++ 的值对类中的二维对象进行排序并赋予值
- 如何在C++中使用一个类中另一个类的私有变量的值
- 我不知道如何将变量从一个类中的方法传递和检索到另一个类的另一个方法
- 使用模板类作为另一个类的变量
- 从一个类到另一个类的成员变量