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 5 or more reasons on the positives and negatives of unitedhealthcare?
List 5 or more reasons on the positives and negatives of unitedhealthcare?
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...
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.
Use set notation to list the elements. 1.The set of positive integers between 5 and 9....
Use set notation to list the elements. 1.The set of positive integers between 5 and 9.    2. The set of seasons in a year. State whether the first set is a subset of the second set. 3. {7, 9, 11}; {The odd counting numbers}    4. {The integers larger than 5}; {7, 8, 9,…} Find the cardinality for the set. 5. A = {The number of seconds in a minute}    6. A = {-8, -7, -6,…, 0}
5.How are Lambda & Gamma similar & how are they different? Include elements of their calculations...
5.How are Lambda & Gamma similar & how are they different? Include elements of their calculations & interpretations in your response. (Please type answer)
IN PYTHON Question B Considering how objects list in their own memory space and can behave...
IN PYTHON Question B Considering how objects list in their own memory space and can behave almost like independent entities, think about some possible programs that you could create with this concept. (for example, a simulated worlds). Be specific in your description as to how OOP concepts would be employed.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT