How to 'insert' a value into a memblock...

Started by
14 comments, last by ZealousEngine 16 years, 11 months ago
For next time, this approach can be described more generally as binary serialisation (rather than memblocks). In general it is considered to be reasonably fragile, because you need to manually record the layout of the data you are using (once for reads and once for writes).

Also you should also be aware that Zahlman's code has some gotchas.

Firstly you can't add any type to a buffer, only Plain Old Data (POD) types, that means no structs or classes with pointers, strings, etc.

As it stands you'll also need some way of getting data back out of the buffer at the other end - which means keeping track of some kind of index/iterator/pointer into the container. From experience, it's really easy to make a mistake keeping track of that index.
Advertisement
Ok I think I got the hang of this.. so to convert some area of the buffer back into a float, the syntax is...

Quote:
myFloat = *reinterpret_cast<float*>(&buffer[0]);


So I guess with this method, I wont be using void pointers at all? The following 'message system' as described in this article should be exactly the same with char* instead of void* right?

(its at the bottom of the page)
http://www.gamedev.net/reference/articles/article1930.asp

And nobody knows of any better way to do such a message system (with variable type messages) than they way they suggested in that article?
When manually allocating memory for basic types, remember the alignment.

While it may work on one processor, it will mysteriously crash on another, if you attempt to use a non-aligned address. I learned this with serialization library.

Which means, for each block you allocate, you should pad it with up to 15 bytes, then make sure the pointer from which you start allocating has address divisible by 16. Could be even more than 16 bytes (not sure right now if 128 bits is largest alignment)

A better way would be to simply have one allocators per type.

Or simply use one of boost pools.
Huh? Dont know much about boost pools.. And that sucks about it not working on different machines..

Do you see any other way to implement a sort of 'dynamic argument passing' like they do in the message system in that article?

http://www.gamedev.net/reference/articles/article1930.asp
Quote:Original post by ZealousEngine
Can you really use [] on a void pointer?


Nope you can't. Cannot do arithmetics with void* type. So, instead, should use a char buffer.
Hmm I read a little on boost pools, that seems to be for when youre constantly allocating/dealocating memory. Im talking about declaring a big chunk of memory once, and reusing it over and over (like a blackboard). Do I really need to worry about padding things in a case like that? I dont understand why things would work differently on different machines..

And also, why can I not copy a class or struct to this area of memory? Its memory isnt it?

Again, ill admit, I would rather NOT have to do things this way, but I just cant for the life of me see any other (fast) way to pass variable argument types.

This topic is closed to new replies.

Advertisement