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>voidmyfunction(int size) {int *p;p = malloc(size*sizeof(int));assert(p != NULL);for (int i = 0; i < size; i++) {p[i] = 1;}}intmain(int argc, char *argv[]){if (argc != 2) { fprintf(stderr, "usage: broken5 <value>\n"); exit(1); } int size = atoi(argv[1]);int counter = 0;while(1) { // the infinite while loop is NOT a bugmyfunction(size);printf("%d \t", counter);counter++;}return 0;}

Question
Asked Nov 30, 2019
57 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>

void
myfunction(int size) {
int *p;
p = malloc(size*sizeof(int));
assert(p != NULL);
for (int i = 0; i < size; i++) {
p[i] = 1;
}
}

int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: broken5 <value>\n");
exit(1);
}
int size = atoi(argv[1]);
int counter = 0;
while(1) { // the infinite while loop is NOT a bug
myfunction(size);
printf("%d \t", counter);
counter++;
}

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[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**).
Step 3

Program:

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

 

void

myfunction(int size) {

int *p;

//type cast memory to int pointer type

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

assert(p != NULL);

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

p[i] = 1;

}

}

 

int

main(int argc, char *argv[])

{

if (argc != 2) {

fprintf(stderr, "usage: broken5...

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: Why am I getting a error message when trying to INSERT data for the table??   (photo attached)

A: The syntax to insert data in the table isINSERT INTO Table_Name (field_Name1, field_Name2, field_Nam...

question_answer

Q: Which of the following is used to communicate with the network? Southbound API Northbound API Northb...

A: To communicate with the network, better Application program Interface is: Northbound API is supposed...

question_answer

Q: Using the code in part I of this lab (included below): 1. Create a function to INPUT the information...

A:  Create a function INPUT which takes a structure variable as parameter.Pass structure variable using...

question_answer

Q: Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements:a. Check wh...

A: We’ll answer the first three subparts of the question since the exact one wasn’t specified. Please s...

question_answer

Q: Given a nested list named table, use a loop to print each sub list of the nested list. The sub lists...

A: Program descriptionOne 3 x3 list is created to demonstrate the conceptdisplay the elements of the su...

question_answer

Q: Given the follwing business rules, create an ERD.  The ERD must include all primary keys, foreign ke...

A: ER diagram:

question_answer

Q: What are the SQL query commands to create the tables (attached) with appropriate attributes and data...

A: Primary key:A primary key is a minimum set of attributes (columns) in a table with uniquely defined ...

question_answer

Q: How do I write a line of code in Java to find how many numbers are below and above average ?

A: ⦁ Create two variables in the main method and initialize them with zero.⦁ Iterate the all numbers wi...

question_answer

Q: 13.10 Explain the concept of a DHCP lease. How is it obtained? How is it used? What does it provide?

A: Obtaining DSCP (Dynamic Host Configuration Protocol) lease time:Lease time assigning process depends...