I'm working with a data buffer. I want to write in an array, e.g [4] and [5], in the same statement. How can I do this? I thought of a couple ways which might work:
My functions require you to cast your data into "Bytes" (typedef unsigned char Byte) before they format the data and put it at the end of the dynamic memory (implemented like a vector of Bytes). I'm not using templates.
Do you mean for each element of the array? As in the physical amount of space that each instance of the object occupies in memeory changes from one entry to the next? This isn't managable, it might be possible but it involves the kind of micro management that C++ was designed to avoid.
Just set the size of your object to the maximium possible and don't worry about "Wasting Memory". Memory is like paint, or ink in a pen is to an artist. You aren't supposed to worry about these things while you're creating, they should only be of concern to you if you run out.
No, I don't mean that. The array consists of bytes. Reread my post you half-quoted, and hopefully you will understand. The array (or buffer) is kind of like a networking packet.
I'm trying to understand here. Are you changing the size of the array? Or the size of the variable? You are type casting, you don't need templates so including that you aren't using them is kind of redundent.
If you are typecasting then the size of the object, as in the amount of memory it occupies, would not change from one instance of the object to another. They would be the size of an unsigned char by the time they get to your array, always.
If you are changing the size of your array then any one of the STL containers would be suited for the job. Some better then others, it has been my experiance that std::vector provides the most functionality out of all of them.
Rather then drag this out any further I'm simply going to step down. My understanding of what you are trying to do seems to be too far removed from the reality of your problem. I'm sorry that I can't be more help to you.
Can't you connect the dots, or are you an idiot? You can cast anything into bytes, because one byte is the smallest unit in C. An array = a pointer. If you take a pointer of bytes (or an array of bytes) and want to insert it into a custom-built container, how? It shouldn't require a for loop, I should be able to just fill it in.
And to answer your question, I would use a template which you have already said you would not use. So again I am stepping down, I appoligies that I could not help you.
@L B
@m4ster r0shi
Yeah, I know those from the C library of course. But isn't there a different (better) way in C++ ? Would this work ?
*((void**)(data+position)) = value;
@Computergeek01
And as for you, do you always give annoying answers, like "it's not possible" even though I know you're wrong ? And notice that I said "one byte is the smallest unit in C" rather than "one bit is the smallest unit in C++".
Not possible. Only Chuck Norris can dereference void pointers.
I guess the C++ way would be using std::fill or std::fill_n somehow...
Though, in that case, you'd have to lie to your compiler about the template arguments
of std::fill(_n) to make it work, and this doesn't really look like a decent C++ solution...
Well...the whole way copying data works anyway is with loops...even if the loops are 'invisible' (ie copying each byte individually to copy a long), they are still there.
And who said bits were the smallest unit in C++? This is untrue...bitsets make use of longs and the various operators that allow some nifty manipulation of specific bits.