Creating an integer variable of a certain size

I want to define an integer variable in C / C ++ so that my integer can store 10 bytes of data or maybe x bytes of data that I defined in the program. for now..! I tried

int *ptr;
ptr = (int *)malloc(10);

the code. Now, if I find sizeof ptr, it displays as 4, not 10. Why?

+3
source share
7 answers

The C and C ++ compilers implement several sizes of integers (usually 1, 2, 4, and 8 bytes {8, 16, 32, and 64 bits}), but without any auxiliary code for the preliminary arithmetic operation, you cannot arbitrary size integers numbers.

Statements you made:

int *ptr;
ptr = (int *)malloc(10);

, , . , , , (10 % sizeof(int) ) == 0), , .

, ++, , 10- (80 ) . C , .

sizeof(ptr) 4, 4- (32- ). sizeof , . , , - sizeof , . , .

+3

4 . , ptr int *. sizeof, malloc new , sizeof - , , .

+2

4 , 4 . , 10 . , :

struct VariableInteger
{
    int *ptr;
    size_t size;
};

, int ptr , , int .

+2

4. - :

typedef struct
{
    int a[10];
} big_int_t;

big_int_t x;

printf("%d\n", sizeof(x));

, int 1 , , , 20 40, .

+1

++ . ? sizeof, , , . .

0

10- . , , <limits.h>, , , .

0

, , , " ". . , , , .

Here is a link to a list of arithmetic libraries of arbitrary accuracy in several different languages, Wikipedia compliments: link .

0
source

Source: https://habr.com/ru/post/1769811/


All Articles