Question

In: Computer Science

2. [9 pts] Write the code necessary to properly allocate memory (on the heap) in the...

2. [9 pts] Write the code necessary to properly allocate memory (on the heap) in the following scenarios

a). An array a of n characters
b) An array b of m+n integers
c) An x × y × z matrix m1 of integers initialized to 0s

Use C code

Solutions

Expert Solution

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

void main()
{
   // malloc() is used to allocate memory at runtime and memory at runtime is allocated using heap
   int count;
   printf("Enter count of characters for which mmeory is required: ");
   scanf("%d", &count);
  
   if(count <= 0)
   {
       printf("Size should be more than 0, always!!");
       return;
   }
  
   // This is the syntax for malloc() function: (void *)malloc(number_of_bytes)
   /*
   Since malloc() returns a void pointer, we need to cast it into some predefined data type.
   */
   // part a
   char *a = (char *)malloc(sizeof(char)*count);
   printf("\n--------------------------------------\n");
   // part b
   int m, n;
  
   printf("Enter m: ");
   scanf("%d", &m);
  
   printf("Enter n: ");
   scanf("%d", &n);
   if(m+n <= 0)
   {
       printf("Size should be more than 0.");
       return;
   }
   int *b = (int *)malloc(sizeof(int)*(m+n));
   printf("\n--------------------------------------\n");
   // part c
   int x, y, z;
   printf("Enter x: ");
   scanf("%d", &x);
  
   printf("Enter y: ");
   scanf("%d", &y);
  
   printf("Enter z: ");
   scanf("%d", &z);
  
   if((x+y+z) <= 0)
   {
       printf("Size should be more than 0.");
       return;
   }
  
   int ***matrix, i, j;
  
   // this is how memory for 3D matrix is allocated.
   matrix = (int ***)malloc(x * sizeof(int**));
  
   for(i = 0 ; i < x ; i++)
   {
       matrix[i] = (int **)malloc(y * sizeof(int *));
       for(j = 0 ; j < y ; j++)
       {
           matrix[i][j] = (int *)malloc(z * sizeof(int));
       }
   }
   printf("\n--------------------------------------\n");
}


Related Solutions

C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Write code that would allocate the space for an array of 20 integers. It will be...
Write code that would allocate the space for an array of 20 integers. It will be pointed to by a variable named "junk" Write code that puts “file did not open” into an error stream.
Problem 2(a). Letter Frequencies. ? Write Python code that reads a text file into memory and...
Problem 2(a). Letter Frequencies. ? Write Python code that reads a text file into memory and creates a dict object with a frequency count for each letter. For example, for encryptedA.txt, your output should contain the key:value pairs 'a': 78 and 'b': 31. Notes Do not distinguish between uppercase and lowercase letters. Ignore punctuation. Punctuation counts must not appear in your dict If a given letter does not appear in the text, there must be a key:value pair with value...
Write Algoritm , code and output. In Operating Systems , 26. Implement a program to allocate...
Write Algoritm , code and output. In Operating Systems , 26. Implement a program to allocate memory by applying the following strategies.a .FIRST FIT b.BEST FIT c.WORST FIT
Write an assembly code the counts the number of accuracies of the byte AAh in memory...
Write an assembly code the counts the number of accuracies of the byte AAh in memory from address 120Ah to address 130Ah. You need to use a subroutine and call it 'COUNT' to do so. You also need to provide the count in BCD if it was less than 64h so that you need to include another subroutine called 'ToBCD' to do so. assembly 8086
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory...
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to compute the value of the best packing of the knapsack? For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to identify the items that are included in an optimal packing of the knapsack? For the Longest Common Subsequence algorithm, is it necessary to maintain the entire...
Using pseudocode, write the code that will perform the necessary file operations as described in the...
Using pseudocode, write the code that will perform the necessary file operations as described in the short scenario below: “During registration at a college, student’s details will be captured by a staff member and written to a file called “studentDetails.dat”. The following student details will be captured: name, surname, studentNumber, registeredCourse. The staff member will be prompted for the student details. After every record added to the file, the staff member will be asked whether they would like to add...
Question 1 2 pts (TCO 1) Which of the following is not a necessary life function?...
Question 1 2 pts (TCO 1) Which of the following is not a necessary life function? Nutrients Maintaining boundaries Responsiveness Metabolism Movement Question 2 2 pts (TCO 1) In describing the relationship of the thoracic and spinal cavities, the thoracic cavity is ventral to the spinal cavity. the thoracic cavity is inferior to the spinal cavity. the thoracic cavity is superior to the spinal cavity. the thoracic cavity is proximal to the spinal cavity. the thoracic cavity is medial to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT