estion #4 please. Given code in lab #include #include /* Test that memory is properly de-allocated by using valgrind to execture * * To compile: gcc lab4b.c * * To execute: ./a.out * * To execute with valgrind tool: valgrind ./a.out */
Solve question #4 please.
Given code in lab
#include <stdio.h>
#include <stdlib.h>
/* Test that memory is properly de-allocated by using valgrind to execture
*
* To compile: gcc lab4b.c
*
* To execute: ./a.out
*
* To execute with valgrind tool: valgrind ./a.out
*/
int main (int argc, char** argv)
{
int i;
/* allocation and use of a dynamic ONE dimenstional array */
/* variable declararion */
int* arr1;
/* allocate space for 20 intergers */
arr1 = (int*) malloc (sizeof(int) * 20 );
/* initialize all array locations to contain the value zero */
for ( i = 0 ; i < 20 ; i++ )
arr1[i] = 0;
/* print out all of the values */
for ( i = 0 ; i < 20 ; i++ )
printf ("Position: %3d, value: %3d\n", i, arr1[i]);
/* de-allocated the memory for the array when finished */
free (arr1);
return 0;
}
Thanks in Advance. :)
![Q4.2
Building on the code from your answer from Q4.1, write the C code that would initialize all of the
array locations to zero.
Q5
Building on the code given in your answers to Questions 3 and 4, write the C free code that
would deallocate the memory allocated in Q4.1.
Verify the correctness of your code using the tool valgrind on bertvm.cs.uic.edu.
In the header comment at the top of lab4b.c, there are notes on how to use the valgrind. Run
valgrind with lab4b.c as given. Then comment out the free() statement in the program lab4b.c.
Recompile and run valgrind again. Notice the LEAK SUMMARY given by the valgrind tool.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F672bf286-8abe-4b07-9ca1-0d5b2612956c%2F8570c055-cad8-4d5c-9d9d-67599384852e%2Fuu9z7yv_processed.png&w=3840&q=75)
![Q3
For Questions 3-4: Consider the following code from lab4b.c. This code uses a pointer to a one
dimensional array of type int.
// variable declaration
int*
arr1;
// allocate space for 20 integers
arri = (int *) malloc ( sizeof (int) * 20 );
// initialize all array locations to zero
for (int i = 0 ; i < 20 ; i++ )
arr1[i] = e;
// de-allocating the memory when finished
free (arr1);
Q3.1
Write the C variable declaration for a pointer to a dynamically allocated two dimensional array
of type int. To access a value in the 2D array, the variable must be able to use two square
bracket operators such as:
arr2d[i][j] = x;
int **arr2d;
Q4
Q4.1
Using the varaible declared in Q3, write the C malloc code that would allocate memory for a 2D
array of 20x30 integers and store them into the array from Q3.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F672bf286-8abe-4b07-9ca1-0d5b2612956c%2F8570c055-cad8-4d5c-9d9d-67599384852e%2Fx010htd_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
I have solved 4 and 5 as per the given description.
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)