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

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -