• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ectara

Passing and Returning Arrays of Bytes

13 posts in this topic

I have an object that maintains three arbitrary precision (AP) integer keys. The caller may supply any of the keys by passing a pointer and a length; as the AP keys are of a templated type, they may change depending on the most optimal integer size, making passing arrays of bytes through an interface the only reasonable option that is agnostic of how it will be represented inside the AP integer.

 

So, there are two constructors: One that accepts up to three pointer-length pairs, and one that accepts parameters to generate the keys.

 

The biggest question is how the keys should be made accessible to the caller. It seems irrational to make the caller guess how big the buffer would be. My goal in asking is not really to find out how it is possible (I can think of a few ways), but to ask what would be most convenient and expected as a caller, as well as what is most likely to require the least amount of transformation into the desired format after retrieving it, like writing it to a file, or putting it in a vector.

 

If you were a caller, and had to get a yet unknown number of bytes from an object, how would you prefer that this interface be designed?

0

Share this post


Link to post
Share on other sites
I would prefer the details of how the number is expressed in bytes, or limbs, or whatever to be hidden from me completely. Look at mpz_class in gmpxx for a good example.
2

Share this post


Link to post
Share on other sites

I would prefer the details of how the number is expressed in bytes, or limbs, or whatever to be hidden from me completely. Look at mpz_class in gmpxx for a good example.

That is what is happening. Okay, I'll write an example:

template <class T>
class DontNeedToKnow<T>{
    T * stuff;
    
public:
    void fromBytes(const void * bytes, unsigned length);
    void toBytes(void * bytes, unsigned length);
};

class Keys{
    DontNeedToKnow<unsigned> x, y, z;
    
public:
    void getKeyX(/* My question */);
    
    void setKeyX(const void * bytes, unsigned length);
};
Edited by Ectara
0

Share this post


Link to post
Share on other sites
No, that interface is not nearly abstract enough.

  mpz_class factorial = 1;
  for (int i = 1; i <= 1000; ++i)
    factorial *= i;
That's what I am talking about. I don't need to know how many bytes it takes to store the number, if I don't want to.
0

Share this post


Link to post
Share on other sites

No, that interface is not nearly abstract enough.
 

  mpz_class factorial = 1;
  for (int i = 1; i <= 1000; ++i)
    factorial *= i;
That's what I am talking about. I don't need to know how many bytes it takes to store the number, if I don't want to.

 

There's an interface for that, already. I'm not asking how to design an AP math class, I'm asking how to best retrieve the number from a containing object in a way that can be transmitted across the network, saved to a binary file, or entered in. Moreover, referring to my above example, I'm asking about the Keys class: how should that be designed?

 

I'm not asking a math question.

Edited by Ectara
0

Share this post


Link to post
Share on other sites

 

Some variation of this perhaps?

int getKeySize()
bool getKey(void *dest, int destLen)

Hm... So, a two-step process of getting the length, then allocating, then getting the data would be most convenient? I can do that. (This is one place where I envy scripting languages with monolithic array types)

0

Share this post


Link to post
Share on other sites

Either that or wrap the key-bytes, like std::vector<char> getKey(). You could have a constant static max-size to simplify using static arrays for the key if it's reasonably short.

1

Share this post


Link to post
Share on other sites

Another alternative if it's acceptable is to just return a constant internal pointer and size to the caller and let the caller memcpy it.

0

Share this post


Link to post
Share on other sites


Either that or wrap the key-bytes, like std::vector getKey(). You could have a constant static max-size to simplify using static arrays for the key if it's reasonably short.

It'd be convenient, but I'd like to not force the user to use a particular container, much less allocated memory yet again just to pass bytes back and forth. I suppose the first option really would be best.

Another alternative if it's acceptable is to just return a constant internal pointer and size to the caller and let the caller memcpy it.

Yeah, unfortunately, that'd break on a little-endian machine, making the bytes out of order by being grouped in small reversed-order segments.

0

Share this post


Link to post
Share on other sites

An array of data and the size of the array of data are almost always passed together. If they are passed together so frequently, they should really be wrapped so they can't *not* go together.
At the simplest:

struct ByteBuffer
{
   size_t bytes;
   Byte *data;
};

But really, arrays of byte data is used frequently enough that it should have a nice class with plenty of helper functions, proper constructors/destructors move-semantics and all that.

Write one, use an existing one, or take mine:
ByteBuffer class (header)
ByteBuffer class (source) (just replace the logging and asserts, and it'll compile fine with no dependencies)

2

Share this post


Link to post
Share on other sites

Really, that has the same advantages and disadvantages of a vector, without being templated. I get your point, though.

0

Share this post


Link to post
Share on other sites

Vectors are really nice for alot of purposes, including occasionally using them as byte buffers. But since byte buffers are a common use, it's also nice having a custom class for it with specialized functions - for example, my class provides an interface for non-owning buffers or taking ownership of existing buffers.

 

But I must've misunderstood your question if that wasn't what you were looking for. smile.png

 

After re-reading your posts, I think I get what you are saying. My answer would still be to use a small class that wraps the result, that can destroy itself when it goes out of scope.

 

getKey() would create and return the class, and the class will have a pointer to the data and know how many elements it contains. A vector would work for that, or a small custom class. You could even return a std::pair<size,pointer> but personally I dislike that - and the caller would have to manually free the pointer, which isn't ideal.

 

If the caller doesn't know the size of the buffer he needs to create, why not let the function create the buffer and return both it and the size at the same time?

1

Share this post


Link to post
Share on other sites


Vectors are really nice for alot of purposes, including occasionally using them as byte buffers. But since byte buffers are a common use, it's also nice having a custom class for it with specialized functions - for example, my class provides an interface for non-owning buffers or taking ownership of existing buffers.

Yeah, I do acknowledge that the special class has desirable features for maintaining a generic byte buffer has many benefits. Looking over the header, I like the ability to make child objects that point to a subset of the buffer, and other useful functions.

 


If the caller doesn't know the size of the buffer he needs to create, why not let the function create the buffer and return both it and the size at the same time?

The caller could find out, with a query function like outlined above. However, one of the biggest reasons is that the caller could know: if they specify the size of the keys, they know how large the buffer should be, and can pre-allocate and re-use the buffer over and over.

 

I guess querying for the size, then passing a pointer and size of a buffer is my best option. I guess my biggest priority was cutting down on allocation requirements, while requiring fewer steps for the caller, but it's already nearly as good as it will get. The discussion did give me some good ideas about another project, though.

 

Thank you all, for your time.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0