Writing two values after one index

Apr 14, 2011 at 10:09pm
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:

1
1
2
int jellyBlob[10];
jellyBlob[3] = 0x80003000;


2
1
2
int jellyBlob[10];
(*jellyBlob+3) = 0x80003000;
Last edited on Apr 14, 2011 at 10:16pm
Apr 14, 2011 at 10:15pm
Maybe this?
1
2
int jellyGlop[10];
*((__int64 *)(jellyGlop + 3)) = /*Some value*/;

My compiler supports the __int64 type and it works for me...
Last edited on Apr 14, 2011 at 10:15pm
Apr 14, 2011 at 10:31pm
What if the size is known but varies?

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.
Apr 14, 2011 at 10:37pm
benjelly asked:
What if the size is known but varies?

Then pick one: http://www.cplusplus.com/reference/stl/
I suggest "std::vector" because they are flexable and have a lot of cool member functions.
Apr 14, 2011 at 10:57pm
Apparently I need to be more clear. Size as in the byte-length of the data being inserted into the memory.
Apr 14, 2011 at 11:14pm
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.
Apr 14, 2011 at 11:26pm
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.
Last edited on Apr 14, 2011 at 11:28pm
Apr 14, 2011 at 11:34pm
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.

There is nothing more about the data to change.
Apr 14, 2011 at 11:45pm
Pointer of bytes.
Apr 14, 2011 at 11:51pm
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.
Apr 14, 2011 at 11:58pm
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.
Apr 15, 2011 at 12:04am
One bit is the smallest unit you can address in C++: http://www.cplusplus.com/reference/stl/bitset/

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.
Apr 15, 2011 at 11:46am
So you just want to perform a memcpy?
http://www.cplusplus.com/reference/clibrary/cstring/memcpy/
Apr 15, 2011 at 12:04pm
Apr 15, 2011 at 3:52pm
@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++".
Apr 15, 2011 at 4:10pm
benjelly wrote:
*((void**)(data+position)) = value;

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...

I'd stick to the C library functions.
Last edited on Apr 15, 2011 at 4:10pm
Apr 15, 2011 at 5:13pm
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.
Apr 15, 2011 at 5:16pm
Oh, I see. Thank you.

Cheers.
Topic archived. No new replies allowed.