in Technology by
Is it better to use malloc () or calloc ()?

1 Answer

0 votes
by

The calloc function initialize the allocated memory with 0 but malloc don’t. So the memory which is allocated by the malloc has the garbage data. In another word you can say that calloc is equal to the combination of malloc and memeset.

See the below expression,

ptr = calloc(nmember, size); //is essentially equivalent to
ptr = malloc(nmember * size);
memset(ptr, 0, (nmember * size));

 

Note: If you don’t want to initialize the allocated memory with zero, It would be better to use malloc over calloc.

Related questions

0 votes
    What is the difference between malloc and calloc?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    Distinguish between malloc() & calloc() memory allocation?...
asked Jan 17, 2021 in Technology by JackTerrance
+1 vote
    Distinguish between malloc() & calloc() memory allocation....
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    So the scenario is the following: I have multiple instances of a web service that writes a blob of data to ... other unknown issues? Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    Is it better to pay back debts or save money first?...
asked Oct 10, 2020 in Credit by Editorial Staff
0 votes
    What is the return value of malloc (0)?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What is the return value of malloc (0)?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    I am observing the following behavior in my test program: I am doing malloc() for 1 MB and then ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Python or R – Which is better for text analytics?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    Is our society better than it would have been without the computer revolution? Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    Can anyone tell me how Python is better for Data Science compared to R? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    P2P networks are better for small networks __________ true or false Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Which of the following refers to stealing one's idea or invention of others and use it for their own benefits? Piracy Plagiarism Intellectual property rights All of the above...
asked Mar 4, 2021 in Technology by JackTerrance
+1 vote
    Is it possible to create a custom domain name, or use your organisation's domain name such as eduforum.in, in Azure Active Directory?...
asked Oct 20, 2020 in Technology by JackTerrance
...