473,666 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6832
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.co m> <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
4764
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 second 4. The total message length is therefore 4 + message length. A number of messages work fine/as expected but there are consistant errors occuring. After a period
3
3042
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 guaranteed to be a continous block. however, for deque, NO!! So when we try to get an array out of a vector, we just set the pointer to be the &vec, done.
5
13348
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 http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.