sizeof(<элемет>) Returns the size in bytes of memory for the element to be allocated.
If We work in a 32-bit system, then the following constructions do the same thing, allocate 20 bytes in memory.
void * v = malloc(20); char * c = (char *)malloc(20); int * i = (int *)malloc(5 * sizeof(int)); int ** pi = (int **)malloc(5 * sizeof(int *)); struct s{ char c; int i; long long int lli; int * pi; short int s; char s; }__attribute__((packed)); struct * s = (struct s *)malloc(5 * sizeof(struct s*)); struct * s = (struct s *)malloc(sizeof(struct s));
But to describe these expressions can be so.
void * v = malloc(20); - create a pointer to an object of any type and allocate 20 bytes
char * c = (char *)malloc(20); - create a pointer to a string and select a string by 20 characters.
int * i = (int *)malloc(5 * sizeof(int)); - create a pointer to an int type and allocate memory for 5 int elements
int ** pi = (int **)malloc(5 * sizeof(int *)); - create a user to an array of int pointers and allocate memory for 5 pointers.
struct * s = (struct s *)malloc(5 * sizeof(struct s*)); - create a pointer to an array of pointers to the structure s.
struct * s = (struct s *)malloc(sizeof(struct s)); - create a pointer to the structure s.
In programming, there is no such thing так принято , there is a standard language that can be used.
sizeofis of typesize_t, for which reason it cannot be passed toprintfwith the format specifier%d- the behavior is undefined. The correct format specifier forsize_tis%zu- AnT%zuis C99, whose support appeared in VS2013 and was completed in VS2015. In VS2015,%zuworks reddened, although it is not described at the docks. If there is no support for%zu, thenprintf("%llu\n", (unsigned long long) (5 * sizeof *arr));is betterprintf("%llu\n", (unsigned long long) (5 * sizeof *arr));- AnT September