Question

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

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
12. In a study of memory recall, 5 people were given 10 minutes to memorize a...
12. In a study of memory recall, 5 people were given 10 minutes to memorize a list of 20 words. Each was asked to list as many of the words as he or she could remember both 1 hour and 24 hours later. The result is shown in the following table. Assume we want to use a 0.05 significance level in your analysis. The research question is: Do students recall significantly more words after 1 hour than after 24 hours...
Answer question for C# 1 of 10 When items are added or removed, a list resizes...
Answer question for C# 1 of 10 When items are added or removed, a list resizes itself. True False Question 2 of 10 What happens when you assign one list to another using the assignment operator? An error message generates. Elements copy to new memory locations. Both lists become value-type data elements. Both lists reference the same memory locations. Question 3 of 10 Which of the following is a valid example of calling a method and sending it an array...
1. The memory units that follow are specified by the number of words times the number...
1. The memory units that follow are specified by the number of words times the number of bits per word. How many address lines and input/output data lines are needed in each case? (a) 8K X 16 (b) 2G X 8 (c) 16M X 32 (d) 256K X 64 2. Give the number of bytes stored in each memory unit in question 1. 3. Word number 563 decimal in the memory shown in Fig. 7.3 (see Mano-Ch7.pdf) contains the binary...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
5) Two heavenly bodies both with radii of 6×103 miles and masses of 9×1024 kg are...
5) Two heavenly bodies both with radii of 6×103 miles and masses of 9×1024 kg are separated by 4×107 miles. They are initially at rest. How fast are they moving just before their surfaces collide. Assume that you can ignore any effects having to do with the existence of atmospheres and that nothing significant exists in the space between the planets. Correct, computer gets: 1.25E+04 mi/hr Hint: Consider the initial "scene" before the planets gain any speed and the final...
1. Dynamics and artificial gravity. The space station has two thrusters pointed in opposite directions. They...
1. Dynamics and artificial gravity. The space station has two thrusters pointed in opposite directions. They operate by expelling propellant at high speed. The effect is that there is a force of magnitude F0 on the two ends of the space station in opposite directions, which causes the entire object to start rotating. The thrusters will stop firing when the artificial gravity created on the space station (described below) reaches the required value. Upward force, magnitude F0 Downward force, magnitude...
Please linked both files. For this assignment you need to create a ToDo list using Javascript,...
Please linked both files. For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
Item 5 In the case below, the original source material is given along with a sample...
Item 5 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version Other major issues involve the accepted methods by which fidelity is measured. There are two major methods described in the literature for fidelity measurement. The first is through mathematical measurement that calculates the number of identical elements shared between the real world and the simulation;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT