9999999999
or better illustrated as 9,999,999,999
is not in the range of an int
, to which dyn
is pointing to, don´t matter how much memory malloc
has allocated:
int *dyn = malloc(16); // `dyn` points to an `int` object, don´t matter
// if malloc allocates 16 bytes.
*dyn = 9999999999; // Attempt to assign an out-of-range value to an `int` object.
An object of type int
shall be allocated by 4 Bytes in memory by the most modern systems.
4 Bytes can hold a maximum of 2^(8*4) = 2^32 = 4,294,967,296 values.
Now you have the type of int
which is equivalent to the type of signed int
.
signed int
can store positive and negative numbers, but since it can store positive and negative numbers it has quite a different range.
signed int
has the range of -2,147,483,648 to 2,147,483,647 and so is the range of int
.
So you can not hold 9,999,999,999 in an int
object because the maximum value an int
object can store is the value of 2,147,483,647.
If you want to store the value of 9,999,999,999 or 9999999999
in an object, use f.e. long long int
, but not long int
since long int
can hold the same range of an int
and an unsigned int
:
long long int *dyn = malloc(16); // `dyn` points to an `long long int` object.
*dyn = 9999999999; // Fine, because 9999999999 is in the range of an `long long int` object.