c++队列不喜欢少于8个char*s
C++ queue doesn’t like less than 8 char*s
用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个字节?!)
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,这样我就不用再问这个问题了!
相关文章:
- 如何在C++中比较两个char数组
- 如何更好地检查两个 char 变量是否在一组值中?
- 在 char* 数组中添加两个索引
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- C++函数返回两个 char 数组的相同索引元素
- 当对字符变量使用toupper()时,所述char变量输出多个字符.我该如何防止这种情况发生
- 提取或(甚至更好)更改 char* 字符串的第一个符号
- QT中不允许使用一行多个转换,例如qString到char*
- C :如何使用char / string 2D数组接受用户的1个以上的源性
- 有没有办法比较用 int 初始化的 char 类型的数组的 2 个元素
- *char数组到字符串(两次运行代码和两个不同的结果)
- 为什么Xor-ing两个“ char”阵列显示垃圾位
- 如何让我的 strchr 函数同时将'const char'数组和'char'数组作为第一个参数?
- 如何在C/ C 中比较两个char的两个2D阵列
- 如何将多个不同的字符串分配给 char* 数组
- 使用"const char*"和"char*"参数连接两个第三方模块
- 如何填充项目为 8 个字符的集合?(std::set<char [8]>)
- 在同一分配语句中进行const char*的多个字符串文字
- C++ 如何将 char[] 中的多个字节转换为一个整数
- char*newx=p+strlen(str1);这将如何执行,因为strstr将返回指向另一个字符串的第一个匹配字符的