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;}

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 19SA
icon
Related questions
Topic Video
Question

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;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,