Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Passing and Returning Arrays of Bytes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Ectara   Crossbones+   -  Reputation: 3019

Like
0Likes
Like

Posted 14 April 2014 - 04:22 PM

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?



Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13670

Like
2Likes
Like

Posted 14 April 2014 - 07:36 PM

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.

#3 Ectara   Crossbones+   -  Reputation: 3019

Like
0Likes
Like

Posted 15 April 2014 - 06:56 AM

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, 15 April 2014 - 06:56 AM.


#4 Álvaro   Crossbones+   -  Reputation: 13670

Like
0Likes
Like

Posted 15 April 2014 - 07:42 AM

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.

#5 Ectara   Crossbones+   -  Reputation: 3019

Like
0Likes
Like

Posted 15 April 2014 - 07:48 AM

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, 15 April 2014 - 07:59 AM.


#6 Erik Rufelt   Crossbones+   -  Reputation: 3532

Like
1Likes
Like

Posted 15 April 2014 - 08:08 AM

Some variation of this perhaps?

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


#7 Ectara   Crossbones+   -  Reputation: 3019

Like
0Likes
Like

Posted 15 April 2014 - 08:56 AM

 

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)



#8 Erik Rufelt   Crossbones+   -  Reputation: 3532

Like
1Likes
Like

Posted 15 April 2014 - 09:04 AM

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.



#9 Erik Rufelt   Crossbones+   -  Reputation: 3532

Like
0Likes
Like

Posted 15 April 2014 - 09:08 AM

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.



#10 Ectara   Crossbones+   -  Reputation: 3019

Like
0Likes
Like

Posted 15 April 2014 - 09:28 AM


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.



#11 Servant of the Lord   Crossbones+   -  Reputation: 20377

Like
2Likes
Like

Posted 15 April 2014 - 10:54 AM

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)


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#12 Ectara   Crossbones+   -  Reputation: 3019

Like
0Likes
Like

Posted 15 April 2014 - 11:46 AM

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



#13 Servant of the Lord   Crossbones+   -  Reputation: 20377

Like
1Likes
Like

Posted 15 April 2014 - 04:16 PM

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?


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#14 Ectara   Crossbones+   -  Reputation: 3019

Like
0Likes
Like

Posted 15 April 2014 - 05:54 PM


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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS