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 */

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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.
Transcribed Image Text: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.
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.
Transcribed Image Text: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.
Expert Solution
Step 1

I have solved 4 and 5 as per the given description.

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education