Question

In: Computer Science

Memory was allocated initially for 5 elements, later on two more elements added to list. How...

Memory was allocated initially for 5 elements, later on two more elements added to list. How can
space be managed in such case. Implement the scenario in C.
IN C PROGRAMING

Solutions

Expert Solution

CODE:

#include <stdio.h>

#include <stdlib.h>

int main(void) {

  int *ptr;

  // a dynamic array of size 5 is allocated

  ptr = (int*) malloc(5 * sizeof(int));

  for(int i = 0; i < 5; ++i){

    // storing index+1 in the array

    *(ptr+i)=i+1;

  }

  printf("%d\n",*(ptr+4));

  // now for example assume 5 is a bit short for us

  // so lets extend the size of ptr array

  ptr = realloc(ptr, 7 * sizeof(int));

  // now we can store value till index 7

  for(int i = 0; i < 7; ++i){

    // storing index+1 in the array

    *(ptr+i)=i+1;

  }

  printf("%d\n",*(ptr+6));

return 0;

}

OUTPUT:

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.


Related Solutions

two or more elements
if two or more elements are in the same row, what do they have in common? If two or more elements are in the same column, what do they have in common?
List 5 intentional torts, and explain the elements needed to prove each. List and explain the...
List 5 intentional torts, and explain the elements needed to prove each. List and explain the elements to prove negligence. What is the difference between negligence per se and comparative negligence? What is the relationship between contributory negligence and res ispa loquitor? What are the differences in the 3 types of damages available in a tort case?
16. List the two elements involved in preparing for maintenance on heat exchangers. 21. List two...
16. List the two elements involved in preparing for maintenance on heat exchangers. 21. List two methods of cleaning the outside surfaces of tubes in a large shell- and-tube heat exchanger 26. Name the four types of plugs used in shell-and-tube heat exchangers to seal leaking tubes 28. Why is maintenance of a smaller heat exchanger done more easily in the shop?
How is a neurotransmitter generally defined? Your answer will list 3 primary elements and two approaches...
How is a neurotransmitter generally defined? Your answer will list 3 primary elements and two approaches used to define a neurotransmitter 1. 2. 3. 4. 5.
List of account activity and how they are added or subtracted on the statement of cash...
List of account activity and how they are added or subtracted on the statement of cash flows? 1. Cash Received from Customers 2. Decrease in Accounts Receivable 3. Increase in Inventory 4. Decrease in Inventory 5. Increase in Prepaid Expenses 6. Decrease in Prepaid Expenses 7. Equipment Purchase 8. Sale of Equipment 9. Paid Salaries and Wages 10. Cash Received from Issuing Stock 11. Cash Paid for Income Taxes 12. Cash Paid to purchase investments in Securities 13 Dividends Paid...
List 5 or more reasons on the positives and negatives of unitedhealthcare?
List 5 or more reasons on the positives and negatives of unitedhealthcare?
11. What are the functions of the 5’ cap? How is it added on the RNA?...
11. What are the functions of the 5’ cap? How is it added on the RNA? 12.Explain how a poly-A tail is produced; (CPSF and CstF, Poly APolymerase, Poly A Binding Protein (PABP)). 13.What are the two hypotheses for eukaryotic transcription termination? 14.How does splicing of pre-mRNA occur? 15.Compare and contrast splicing of group I introns, group II introns and spliceosome-mediated splicing of mRNA.
C++ Given two finite sets, list all elements in the Cartesian product of these two sets.
C++ Given two finite sets, list all elements in the Cartesian product of these two sets.
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
Assume there is a 30-byte heap. The free list for this heap has two elements on...
Assume there is a 30-byte heap. The free list for this heap has two elements on it. One entry describes the first 10-byte free segment (bytes 0-9), and one entry describes the other free segment (bytes 20-29). Now assume we have a request for just a single byte of memory. In this case, the allocator will perform an action known as __________ to find a free chunk of memory that can satisfy the request. splitting coalescing chopping relocating Refer to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT