8. Write few lines of code that creates two arrays with malloc. Then write a statement that can create a memory leak. Discuss why you think your code has a memory leak by drawing the status of the memory after you use malloc and the line of the code you claim that creates a memory leak.
(C Programming)
Creating the two dynamic arrays :
int *array1 = (int *) malloc(sizeof(int)); int *array2 = (int *) malloc(sizeof(int));
Now, the code for memory leak could be like this :
int *array1;
int *array2;
int n = 100;
for(int i = 0; i< n; i++)
{
array1 = (int *) malloc(sizeof(int));
array2 = (int *) malloc(sizeof(int));
// Allocation is done and we havn't free the space and now we'll try to assign the some values in the location of array1 and array2, this will cause memory leak.
*array1 = i * 10;
*array2 = i % 10;
}
If you want to avoid the case of memory leak always free the memory you allocated for any purpose then you can perform another task at that location,
So the updated code after avoiding the memory leak would be:
int *array1 = (int *) malloc(sizeof(int)); int *array2 = (int *) malloc(sizeof(int)); free(array1); free(array2); *array1 = 1000; *array2 = 9;
8. Write few lines of code that creates two arrays with malloc. Then write a statement...
C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...
Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...
c++ help
Write a program that: Creates two finite arrays of size 10 These arrays will serve as a storing mechanism for student grades in Classes A and B Uses a loop to populate the arrays with randomly generated numbers from 0 to 100.These numbers will represent student grades. Compute average grade of each class. Finally, output the two arrays and the message with two averages and best class. ("Class A average is: 65.43; class B average is: 68.90. Class...
write a code to multiply two integer arrays (each with N numbers). Monitor the time to run that code with the following N: 50, 100, 200, and 400 ================= You can write in either C or in Java, Provide correct solution to get upvote, Thanks
Write the code in PYTHON and please provide the
full code, thank you!
Write a program that plots two functions: f(x) = x2 and g(x) = ax. Write it in such a way that the intersection points are marked with an X, for arbitrary a. • You may use your favorite ready-made function to find the intersection points, if not: • One possible way to tackle this problem is to put both function values in arrays and check using a...
Java Programming Language Write code in Java that shows race condition using two or more threads. Give comments on first line (can be multiple lines), explain race condition that occurs in your program.
Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...
Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...
C LANGUAGE!!! The program uses both files and two dimensional arrays. The Problem Statement Business is going well for your friend, who is selling discounts to area clubs and restaurants, which means that business is going well for you! Both of you have decided that you'll advertise on memory mall, which is roughly arranged as a rectangle. There's not enough time before a football game to hand flyers to everyone arranged on the mall. Therefore, you will only be able...
#PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a list of up to 10 players and their high scores in the computer's memory. Use two arrays to manage the list. One array should store the players' names, and the other array should store the players' high scores. Use the index of the arrays to correlate the names with the scores. Your program should support the following features: a. Add a new player and...