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 -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -