Why use sizeof(int) in a malloc call? -


i new c++ , in mpi programming. have confused code block in c++

int count; count=4; local_array=(int*)malloc(count*sizeof(int)); 

why using sizeof(int) here in mpi programming?

i see you're trying allocate 4 ints here.

if @ malloc's signature, takes number of bytes first parameter. stated here, int data type takes 4 bytes.

therefore, if want 4 ints, have typed local_array=(int*)malloc(count*4);. not remembers int actualy takes 4 bytes. that's why use sizeof find out actual size of object or type.


Comments

Popular posts from this blog

swift - Button on Table View Cell connected to local function -

dns - Dokku server hosts two sites with TLD's, both domains are landing on only one app -

c# - ajax - How to receive data both html and json from server? -