Custom byte size?
Asked Answered
S

5

12

So, you know how the primitive of type char has the size of 1 byte? How would I make a primitive with a custom size? So like instead of an in int with the size of 4 bytes I make one with size of lets say 16. Is there a way to do this? Is there a way around it?

Salesmanship answered 21/4, 2010 at 1:45 Comment(3)
What do you want to accomplish with this? Is it to allow bigger numbers in your computations or do you have another plans for your 16 byte large integers?Tenderize
This is an interesting question, but a bit underspecified. And the answers don't address creating either bigger or smaller primitives. Smaller than a char: override operator& as in std::vector<bool>::iterator. Larger than a long int: use a bignum library.Recitative
@Tenderize I want to use this for math with primitives in C++ that numbers go out of the range of the standard primitives in C++.Salesmanship
F
5

Normally you'd just make a struct that represents the data in which you're interested. If it's 16 bytes of data, either it's an aggregate of a number of smaller types or you're working on a processor that has a native 16-byte integral type.

If you're trying to represent extremely large numbers, you may need to find a special library that handles arbitrarily-sized numbers.

Felicidadfelicie answered 21/4, 2010 at 1:49 Comment(0)
A
7

It depends on why you are doing this. Usually, you can't use types of less than 8 bits, because that is the addressable unit for the architecture. You can use structs, however, to define different lengths:

struct s {
  unsigned int a : 4;  // a is 4 bits
  unsigned int b : 4;  // b is 4 bits
  unsigned int c : 16; // c is 16 bits
};

However, there is no guarantee that the struct will be 24 bits long. Also, this can cause endian issues. Where you can, it's best to use system independent types, such as uint16_t, etc. You can also use bitwise operators and bit shifts to twiddle things very specifically.

Antibiosis answered 21/4, 2010 at 1:48 Comment(3)
uint16_t still has endian issues.Feil
@Billy true... I guess my point is that bitfields in structs are very messy in this regard, and that's one of their primary uses.Antibiosis
Yep. I wasn't gonna downvote you for it. But I did think it was worth mentioning.Feil
F
5

Normally you'd just make a struct that represents the data in which you're interested. If it's 16 bytes of data, either it's an aggregate of a number of smaller types or you're working on a processor that has a native 16-byte integral type.

If you're trying to represent extremely large numbers, you may need to find a special library that handles arbitrarily-sized numbers.

Felicidadfelicie answered 21/4, 2010 at 1:49 Comment(0)
T
4

In C++11, there is an excellent solution for this: std::aligned_storage.

#include <memory>
#include <type_traits>

int main()
{
    typedef typename std::aligned_storage<sizeof(int)>::type memory_type;
    memory_type i;
    reinterpret_cast<int&>(i) = 5;
    std::cout << reinterpret_cast<int&>(i) << std::endl;

    return 0;
}

It allows you to declare a block of uninitialized storage on the stack.

Tjader answered 2/7, 2017 at 6:12 Comment(2)
What's the link with the question ?Haggar
@Haggar Besides the obvious fact that allowing you to allocate custom sized storage on the stack or heap allows you to then define custom classes of a given side using the right methods? It may not behave like a primitive, but if you need a custom-sized class, this is a great tool to use (even like a custom width integer). It is much better than defining a custom strict with arbitrary data members.Tjader
I
1

If you want to make a new type, typedef it. If you want it to be 16-bytes in size, typedef a struct that has 16-bytes of member data within it. Just beware that quite often compilers will pad things on you to match your systems alignment needs. A 1 byte struct rarely remains 1 bytes without care.

Intracutaneous answered 21/4, 2010 at 2:13 Comment(0)
O
0

You could just static cast to and from std::string. I don't know enough C++ to give an example, but I think this would be pretty intuitive.

Opulence answered 23/6, 2022 at 16:38 Comment(2)
Using a cast (any sort of cast) on a Standard Library container is rarely (if ever) a workable solution to a problem. Can you explain how it would help in this case?Specious
@AdrianMole For example, you could cast an array of ints to a string and return it, and then cast the string back to an array of ints in the parent function. This casting method can bypass the "you cannot return an array" error without really changing how the code works (since casts pretty much just tell the compiler not to make a type-error).Opulence

© 2022 - 2024 — McMap. All rights reserved.