1

I'm developing a Python module in C that parses a very efficient protocol which uses dynamic integer sizes. Integers sent using this protocol can range in size from the equivalent of C's 'short' to a 'long long.'

The protocol has a byte that specifies the type of variable being sent (from short to long long), but I'm not sure how to deal with this in code. Right now, I'm setting up a void pointer and allocating memory in the size of the value being sent -- then using atoi, atol, and atoll to set that pointer. The problem is, I need to be able to access that value, and am unable to do so without it being cast later.

What are some good ways to handle this issue?

0

1 Answer 1

4

Either always store it in a long long locally, or put it in a struct composed of a flag for the size and a union of all the possible types.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm a little confused about how to use a struct/union for that.. Have a quick example?
Then you'd just always cast the union as a long long when you're accessing it in code? Or do you have to switch off of that flag and cast accordingly?
You switch off .flag and read the appropriate member (.ubyte, .ulong, etc.).
Thanks a lot! Gonna stick with using long longs locally.
The first part of this answer, always using a long long, is going to be easier for you without any loss, compared to the second part of using a union.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.