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&MOD MSGT/ET SA AC PKG
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
- Create a dynamic array that contains an int array. dynamic array, array inside when the existing array is full and a new element needs to be added will double its size, make all the elements of the old array the same will copy the indexes and add the element to the new array. Use the following functions. Write in C language. 1.append(dynamic array, new element): function sent to itself Adds the element sturct to the end of the array inside DynamicArray. Place If not, it behaves in accordance with the dynamic array definition given above. 2. get(index): The element in the index sent to it by the function takes it from the array it hosts and sends it back. Do not use link lists. Solve the question using array.arrow_forwardWRITE A CODE IN C++ You work for an analytics organization have been tasked with writing a program that simulate a service desk. This service desk should be able to service 100 customers that can have one of three different priorities (high, medium, and low). The duration for any customer is a random number (between 5 minutes and 8 minutes). Code a modular program that uses parallel arrays to store priority levels and service times for 100 service requests. The program must do the following: The program must first ask the user to run the simulation or quit the program using a menu. This must be done using a function that is called. Upon input of the menu choice, if the user inputs a 2, the program will exit, otherwise if the user enters a 1, the program must begin processing the 100 service requests. The program must use loops and random numbers to generate priority level (high, medium, or low) as well as the service time (between 5 and 8 minutes) for each request in parallel arrays.…arrow_forwardIn C programming languge: Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92}arrow_forward
- Write 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_forwardRevise the following Course class implementation in the following c++ code When adding a new student to the course, if the array capacity is exceeded, increase the array size by creating a new larger array and copying the contents of the current array to it. Implement the dropStudent function. Add a new function named clear() that removes all students from the course. Implement the destructor and copy constructor to perform a deep copy in the class. Write a test program that creates a course, adds three students, removes one, and displays the students in the course.arrow_forwardA 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_forward
- Write a function void switchEnds(int *array, int size);that is passed the address of the beginning of an array and the size of the array. The function swaps the values in the first and last entries of the array.arrow_forwardin C++ // praphrase or change this code , while the purpose and output is same . #include <iostream>#include <stdint.h>using namespace std; class arr{private: int size_arr; //To store the size of the array char *begin; //Pointer for dynamic allocation of memory int capacity; //To store the capacity of the elements in the arraypublic: arr(int n); //constructor (default) for creating an array int insert_arr(char x); //To insert an element x in the array int remov_arr(int i); //To remove an element from position i void display(); void displayAtPos(int i);//Function to display a character at position i}; arr::arr(int n){ size_arr = n; begin = new char[size_arr]; capacity = 0;} int arr::insert_arr(const char x){ if(capacity >= size_arr){ cout << "Array Full!" << endl; return -1; } begin[capacity++] = x; //One element is added, so increment the…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_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 Programming: Write a function inputAllCourses() which receives an array of course pointers and the array’s size, then allows the user to input all courses in the array by calling inputCourse()arrow_forwardIn C programming: Write a function printAllCourses() which receives an array of course pointers and the array’s size, then prints all courses in the array by calling printCourseRow()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