pointers - C malloc function's size parameter -


i reading in book malloc function in c takes number of 'chunks' of memory wish allocate parameter , determines how many bytes chunks based on cast value returned malloc to. example on system int 4 bytes:

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

would allocate 40 bytes because compiler knows ints 4 bytes.

this confuses me 2 reasons:

  1. i reading up, , size parameter number of bytes want allocate , not related sizes of types.

  2. malloc function returns address. how adjust size of memory allocated based on external cast of address returned void different type? compiler magic supposed accept?

i feel book wrong. or clarification appreciated!

here book said:

char *string; string = (char *)malloc(80); 

the 80 sets aside 80 chunks of storage. chunk size set typecast, (char *), means malloc() finding storage 80 characters of text.

yes book wrong , correct please throw away book.
also, let know of name of book can permanently put in our never recommend black list.

good read:
what best practice malloc?


Comments