memcpy() 在一个类中被调用以复制到另一个类变量中后会引发异常

memcpy() throws exception after it gets called in a class to copy in another class variable.

本文关键字:类变量 另一个 异常 复制 调用 一个 memcpy      更新时间:2023-10-16

我有两个类,一个是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)
}
相关文章: