Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

his assignment tests the concepts of: Pointers Dynamic memory Program Objective:

ID: 3750087 • Letter: H

Question

his assignment tests the concepts of:

Pointers

Dynamic memory

Program Objective:

Write and test a C++ program using dynamic memory allocation. You will not be using the bag class as we did in last lab. If at any time you need assistance, please get your professors attention. Please test your program between each step before moving on.

Write the code to your program

Save your program

Execute the program

Verify the output

Modify your program

DELIVERABLES:

Main program: lab02.cpp

Submitted assignments without the above file named correctly will render your assignment as uncompilable and will be detrimental to your assignment grade.

Instructions:

Reading in data:

Create a int pointer and set it to NULL or nullptr.

Create a size_t variable to maintain the current size of the dynamic array, initialize this variable to 0.

Create a for loop which iterates over all the command-line arguments

Do not sum the numbers in this loop (assuming you read ahead)

If first argument (argi == 1): Create an array of size 1 on the heap and set its value to the first command-line arguments numeric value (use atoi)

For all other arguments: Increase the size of the dynamic array by ONE. Grow the array without introducing any memory leaks

Create and use a function that resizes the array

place the numeric value of command-line argument to the “empty” location of the array

Processing of data:

In a separate loop located AFTER the one created in the last step.

Do not use argc in this loops condition

Calculate the mean of the numbers in the array

Display of results:

Display the mean of the numbers

Before the return 0 of your main function

Destroy the array and leave no memory on the heap

Additional requirements:

No STL containers may be used. Include only iostream and algorithm (if you wish to use copy).

No class creation and keep all code inside your one cpp file. Follow the best practices of C++ and utilize function prototypes separated from their implementation.

This is my code:

#include <iostream>

using namespace std;

void resize(int * &a, size_t& oldSize, size_t newSize);

int main(int argc, char *argv[])

{

int *dp= nullptr;

*dp = new int

*dp = 1;

delete dp;

if (dp!= nullptr)

{delete dp;}

int argi;

if (argi==1)

size_t s = 0;

/*p= new int [0];

p [0]= 4;

p [1] = 3;

p [2] = 8;

s= 4;

int *np = new int [s + 1];

copy(p, p+s, np);

delete [] p;

p= np;*/

for ( size_t argi=1 ; argi < argc ; ++argi )

{

cout << "Arg_" << argi << ":" << argv[argi] << resize<< endl;

}

double value [1] = 89;

double sum =0;

for (int i =0; i < 1; ++i )

{

sum = sum + value[i];

}

cout << " The Mean of the Number: " << sum<< endl;

return 0;

}

void resize(int * &a, size_t& oldSize, size_t newSize)

{

if (newSize <oldSize)

oldSize =newSize;

int *p = new int [oldSize];

copy (a, a + oldSize, p);

delete [] a;

a=p;

oldSize = newSize;

}

Explanation / Answer

Dynamic memory designation in C/C++ alludes to performing memory assignment physically by software engineer. Powerfully allotted memory is distributed on Heap and non-static and nearby factors get memory dispensed on Stack (Refer Memory Layout C Programs for points of interest).

What are applications?

One utilization of progressively apportioned memory is to dispense memory of variable size which isn't conceivable with compiler assigned memory aside from variable length exhibits.

The most imperative utilize is adaptability given to developers. We are allowed to dispense and deallocate memory at whatever point we require and at whatever point we don't require any longer. There are numerous situations where this adaptability makes a difference. Precedents of such cases are Linked List, Tree, and so forth.

How is it not quite the same as memory apportioned to ordinary factors?

For ordinary factors like "int a", "singe str[10]", and so forth, memory is consequently apportioned and deallocated. For progressively apportioned memory like "int *p = new int[10]", it is software engineers duty to deallocate memory when never again required. In the event that software engineer doesn't deallocate memory, it causes memory spill (memory isn't deallocated until the point that program ends).

How is memory designated/deallocated in C++?

C utilizes malloc() and calloc() capacity to designate memory powerfully at run time and uses free() capacity to free progressively assigned memory. C++ bolsters these capacities and furthermore has two administrators new and erase that play out the undertaking of distributing and liberating the memory in a superior and simpler way.

This article is about new and erase administrators.

new administrator

The new administrator signifies a demand for memory designation on the Heap. In the event that adequate memory is accessible, new administrator instates the memory and returns the location of the recently dispensed and introduced memory to the pointer variable.

Language structure to utilize new administrator: To allot memory of any information compose, the punctuation is:

pointer-variable = new information compose;

Here, pointer-variable is the pointer of sort information compose. Information compose could be any worked in information compose including cluster or any client characterized information composes including structure and class.

Precedent:

/Pointer introduced with NULL

/Then ask for memory for the variable

int *p = NULL;

p = new int;

Or then again

/Combine revelation of pointer

/and their task

int *p = new int;

Instate memory: We can likewise introduce the memory utilizing new administrator:

pointer-variable = new information type(value);

Precedent:

int *p = new int(25);

drift *q = new float(75.25);

Allot square of memory: new administrator is likewise used to dispense a block(an cluster) of memory of sort information compose.

pointer-variable = new information type[size];

where size(a variable) determines the quantity of components in an exhibit.

Precedent:

int *p = new int[10]

Powerfully designates memory for 10 whole numbers consistently of sort int and returns pointer to the primary component of the succession, which is relegated to p(a pointer). p[0] alludes to first component, p[1] alludes to second component et cetera.

dynamic

Ordinary Array Declaration versus Using new

There is a distinction between announcing a typical exhibit and designating a square of memory utilizing new. The most vital distinction is, typical exhibits are deallocated by compiler (If cluster is neighborhood, at that point deallocated when work returns or finishes). Notwithstanding, progressively allotted exhibits dependably stay there until possibly they are deallocated by software engineer or program ends.

Imagine a scenario in which enough memory isn't accessible amid runtime.

On the off chance that enough memory isn't accessible in the load to assign, the new demand shows disappointment by tossing an exemption of sort std::bad_alloc and new administrator restores a pointer. Along these lines, it might be smart thought to check for the pointer variable delivered by new before utilizing it program.

#include <iostream>
using namespace std;
int main ()
{
int* ptr = NULL;
ptr = new int;
if (!ptr)
cout << "allocation of memory failed ";
else
{
*ptr = 29;
cout << "Value of ptr: " << *ptr << endl;
}
float *r = new float(75.25);
  
cout << "Value of r: " << *r << endl;
int n = 5;
int *qr = new int[n];
  
if (!qr)
cout << "allocation of memory failed ";
else
{
for (int x = 0; x < n; x++)
qr[x] = x+1;
  
cout << "Value store in block of memory: ";
for (int x = 0; x < n; x++)
cout << qr[x] << " ";
}
delete ptr;
delete r;
delete[] qr;
  
return 0;
}