c++队列不喜欢少于8个char*s

C++ queue doesn’t like less than 8 char*s

本文关键字:char 8个 队列 不喜欢 c++      更新时间:2023-10-16

用c++编写的程序,从网络套接字读取数据,然后写入本地套接字。它使用一个单独的线程来执行" read "。

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

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

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


示例

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>

你知道这是什么原因吗?队列类能否只处理最小数量的字符?(我不这么认为,因为有"空"方法)。(这不是一个大问题,因为我说话从不少于8个字节;我只是想知道,以防它在以后的生活中出现并袭击我。

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

其他信息;

操作系统:RedHat

Eclipse IDE:


队列

代码:

//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 status = 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

read char*使用

保存到队列中
SaveToQueue()

方法,并使用

从队列中取出
GetFromQueue

方法。

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

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

查看信息;

char*是指向char的指针,占用8字节(取决于机器?),这意味着当我尝试输出指针的内容时,它输出所有8字节的大小;我想要的四个后面跟着四个空的

要自己查看,可以使用以下代码:

/*
 * main.cpp
 *
 *  Created on: Jul 20, 2011
 *      Author: Andy
 */
#include <iostream>
using namespace std;
int main ()
{
cout << "**** Size of ****n" << endl;
char * Lemon;
char apple;
Lemon = "It's a long way to Tipperary";
int sizeOfApple = sizeof(apple);
int sizeOfLemon = sizeof(Lemon);
cout << "sizeOfApple is: " << sizeOfApple << endl;
cout << "sizeOfLemon is: " << sizeOfLemon << endl;
cout << "Lemon is: " << Lemon << endl;
return 0;
}

得到以下O/p;

sizeOfApple is: 1
sizeOfLemon is: 8
Lemon is: It's a long way to Tipperary

所以我可以使用char,这样我就不用再问这个问题了!