473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

queue<char> and memcpy()

Is using memcpy() with queue<char> safe?

------ C++ code ------
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int main()
{
queue<char> qch;
char ach[100];

qch.push ('h');
qch.push ('e');
qch.push ('l');
qch.push ('l');
qch.push ('o');
qch.push (' ');
qch.push ('w');
qch.push ('o');
qch.push ('r');
qch.push ('l');
qch.push ('d');

memset (ach, 0, sizeof(ach));

memcpy (ach, &qch.front(), 5); // Is this safe?
cout << ach << endl;

qch.pop();
qch.pop();
qch.pop();
qch.pop();
qch.pop();
qch.pop();

memcpy (ach, &qch.front(), 5);
cout << ach << endl;

return 0;
}

----------------------

--- Output ---

hello
world

--------------
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mar 17 '06 #1
3 6788
Alex Vinokur wrote:
Is using memcpy() with queue<char> safe?


No: the default container underlying a 'std::queue<T>' is 'std::deque<T>'
which is not contiguous at all. Well, it may be contiguous if all
elements fit into on block but even this is not guaranteed as objects
are not rearranged when removing the from the front.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 17 '06 #2

Dietmar Kuehl wrote:
Alex Vinokur wrote:
Is using memcpy() with queue<char> safe?


No: the default container underlying a 'std::queue<T>' is 'std::deque<T>'
which is not contiguous at all. Well, it may be contiguous if all
elements fit into on block but even this is not guaranteed as objects
are not rearranged when removing the from the front.

[snip]

So, how to copy data chunks from queue<char> into char[] ?
One-to-one?

queue<char> qch;
char ach[100];

for (int i = 0; i < 5; i++)
{
char[i] = qch.front();
qch.pop();
}

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mar 17 '06 #3

Alex Vinokur wrote:
Dietmar Kuehl wrote:
Alex Vinokur wrote:
Is using memcpy() with queue<char> safe?
No: the default container underlying a 'std::queue<T>' is 'std::deque<T>'
which is not contiguous at all. Well, it may be contiguous if all
elements fit into on block but even this is not guaranteed as objects
are not rearranged when removing the from the front.

[snip]

So, how to copy data chunks from queue<char> into char[] ?
One-to-one?


Right. But you could wrap it in a function if you'd like to. You
know... memcpy is likely to copy one-to-one as well.

/Peter

queue<char> qch;
char ach[100];

for (int i = 0; i < 5; i++)
{
char[i] = qch.front();
qch.pop();
}

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Mar 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
3
by: Yuan | last post by:
hi, I do not know if this worth a cent. we know vector<boolis bad and we should use deque<bool>. However, this guy gives me some trouble, (pls correct me if I am wrong). In vector, the mem is...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.