These questions refer to memory allocation with my answers; would like to confirm if I explained everything correctly: Please refer to the image for the memory allocation example code. On some system, invoking the program as ./memory 0,0,0,0 produces this output: 0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 | -------------- Question 1: If invoking the program as ./memory 480,0,0,0 produced this output: 0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 | -------------- The 480-byte chunk was allocated at address 2144 would you conclude that the heap allocator allocates new chunks of RAM at the beginning (i.e., lower addresses) or at the end (i.e., higher addresses) of holes in memory? Why?  In all subsequent questions, we assume that all considered algorithms go through the list of holes left to right (i.e., from low addresses to high addresses), and when there is a choice, the algorithms always pick the leftmost hole. My answer for #1: The heap allocator allocates new chunks of RAM at the end of holes in memory. You can see see that 2624 - 480 = 2144, which takes up all the space up to the upper bound of that specific hole (2112 | 2624). If the heap allocator allocated new chunks of RAM at the beginning of holes, then the new chunk would have been allocated at 2112 instead of 2144. 2112 + 480 = 2592, where the memory is stored at the lower bound address, 2112. Question 2: If invoking the program as ./memory 100,200,100,80 produces this output: 0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 | -------------- The 100-byte chunk was allocated at address 2524 The 200-byte chunk was allocated at address 2324 The 100-byte chunk was allocated at address 2224 The 80-byte chunk was allocated at address 3696 would you say that the heap allocator uses first fit best fit worst fit none of the above? Answer for #2: None of the above, listed below are the unmet conditions for each memory allocation algorithm: First fit: If the algorithm was first fit, all memory chunks would have been allocated within the 0 | 1024 address range. The total of all the chunks are 480 bytes so the first hole range would have been sufficient for first fit. Best fit: The 80-byte chunk was allocated in the best fit address range, however, one of the 100-byte chunks could have been allocated within the 3648 | 3776 range due to its 128-byte availability. Instead the two 100's and the 200-byte chunks were all allocated within the 512-byte hole. Worst fit: If the allocator used the worst-fit algorithm, all the byte chunks would have been allocated within the 1024-byte holes from left to right. Instead, the 80-byte chunk was allocated in the 128-byte hole at address 3696. Question 3: If invoking the program as ./memory 40, 400, 80, 60 produces this output: 0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 | -------------- The 40-byte chunk was allocated at address 3736 The 400-byte chunk was allocated at address 2224 The 80-byte chunk was allocated at address 2144 The 60-byte chunk was allocated at address 4804 would you say that the heap allocator uses first fit best fit worst fit none of the above? Answer for 3: None of the above, listed below are the unmet conditions for each memory allocation algorithm: First fit: If first fit was used, all memory chunks would have been allocated within the first hole, which is 1024 bytes. All of the chunks were allocated in different blocks so it can't be first fit. Best fit: For best fit, the 40-byte chunk would be allocated within the 1024 | 1088 hole. Instead, it was allocated in a 128-byte block at address 3736 so it cannot be best fit. Worst fit: If worst fit was used, every memory chunk would have been allocated in the 1024-byte holes from left to right. The 40-byte chunk was allocated in a 128-byte hole, the 400-byte chunk in the 512-byte hole, the 80-byte chunk in the 512-byte hole, and the 60-byte chunk in the best fit scenario, which is the 64-byte hole. Question 4: After looking up some documentation, you find out that the heap allocator uses worst fit! What would the output of the memory program be when invoked as follows: ./memory 400, 100, 10, 10 Answer for 4: Assuming that we follow the hole placement of the previous heap allocator (pick left-most hole/allocates at ending of holes): The worst fit algorithm always chooses the biggest hole for each memory allocation. The first chunk which is 400 bytes would be allocated at address 624 (1024 - 400 = 624) so the range 0-400 would be taken to decrease the size of the first hole to 624-bytes. The 100-byte chunk would then be allocated in the next biggest hole so it would be allocated to address 2012 (2112 - 100 = 2012), which decreases the size of that hole to 924 bytes. The first 10-byte chunk would be then allocated in the next 1024-byte hole at the address 3638 (3648 - 10 = 3638), which decreases the memory availability to 1014 bytes. The last 10-byte chunk would be allocated to the last 1024-byte hole at address 4790 (4800 - 10 = 4790).

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

These questions refer to memory allocation with my answers; would like to confirm if I explained everything correctly:

Please refer to the image for the memory allocation example code.

On some system, invoking the program as

./memory 0,0,0,0

produces this output:

0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 |

--------------

Question 1:

If invoking the program as

./memory 480,0,0,0

produced this output:

0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 |

--------------

The 480-byte chunk was allocated at address 2144

would you conclude that the heap allocator allocates new chunks of RAM at the beginning (i.e., lower addresses) or at the end (i.e., higher addresses) of holes in memory? Why? 

In all subsequent questions, we assume that all considered algorithms go through the list of holes left to right (i.e., from low addresses to high addresses), and when there is a choice, the algorithms always pick the leftmost hole.

My answer for #1: The heap allocator allocates new chunks of RAM at the end of holes in memory. You can see see that 2624 - 480 = 2144, which takes up all the space up to the upper bound of that specific hole (2112 | 2624). If the heap allocator allocated new chunks of RAM at the beginning of holes, then the new chunk would have been allocated at 2112 instead of 2144. 2112 + 480 = 2592, where the memory is stored at the lower bound address, 2112.

Question 2:

If invoking the program as

./memory 100,200,100,80

produces this output:

0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 |

--------------

The 100-byte chunk was allocated at address 2524

The 200-byte chunk was allocated at address 2324

The 100-byte chunk was allocated at address 2224

The 80-byte chunk was allocated at address 3696

would you say that the heap allocator uses

  • first fit
  • best fit
  • worst fit
  • none of the above?

Answer for #2:

None of the above, listed below are the unmet conditions for each memory allocation algorithm:

First fit: If the algorithm was first fit, all memory chunks would have been allocated within the 0 | 1024 address range. The total of all the chunks are 480 bytes so the first hole range would have been sufficient for first fit.

Best fit: The 80-byte chunk was allocated in the best fit address range, however, one of the 100-byte chunks could have been allocated within the 3648 | 3776 range due to its 128-byte availability. Instead the two 100's and the 200-byte chunks were all allocated within the 512-byte hole.

Worst fit: If the allocator used the worst-fit algorithm, all the byte chunks would have been allocated within the 1024-byte holes from left to right. Instead, the 80-byte chunk was allocated in the 128-byte hole at address 3696.

Question 3:

If invoking the program as

./memory 40, 400, 80, 60

produces this output:

0 | 1024 | 1088 | 2112 | 2624 | 3648 | 3776 | 4800 | 4864 |

--------------

The 40-byte chunk was allocated at address 3736

The 400-byte chunk was allocated at address 2224

The 80-byte chunk was allocated at address 2144

The 60-byte chunk was allocated at address 4804

would you say that the heap allocator uses

  • first fit
  • best fit
  • worst fit
  • none of the above?

Answer for 3:

None of the above, listed below are the unmet conditions for each memory allocation algorithm:

First fit: If first fit was used, all memory chunks would have been allocated within the first hole, which is 1024 bytes. All of the chunks were allocated in different blocks so it can't be first fit.

Best fit: For best fit, the 40-byte chunk would be allocated within the 1024 | 1088 hole. Instead, it was allocated in a 128-byte block at address 3736 so it cannot be best fit.

Worst fit: If worst fit was used, every memory chunk would have been allocated in the 1024-byte holes from left to right. The 40-byte chunk was allocated in a 128-byte hole, the 400-byte chunk in the 512-byte hole, the 80-byte chunk in the 512-byte hole, and the 60-byte chunk in the best fit scenario, which is the 64-byte hole.

Question 4:

After looking up some documentation, you find out that the heap allocator uses worst fit! What would the output of the memory program be when invoked as follows:

./memory 400, 100, 10, 10

Answer for 4:

Assuming that we follow the hole placement of the previous heap allocator (pick left-most hole/allocates at ending of holes):

The worst fit algorithm always chooses the biggest hole for each memory allocation. The first chunk which is 400 bytes would be allocated at address 624 (1024 - 400 = 624) so the range 0-400 would be taken to decrease the size of the first hole to 624-bytes. The 100-byte chunk would then be allocated in the next biggest hole so it would be allocated to address 2012 (2112 - 100 = 2012), which decreases the size of that hole to 924 bytes. The first 10-byte chunk would be then allocated in the next 1024-byte hole at the address 3638 (3648 - 10 = 3638), which decreases the memory availability to 1014 bytes. The last 10-byte chunk would be allocated to the last 1024-byte hole at address 4790 (4800 - 10 = 4790).

 

In this exercise we "reverse engineer" code to try to determine how the heap in a C program is managed. You do not need to compile and/or run this program. Consider the
following C program (compiled as an executable called memory):
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv) {
int chunk_sizes[4];
if ((argc != 2) ||
(sscanf(argv[1], "%d,%d,%d,%d", &chunk_sizes [0], &chunk_sizes[1], &chunk_sizes [2], &chunk_sizes [3]) != 4) ||
(chunk_sizes [0] < ®) || (chunk_sizes [1] < 0) || (chunk_sizes [2] < 0) || (chunk_sizes [3] < 0) ) {
fprintf(stderr,"Usage: %s < a, b, c, d>\n", argv[0]);
fprintf(stderr,"
exit(1);
}
where a, b, c, and d above must be >=0 integers (chunk sizes)\n");
char *ptrs [9];
unsigned long sizes[9] = {1024, 64, 1024, 512, 1024, 128, 1024, 64, 1024};
for (int i=0; i < 9; i++) {
ptrs[i]
printf("%ld | ", (unsigned long)(ptrs[i]));
}
(char *) calloc(sizes [i], sizeof (char));
%3D
printf("\n-
--\n");
for (int i=1; i < 9; i += 2) {
free(ptrs[i]);
}
for (int i=0; i < 4; i++) {
if (chunk_sizes [i] > 0) {
char *chunk =
printf("The %d-byte chunk was allocated at address %ld\n", chunk_sizes[i], (unsigned long)chunk);
(char *) calloc(chunk_sizes [i], sizeof(char));
}
}
exit(0);
}
Transcribed Image Text:In this exercise we "reverse engineer" code to try to determine how the heap in a C program is managed. You do not need to compile and/or run this program. Consider the following C program (compiled as an executable called memory): #include <stdio.h> #include <stdlib.h> int main (int argc, char **argv) { int chunk_sizes[4]; if ((argc != 2) || (sscanf(argv[1], "%d,%d,%d,%d", &chunk_sizes [0], &chunk_sizes[1], &chunk_sizes [2], &chunk_sizes [3]) != 4) || (chunk_sizes [0] < ®) || (chunk_sizes [1] < 0) || (chunk_sizes [2] < 0) || (chunk_sizes [3] < 0) ) { fprintf(stderr,"Usage: %s < a, b, c, d>\n", argv[0]); fprintf(stderr," exit(1); } where a, b, c, and d above must be >=0 integers (chunk sizes)\n"); char *ptrs [9]; unsigned long sizes[9] = {1024, 64, 1024, 512, 1024, 128, 1024, 64, 1024}; for (int i=0; i < 9; i++) { ptrs[i] printf("%ld | ", (unsigned long)(ptrs[i])); } (char *) calloc(sizes [i], sizeof (char)); %3D printf("\n- --\n"); for (int i=1; i < 9; i += 2) { free(ptrs[i]); } for (int i=0; i < 4; i++) { if (chunk_sizes [i] > 0) { char *chunk = printf("The %d-byte chunk was allocated at address %ld\n", chunk_sizes[i], (unsigned long)chunk); (char *) calloc(chunk_sizes [i], sizeof(char)); } } exit(0); }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY