Memory allocation for incremental garbage collection simulation in c++ -
i need simulate incremental garbage collection algorithm in c++ or java. had doubt based on this.
as input (stdin
keyboard), asked allocate memory code. syntax be:
x = alloc(128kb);
my question: ok use malloc
assignment? or there other way allocate memory? had doubt because, size can go gb assignment, using malloc
might not idea think.
first of all, if want prohibit huge memory allocation, check users' input value, i'm not sure how memory think huge memory. think don't worry that, because if memory allocation failed, malloc , calloc return null pointer.
secondly, can use 'calloc' case.
void calloc(size_t num, size_t size);
'num' mean elements' count allocation , 'size' is, of course, size of element. below codes have same result.
ar = (int *)malloc(5 * sizeof(int)); ar = (int *)calloc(5, sizeof(int));
however, if choose 'calloc', may manage more logically code, since can divide memory quantity unit , count. also, if use 'calloc', don't need use memset setting memory value zero. 'calloc' set automatically memory value zero.
i hope article can you.
Comments
Post a Comment