The library function calloc has the following declaration:
void *calloc(size_t nmemb, size_t size);
According to the library documentation, “The calloc function allocates memory for an array of nmemb elements of size bytes each. The memory is set to zero. If nmemb or size is zero, then calloc returns NULL.”
Write an implementation of calloc that performs the allocation by a call to malloc and sets the memory to zero via memset. Your code should not have any vulnerabilities due to arithmetic overflow, and it should work correctly regardless of the number of bits used to represent data of type size_t.
As a reference, functions malloc and memset have the following declarations:
void *malloc(size_t size);
void *memset (void *s, int c, size_t n);
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Additional Engineering Textbook Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Modern Database Management (12th Edition)
Database Concepts (8th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Artificial Intelligence: A Modern Approach
- write in html and scriptarrow_forwardPROGRAMMING LANGUAGE: C++ Please test the code first before uploading here in bartleby. The expected outcome for each of the test case is also given in the screenshot. Follow accordingly. Thanksarrow_forwardWrite a function in C++ which (normally) allocates new space for a double array. It should alter its pointer argument to point to the newly allocated space. The size of the array should be a second argument. If this argument is not a valid array size, allocate a single double instead of an array. An example call might be: double * p; allocate(p, 25); // p now points to an array of 25 doubles -- if successfularrow_forward
- A main() function is given below. Based on this function, your task is to write complete coderequired to run the main() function successfully. class MyArray{private:float* array;int size;Declare a static variable to count the number of objectspublic:Implement a static function to return total count of objects};int main(){int size;float* ptr;cout << "Enter the size of your array" << endl;cin >> size;ptr = new float[size];cout << "Enter elements of array" << endl;for (int i = 0; i < size; i++){cin >> ptr[i];}MyArray a1(ptr, size);MyArray a2 = a1;int len = a1.getSize();float* res;res = new float[len]; res = a1.getArray();for (int i = 0; i < len; i++){cout << res[i];}// incase of array, compare or add or perform any operation index wiseif (a1 == a2) // overload == operator to compare two objects{cout << "Both arrays are equal" << endl;}else{cout << "Both arrays are not equal" << endl;}MyArray a3;a3 = a1 + a2; //…arrow_forwardIn java Develop a function that accepts an array and returns true if the array contains any duplicate values or false if none of the values are repeated. Develop a function that returns true if the elements are in decreasing order and false otherwise. A “peak” is a value in an array that is preceded and followed by a strictly lower value. For example, in the array {2, 12, 9, 8, 5, 7, 3, 9} the values 12 and 7 are peaks. Develop a function that returns the number of peaks in an array of integers. Note that the first element does not have a preceding element and the last element is not followed by anything, so neither the first nor last elements can be peaks. Develop a function that finds the starting index of the longest subsequence of values that is strictly increasing. For example, given the array {12, 3, 7, 5, 9, 8, 1, 4, 6}, the function would return 6, since the subsequence {1, 4, 6} is the longest that is strictly increasing. Develop a function that takes a string…arrow_forwardLanguage is C++arrow_forward
- In C++ Create an array of head pointers, statically allocated of size 101 of that type: Now, create a pointer that can point to the first element: Show how to dynamically allocate an array of 101 elements of head pointers: Write the code to initialize each element to NULL: Rewrite the code to initialize each element to NULL using pointer arithmetic:arrow_forwardin c++ Write a function named “countTheSameAsLast” that accepts an array of pointers to Name objects and its array size. It will go through the array and return how many elements (int) in the array are the same as the last element and whether the first and the last elements are the same or not (bool). Please note that the objects pointed by elements in the array can be either Word or Item objects. Please show how you test with the list of mixed objects.arrow_forwardin c++ fast pleasearrow_forward
- In C programming: Write a main() function using the following requirements:• Define a SIZE constant (the value is irrelevant, but for testing, you may want to keep it small enough – no bigger than 5)• Create an array of course pointers using SIZE• Dynamically allocate each element of the array• Call inputAllCourses()• Call printAllCourses().arrow_forwardWrite a complete C++ program which consists of two parts. Part 1. Write a function which counts the number of y's in an array of chars. It takes two parameters: an array of chars called array, and the number of elements in the array called size. It returns the number of elements in the array that are equal to 'y. You may assume that the parameters passed to the function are valid. Your function must have the following signature: int countYs (const char array[], int size) Part 2. Write a simple main() function which demonstrates your function by doing the following: - create an array of chars and give its elements some reasonable values. - call your function to count the number of y's in the array, and output the returned valuearrow_forwardIn C++, define a “Conflict” function that accepts an array of Course object pointers. It will return two numbers:- the count of courses with conflicting days. If there is more than one course scheduled in the same day, it is considered a conflict. It will return if there is no conflict.- which day of the week has the most conflict. It will return 0 if there is none. Show how this function is being called and returning proper values. you may want to define a local integer array containing the count for each day of the week with the initial value of 0.Whenever you have a Course object with a specific day, you can increment that count for that corresponding index in the schedule array.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education