asked    Sophia     2018-10-22       c       127 view        5 Answers

[SOLVED] how to create a new type with custom bytes in C?

Is it possible to create a new type in C that uses the amount of bytes that I decide? I know that an int takes 4 bytes, but I need to work with very small numbers so allocating 4 bytes with malloc for every int is a bit of a waste, i was thinking of creating a new type for numbers that takes only 1 byte...if it's possible

  5 Answers  

        answered    Cliff     2018-10-22      

You can just use char instead of int

Or, you can create structure, it is the most commonly used custom data type in C. For example:

struct customStructure {
    char c;
};


        answered    Joyce     2018-10-22      

Is it possible to create a new type in C that uses the amount of bytes that I decide?

Yes, you can declare an array of char (or signed char or unsigned char) of any positive length you like, up to an implementation-dependant (but usually large) limit.

I know that an int takes 4 bytes,

You are mistaken: an int may take four bytes, but the standard does not require that. Its minimum required range can be represented with only two bytes, and some implementations indeed provide two-byte ints. That was more common historically than it is today. Also, implementations can make int larger than four bytes. That's rare as a default, but some compilers provide an option to produce that result.

but I need to work with very small numbers so allocating 4 bytes with malloc for every int is a bit of a waste, i was thinking of creating a new type for numbers that takes only 1 byte...if it's possible.

A one-byte number is a signed char or unsigned char. Technically, plain char also qualifies, but its signedness is implementation-defined, and as a matter of style, it is preferable to reserve its use to character data. Also, technically, char and its signed and unsigned variations may be larger than 8 bits, but you are unlikely ever to run into a C implementation where that is the case, and C anyway offers no smaller data type.



        answered    June     2018-10-22      

A byte type already exits, it is called char, and sizeof(char)=1, so strictly talking about datatypes, char is the smallest amount of memory you can manage though C.

However, if you are talking about bits, it doesn't mean that char type is always 8bits (it is common a byte of 10 or 16bit in DSPs). Given that, the number of bits in a char is indicated by CHAR_BIT.



        answered    Warner     2018-10-22      

Strictly speaking, an int has at least 2 bytes, according to the C standard, and the actual width of a type is machine-dependent. If you want numeric types with a certain width, I suggest you take a look at those defined in <stdint.h>.

If you really want to use a type with, say, N bytes, you could use char small_numbers[N] and get your hands dirty with some bit twiddling, but the more practical solution would be to simply use the built-in types.



        answered    Beryl     2018-10-22      

You can use below structures for bytes and bits respectively.

 typedef struct data_type_bytes {
        unsigned char data;
 } bytes;

typedef struct data_type_bits{
   unsigned int heightValidated : 1;
} bits;




Your Answer





 2018-10-22         Rex

Why assigning a function as variable is faster in Python

Today I observed an interesting behavior using Python.def f(): ls1 = [] for x in range(1000000): ls1.append(x)%timeit f()77.2 ms ± 1.83 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)And tested a second function.def f2(): ls1 = [] lsa = ls1.append for x in range(1000000): lsa(x) %timeit f2()56 ms ± 566 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)Assigning append to a variable was faster than using append inside the loop.Why the second function is faster? Because in the second function, you have the reference to the m...
 python-3.x                     2 answers                     59 view
 2018-10-22         Rock

Please clarify my mistake while doing the dry run

The following code is all correct but I have a doubt. While I am doing the dry run I find in my mind that it should show 2 as NOT a prime number. Someone please clarify how the code is running all good. num = 4if num > 1: for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number")else: print(num,"is not a prime number") 2 is a prime number, but your code will shows 2 as not a prime numberHere is the fix:num = 4if num > 2: fo...
 python                     1 answers                     55 view
 2018-10-22         Merry

Calculate the area under each peak in a graph in python

I am trying to calculate the area under each peak in a graph that I plotted with a set of x and y co-ordinates, I don't have a function for (x,y), and so I haven't been able to find an appropriate method to do the same.the co-ordinates are { [10 10] [11 1] [12 7] [14 4] [16 8] [17 5]]}And y=0 for all the unmarked x values The area below two points consists of a 'square' part and a triangular part. The trick is to calculate them both separately and add them. Do this for all points, and you get a list of areas under peaks:points = [[10, 10], [11, 1], ...
 python                     3 answers                     55 view