472,951 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

STL without memcpy

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;
  5. *(unsigned short*)(&Buffer[0]) = 1;  
  6. *(unsigned short*)(&Buffer[2]) = count;
  7. Buffer[4] = temp_char;
  8. Buffer[5] = temp_char2;
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;
  6. *buff_iter = 0; // High Byte
  7. buff_iter++;
  8. *buff_iter = 1; // Low Byte
  9. buff_iter++;
  11. *buff_iter = ((count >> 8) & 0xFF); // High Byte
  12. buff_iter++;
  13. *buff_iter = (count & 0xFF); // Low Byte
  14. buff_iter++;
  16. *buff_iter = temp_char;
  17. buff_iter++;
  18. *buff_iter = temp_char2;
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?


Btw... I didn't actually attempt to compile this so it may not be 100% syntactically correct.
Sep 5 '06 #1
2 3921
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();
  8.     AddChar(unsigned char data);
  9.     AddShort(unsigned short data);
  10.     AddString(string data);
  12.     // etc
  13. }
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;
  5. Buffer.AddShort(1);
  6. Buffer.AddShort(count);
  7. Buffer.AddChar(temp_char);
  8. Buffer.AddChar(temp_char2);
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;
  5. Buffer += (unsigned short)1;
  6. Buffer += count;
  7. Buffer += temp_char;
  8. Buffer += temp_char2;
I have left you to actually implement the functions/operators.
Sep 5 '06 #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

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...
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); .. .. ..
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...
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...
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;...
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...
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
by: novice | last post by:
Please explain with an example whts the DIFFERENCE between "memcpy" and "memmove"
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...
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.