473,403 Members | 2,354 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,403 software developers and data experts.

STL without memcpy

2
I wanted to write a quick test program to send a message with variable length fields in the message. Typically I would use some sort of dynamically allocated c-like buffer and just memcpy the elements of different sized into their proper positions. For example:

Expand|Select|Wrap|Line Numbers
  1. char Buffer[8192];
  2. unsigned short count = 512;
  3. char temp_char = 3, temp_char2 = 255;
  4.  
  5. *(unsigned short*)(&Buffer[0]) = 1;  
  6. *(unsigned short*)(&Buffer[2]) = count;
  7. Buffer[4] = temp_char;
  8. Buffer[5] = temp_char2;
  9.  
I would like to use the STL to do this a little more elegantly and c++-ish, but I don't know how to go about doing it the best way. I could do something like this:
Expand|Select|Wrap|Line Numbers
  1. deque<char> Buffer(8192);
  2. deque<char>::iterator buff_iter = Buffer.begin();
  3. unsigned short count = 512;
  4. char temp_char = 3, temp_char2 = 255;
  5.  
  6. *buff_iter = 0; // High Byte
  7. buff_iter++;
  8. *buff_iter = 1; // Low Byte
  9. buff_iter++;
  10.  
  11. *buff_iter = ((count >> 8) & 0xFF); // High Byte
  12. buff_iter++;
  13. *buff_iter = (count & 0xFF); // Low Byte
  14. buff_iter++;
  15.  
  16. *buff_iter = temp_char;
  17. buff_iter++;
  18. *buff_iter = temp_char2;
  19.  
However, that seems worse to me. Could someone give me some ideas on a good way to handle packing data words of different sizes into an STL container?

Thanks!

Btw... I didn't actually attempt to compile this so it may not be 100% syntactically correct.
Sep 5 '06 #1
2 3936
Banfa
9,065 Expert Mod 8TB
Rather than just instanciating a variable of type deque(char) you could create a subclass of that type and add you own methods to insert data

Expand|Select|Wrap|Line Numbers
  1. class message : public deque<char>
  2. {
  3. public:
  4.     message();
  5.     message(int defaultBufferSize);
  6.     ~message();
  7.  
  8.     AddChar(unsigned char data);
  9.     AddShort(unsigned short data);
  10.     AddString(string data);
  11.  
  12.     // etc
  13. }
  14.  
Then your main cod becomes

Expand|Select|Wrap|Line Numbers
  1. message Buffer(8192);
  2. unsigned short count = 512;
  3. char temp_char = 3, temp_char2 = 255;
  4.  
  5. Buffer.AddShort(1);
  6. Buffer.AddShort(count);
  7. Buffer.AddChar(temp_char);
  8. Buffer.AddChar(temp_char2);
  9.  
If you wished rather than add methods you could override the += operator giving

Expand|Select|Wrap|Line Numbers
  1. message Buffer(8192);
  2. unsigned short count = 512;
  3. char temp_char = 3, temp_char2 = 255;
  4.  
  5. Buffer += (unsigned short)1;
  6. Buffer += count;
  7. Buffer += temp_char;
  8. Buffer += temp_char2;
  9.  
I have left you to actually implement the functions/operators.
Sep 5 '06 #2
Esam
2
Hey that's a good idea, thanks!
Sep 6 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
16
by: Delali Dzirasa | last post by:
I would have a number packed with its hex representation of the integer below is some sample code of what is being done. int value = 20; //in hex it is 0x14 AddData (value); .. .. ..
6
by: Samee Zahur | last post by:
Hi all, I'm a little confused - my guess is memcpy is no longer (or perhaps never was) a standard c++ function, since it has very little type check into it - and can potentially create havoc for...
5
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a...
35
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src;...
16
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the following prototype of memcpy(3): #include...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
by: novice | last post by:
Please explain with an example whts the DIFFERENCE between "memcpy" and "memmove"
18
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.