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
- Please define and initialize and array X of length 8. You need implement following and write outcomes with reasons; int X[ ] = { 1,2,3,4,5,6,7,8 }; 1.cout<< X+1; 2.cout<<&X+1; 3.cout<<2[X]; 4.cout<<&X[0]+1; 5.cout<<*(X+1); 6.cout<<*X+1; Also, practice and play with pointers using multiplication, division, subtraction and addition. You may also play with it your ways and find out reasoning. Write down those reasoning.arrow_forwardConsider the following function main:Int main(){int inStock[10][4];int alpha [20];int beta[20];int gamma[4]= {11,13,15,17};int delta [10] = {3,5,2,6,10,9,7,11,1,8};} a) Write the definition of the function setZero that initializes any one-dimentional array of type int to 0.b) Write the definition of the function inputArray that prompts the user to input 20 numbers and stores the numbers into alpha.c) Write the definition of the function doubleArray that initializes the elements of beta to two times the corresponding elements of alpha. Make sure that you prevent the function from modifying the elements of alpha.d) Write the definition of the function copyGamma that sets the elements of the first row of inStock to gamma and the remaining rows of inStock to three times the previous row of inStock. Make sure that you prevent the function from modifying the elements of gamma.e) Write the definitions of the function copyAlphaBeta that stores alpha into the first five rows of inStock and beta…arrow_forwardYour Friendly Array Neighbor Program only in java not in python Create a Java program that takes an array called ar as an input that keeps track of arrays. Every day a new array is produced using the array of the previous day. On the n’th day, the following operations are performed on the array of day n – 1 to produce the array of day n: -If an element is smaller than both left and right neighbor, then this element is incremented -If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented. -The first and last elements should never change Eventually after some days the array will not change, some examples: Example: Input from user: ar = [6,2,3,4] Output from program: [6,3,3,4] After the first day the array is changed from [6,2,3,4] to [6,3,3,4] and no further operations may be completed. Example: Input from user: ar = [1,6,3,4,3,5] Output from program: [1,4,4,4,4,5] After the first day the array is changed from [1,6,3,4,3,5]…arrow_forward
- Write a function for given main functionint * return_an_array(int n)which takes an integer n and returns a pointer to an array (allocated off of the heap inthe function) containing the digits of n in the appropriate positions. If n = 0 return 0(which is the “null” pointer). Recall an array name is just a pointer to the first element ofthe array. Please use following main to test your function.int main() {int *a = return_an_array(23542); // array of size 5for(int i=0;i<5;i++)cout<<a[i]<<" "; // print 2 3 5 4 2cout<<endl;delete [] a;int *b = return_an_array(12345678); // array of size 8for(int i=0;i<8;i++)cout<<b[i]<<" "; // print 1 2 3 4 5 6 7 8cout<<endl;delete [] b;return 0;}arrow_forwardint arr[10]; Given the array declaration above, which of the following array operation are supported by the standard C library? i) arr.length() ii) arr[-1] iii) arr[3:4] iv) None of the above int arr[10][50]; The code above statically allocates a 2D array with 10 rows and 50 columns. If instead we were to allocate this 2D array dynamically, how many calls to malloc() would be needed such that we can still use the arr[i][j] access syntax? a) 10 b) 11 c) 50 d) 51 e) 1 char buf[50]; Given the character array declaration above, what would be the data types of the following? *buf bufarrow_forwardAn array is a container object that holds a fixed number of values of a single type. To create an array in C, we can do int arr[n];. Here, arr, is a variable array which holds up to integers. The above array is a static array that has memory allocated at compile time. A dynamic array can be created in C, using the malloc function and the memory is allocated on the heap at runtime. To create an integer array, of size , int *arr = (int*)malloc(n * sizeof(int)), where points to the base address of the array. When you have finished with the array, use free(arr) to deallocate the memory. In this challenge, create an array of size dynamically, and read the values from stdin. Iterate the array calculating the sum of all elements. Print the sum and free the memory where the array is stored. While it is true that you can sum the elements as they are read, without first storing them to an array, but you will not get the experience working with an array. Efficiency will be required later.…arrow_forward
- Write a single line of code that will accomplish the following: declare arr to be of type "pointer to a char" allocate a dynamic array of type char with capacity 4 assign the starting address of the array to arr HINT: Use conventional spacing (one space each side of the = operator) and don't forget your semicolon.arrow_forwardWrite a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. #include <iostream>using namespace std; /* Your solution goes here */ int main() { const int SORT_ARR_SIZE = 4; int sortArray[SORT_ARR_SIZE]; int i; int userNum; for (i = 0; i < SORT_ARR_SIZE; ++i) { cin >> userNum; sortArray[i] = userNum; } SwapArrayEnds(sortArray, SORT_ARR_SIZE); for (i = 0; i < SORT_ARR_SIZE; ++i) { cout << sortArray[i] << " "; } cout << endl; return 0;}arrow_forwardWrite a program that will sort a prmiitive array of data using the following guidelines - DO NOT USE VECTORS, COLLECTIONS, SETS or any other data structures from your programming language. The codes for both the money and currency should stay the same, the main focus should be on the main file code (Programming language Java) Create a helper function called 'RecurInsSort' such that: It is a standalone function not part of any class from the prior lab or any new class you feel like creating here, Takes in the same type of parameters as any standard Insertion Sort with recursion behavior, i.e. void RecurInsSort(Currency arr[], int size) Prints out how the array looks every time a recursive step returns back to its caller The objects in the array should be Money objects added or manipulated using Currency references/pointers. It is OK to print out the array partially when returning from a particular step as long as the process of sorting is clearly demonstrated in the output. In…arrow_forward
- Write a function named "getDynamicArrayOfPointersToDouble" that accepts an array of double and its size. The function will allocate an array of the same size of double pointers dynamically (using new operator) For each number in the given array, it will allocate memory for each double initialized it with that value and save the pointer in the new array.It then returns the pointer of the array of double pointers to the caller. In another words, it builds an array of double pointers from the given array of doubles. Here is an example of calling "getDynamicArrayOfPointersToDouble"with the array of 4 doubles of {10, 20, 30, 40}.It will allocate an array of 4 pointers each will point to double which contains 10, 20, 30 and 40 respectively. The caller should print out this array based on that description.arrow_forwardwrite in C for a STM32F446RE microcontroller Write a source library that contains the with the following public functions: void keypadInit(void); /Initiallized the GPIO to read the keypad. uint16_t readKeypad(void); //Returns the state of all of the keypad buttons in the return value at the moment the function is called. void decodeKeypad(uint16_t, char *); //Takes the state of the keypad and returns (by reference) an array of the key's pressed. The library should work with the following main: int main (void) { uint16_t key; char carray[17]; keypadInit(); while(1) { while(!(key = readKeypad())); /*Get which keys pressed*/ decodeKeypad(key, carray); /*What are those keys*/ printf("%s\n",carray); /*Print those keys to screen*/ while(readKeypad() == key); /*Wait for the keypad to change*/ }} Problem 1: Write a library that works with the following pin assignments Row 0 -> PC0 Row 1 -> PC2 Row…arrow_forwardTalk about the difference of accessing elements in an array VS access the memory address of an array What will happen with the following code: 1: cout << array<<endl; 2: cout << array[0] << endl; Does C++ support Index Out of bound Checking? If so, please explain how that works, if not, explain what will happen if Index Out of bound happens.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