It’s known that calloc is different than malloc in that it initializes the memory allocated. With calloc, the memory is set to zero. With malloc, the memory is not...
  • May 18, 2022
  • 0 Comments
I want to know how malloc and free work. int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes** cout << p; free(p);...
  • May 16, 2022
  • 0 Comments
What is the difference between doing: ptr = malloc (MAXELEMS * sizeof(char *)); or: ptr = calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc over...
  • April 19, 2022
  • 0 Comments
In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this: int *sieve = malloc(sizeof(*sieve) * length); rather...
  • April 10, 2022
  • 0 Comments
TL;DR int *sieve = (int *) malloc(sizeof(int) * length); Has two problems in result of malloc. The cast and that you’re using the type instead of variable as argument...
  • March 3, 2022
  • 0 Comments