gpt4 book ai didi

C++ 队列不喜欢少于 8 个字符*

转载 作者:太空宇宙 更新时间:2023-11-04 14:22:02 24 4
gpt4 key购买 nike

用 C++ 编写的程序从网络套接字读取数据,然后写入本地套接字。它使用单独的线程进行“读取”。

当一条消息被读取时,它被放入一个 char * 队列中(使用来自 boost 库的互斥锁使其线程安全)。

同时检查队列是否为空,如果不是,则第一条消息从队列中弹出(再次使用互斥锁)并作为 char * 写入本地套接字。

我的问题是:当一个 4 字节的消息被推送到队列时,队列保存它没有问题,但是当再次写回消息时,它增加了消息到八个字节! "new"四个字节为零。


例子A

Message in: {4,0,0,0}
Saved to queue as; <4>, <0>, <0>, <0>
Read from queue as: <4>, <0>, <0>, <0>, <0>, <0>, <0>, <0>

示例 B

Message in: {4,0,0,0,8,0,0,0}
Saved to queue as; <4>, <0>, <0>, <0>, <8>, <0>, <0>, <0>
Read from queue as: <4>, <0>, <0>, <0>, <8>, <0>, <0>, <0>

关于这个的原因有什么想法吗?队列类只能处理最少数量的字符吗? (不会这么想,因为有“空”方法)。(这不是什么大问题,因为我从来不会用少于八个字节来说话;我只是想知道它是否会在以后的生活中出现并攻击我。)

我在网上和文档中做了一些挖掘,发现了对缓冲区的奇怪引用,但这似乎更多地与使用队列作为缓冲区有关,而不是它有一个...

其他信息;

操作系统:RedHat

集成环境:Eclipse


代码:队列

//Thread-safe call to save the message to the queue
void MessageQueue::SaveToQueue(char* Data)
{
// Lock the mutex to prevent any other threads accessing this member (released when method exits).
boost::mutex::scoped_lock l(m_Msg);

//int i = 0;
//while (i < sizeof(Data))//iLength)
//{
// unsigned int ByteVaule = Data[i];//pBuffer[i];//ByteValue = int(pBuffer[i]);//unsigned int(pBuffer[i]);
// cout << "Buffer in Queue" << i << ": " << ByteVaule << endl;
// i++;
//}

MsgQ.push(Data);
}

//Thread-safe call to get the message from the queue
char* MessageQueue::GetFromQueue()
{
// Lock the mutex to prevent any other threads accessing this member (released when method exits).
boost::mutex::scoped_lock l(m_Msg);
char* message = MsgQ.front();
MsgQ.pop();
return message;
}

//Thread-safe call to check if the queue is empty
bool MessageQueue::IsEmpty()
{
// Lock the mutex to prevent any other threads accessing this member (released when method exits).
boost::mutex::scoped_lock l(m_Msg);
return MsgQ.empty();
}

代码:经理 int iStatus = 0;

//Start class to store message queue
MessageQueue* pQueue = new MessageQueue();

// Current hard coded value for the write scoket location
// TODO: change this to reading from enviroment variable
string WritePath = "DefaultSocket";

ReadSocket* pRead = new ReadSocket();
WriteSocket* pWrite = new WriteSocket();
cout << "Creating read socket" << endl;
iStatus = pRead->CreateSocket(pQueue);
cout << "Creating write socket." << endl;
iStatus = pWrite->CreateSocket(WritePath);

//while is running, check the message container and process it as needed
while (pRead->IsRunning())
{
while (!(pQueue->IsEmpty()))
{
char* Msg = pQueue->GetFromQueue();

iStatus = pWrite->WriteToSocket(Msg);
// TODO: catch on failure
}
//sleep(1);
}
//Destroy sockets as program is closing
pWrite->~WriteSocket();
pRead->~ReadSocket();

// TODO: add exception?
//Token return
return iStatus;

为了避免使这个太长和太复杂,读写套接字与中的相同

http://www.linuxhowtos.org/C_C++/socket.htm

读取的char*使用

保存到队列中
SaveToQueue()

方法并使用

从队列中取出
GetFromQueue

方法。

最佳答案

std::queue 没有这样的限制。您所看到的一定是您的代码中出现问题的结果。 (你自己说,你从不接受少于 8 个字节?!)

编辑:代码示例中看起来奇怪的是内存管理(显式调用析构函数,而不是 deletechar* -s 的内存没有似乎被释放了 - 它是如何分配的?)。

关于C++ 队列不喜欢少于 8 个字符*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6759902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com