analyze each program and figure out what is wrong with the memory management in the code and how to fix the code.#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <assert.h>intmain(int argc, char *argv[]){if (argc != 2) { fprintf(stderr, "usage: broken3 <value>\n"); exit(1); } int size = atoi(argv[1]);int **a;a = malloc(size*sizeof(int));assert(a != NULL);for (int i = 0; i<size; i++) {a[i] = malloc(size*sizeof(int));for (int j = 0; j<size; j++) {a[i][j] = j;}}free (a);//code that does more stuff here.return 0;}

Question
Asked Nov 30, 2019
30 views

analyze each program and figure out what is wrong with the memory management in the code and how to fix the code.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: broken3 <value>\n");
exit(1);
}
int size = atoi(argv[1]);
int **a;
a = malloc(size*sizeof(int));
assert(a != NULL);
for (int i = 0; i<size; i++) {
a[i] = malloc(size*sizeof(int));
for (int j = 0; j<size; j++) {
a[i][j] = j;
}
}
free (a);
//code that does more stuff here.
return 0;
}

 

check_circle

Expert Answer

Step 1

In this program malloc() function is used to allocate memory.

malloc() syntax:

pointer= (cast-type*) malloc(byte-size)

 

This program tested with command line input value: 10.

Step 2

Wrong with memory management in the code:

  • malloc() used to allocate memory size but does not use type casting.
  • The statement a = malloc(size*sizeof(int)) used to assign size in double pointer.
  • This statement requires to type-case the size in integer type double pointer, which is (int**).
  • The statement a[i] = malloc(size*sizeof(int)) used to assign size in 1d array of pointer.
  • This statement requires to type-case the size in integer type single pointer, which is (int**).
  • for loop is used to print 2d array.
Step 3

Program:

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

 

int

main(int argc, char *argv[])

{

if (argc != 2) {

fprintf(stderr, "usage: broken3 <value>\n");

exit(1);

}

int size = atoi(argv[1]);

int **a;

//memory alocation for 2d array

a = (int**)malloc(size*sizeof(int));

assert(a != NULL);

//memory alocation for 1d array

for (int i = 0; i<size; i++) {

a[i] = (int*)malloc(size*...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: What is the main difference between WEP and WPA?

A: Wired Equivalent Privacy(WEP):WEP is the world's most popular Wi-Fi security protocol. It is part of...

question_answer

Q: Simplify the following Boolean function , using three- variable maps: a) F(x,y,z) = Σ (0,2,6,7) b) F...

A: Note: Since we only answer up to 3 sub-parts, we’ll answer the first 3. Please resubmit the question...

question_answer

Q: In this unit, we covered several tips for revising and editing documents. What do you believe is you...

A: Revising and editing featuresRevising and editing are the two tasks that are important for the writi...

question_answer

Q: What are the SQL query commands (does not need to be on a platform, just need to verify the query co...

A: SQL query commands to create tables:To create table “COURSE” with “COURSE_TYPE” as primary key,CREAT...

question_answer

Q: what are the major differences between php and asp.net?

A: PHP stands for PHP Hypertext Preprocessor, it is server-side scripting used to make interactive and ...

question_answer

Q: privacy and data protection in mobile applications.

A: Privacy design strategies in mobile device:Minimize the usage – limit the processing of personal dat...

question_answer

Q: What is inheritance in c++? Explain various types of inheritance?

A: Inheritance in c++:Inheritance is defined as the capability of deriving the properties and character...

question_answer

Q: Let's say we have the attached table :   Find the year in which employees hired with minimum salary ...

A: Answer:Select HIREDATE from EMP where SAL &gt; 1000;Here, the above query will display the employees...

question_answer

Q: Why are jumping statements in c? Explain with help of examples?

A: Definition of jump statement in C language –Jumps statements are used to disrupt the program’s norma...