
C programming I'm trying to fix memory leak but it kept giving that one error. Where did I go wrong?
#include <stdlib.h>
#include <stdio.h>
void printArray(int *arr, int size){
int i;
if (arr != NULL)
{
for( i = 0; i < size; i++) {
// Print each element out
printf("%d", *(arr+i));
//Print addresses of each element
printf("%p", (arr+i));
//Printing a new line
printf("\n");
}
}
}
int main() {
// Allows user to specify the original array size, stored in variable n1.
printf("Enter original array size: ");
int n1 = 0;
scanf("%d", &n1);
//a- Create a new array *a1 of n1 ints using malloc
int *a1 = malloc(n1 * sizeof(int));
//b- Remove comments for if statment below
//b- Set each element in a1 to 100 + indexValue ??
if(a1 == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
int i;
for (i = 0; i < n1; i++)
{
a1[i] = 100 + i;
}
printf("Printing the first array allocated using malloc\n");
//c- Print each element and addresses out (to make sure things look right)
printArray(a1, n1);
// User specifies the new array size, stored in variable n2.
printf("\nEnter new array size: ");
int n2 = 0;
scanf("%d", &n2);
//d- Dynamically change the array a1 to size n2
a1 = malloc (n2 * sizeof(int));
//e- Remove comments for if statment below
if(a1 == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
//e- If the new array is a larger size, set all new members to 200 + indexValue.
if (n2 > n1)
{
int i;
for (i = 0; i < n2; i++)
{
a1[i] = 100 + i;
}
}
printf("Printing the reallocated array: \n");
//f- Print each element and addresses out (to make sure things look right)
printArray(a1, n2);
//g-Free the allocated memory for a1
free(a1);
a1 = NULL;
printf("\nEnter new array size to be initialized with 0: ");
int n3 = 0;
scanf("%d", &n3);
//h- Remove comments for if statement
//h- Using calloc create a new array and assign it to a2
//with new array size, stored in variable n3.
int *a2 =calloc(n3, sizeof(int));
if(a2 == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Printing the array created using calloc: \n");
//i- Print array a2 with size n3
printArray(a2, n3);
//j- Print array a1 again, How you can fix this problem
printf("Printing deallocated array a1: \n");
printArray(a1, n2);
// Done with arrays now, free the and assign NULL
free(a2);
a1 = NULL;
a2 = NULL;
printf("Program Successfully Completed!");
return 0;
}


Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

- C++languagearrow_forward) Consider the following C code snippet. void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } int findMinimum(int arr[], int N) { // variable to store the index of minimum element int min_idx = 0; int min_E = arr[min_idx]; // Traverse the given array for (int i = 1; i < N; i++) { // If current element is smaller than min_idx then update it if (arr[i] < min_E) { min_idx = i; min_E = arr[min_idx]; } } return min_idx; } /* Function to sort an array using selection sort*/ void selectionSort(int arr[], int n) { int i, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = findMinimum(&arr[i], n-i); // Swap the found minimum element with the first element if(min_idx != 0) swap(&arr[min_idx+i], &arr[i]); } }…arrow_forwardHow difficult is it to duplicate a collection of shared pointers into another array while using the C++ programming language? Create a list of the several approaches you may use to tackle the issue that has been presented to you. Is it the case that copying a shared pointer also copies the objects that it controls? Explainarrow_forward
- Java Programming language Please help me with this questionarrow_forwardHow simple is it to transfer shared references into another array in C++? List approaches to tackle the issue. Do shared pointers copy the objects they control? Explainarrow_forwardHow simple is it to transfer shared references into another array in C++? List approaches to tackle the issue. Do shared pointers transfer the objects they control? Explain?arrow_forward
- QUESTION 1 When defining a Java array, the index range of the array: Must start at 1 Must start at 0 Can be a customized range (from 2 to 5, e.g.) at compilation time Can be a customized range (from 2 to 5, e.g.) at run timearrow_forwardWrite a code using c programmingarrow_forwardJava Programming language Please help me with this questionarrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





