I know when using Dynamic memory I should use the delete operator to de-allocate memory after using the new operator. Using the method MSDN gives to check for memory leaks I have several memory I need to de-allocate.If I place the delete at line 58 the only output I get in the following code is the first cout statements at line 49 & 50. If I place it at the end at line 86 the program executes but does not execute the system ("Pause"). I could use a push in the right direction to resolve this.
Detected memory leaks!
Dumping objects ->
{138} normal block at 0x004C49D8, 16 bytes long.
Data: < & @ > 00 00 00 00 00 26 BD 40 CD CD CD CD CD CD CD CD
{137} normal block at 0x004C4988, 16 bytes long.
Data: < t @ > 00 00 00 00 00 74 C3 40 CD CD CD CD CD CD CD CD
{136} normal block at 0x004C4938, 16 bytes long.
Data: < 6 @ > 00 00 00 00 00 36 AA 40 CD CD CD CD CD CD CD CD
{135} normal block at 0x004C48E8, 16 bytes long.
Data: < * @ > 00 00 00 00 00 2A AB 40 CD CD CD CD CD CD CD CD
{134} normal block at 0x004C4898, 16 bytes long.
Data: < + @ > 00 00 00 00 00 2B BE 40 CD CD CD CD CD CD CD CD
{133} normal block at 0x004C4848, 16 bytes long.
Data: < @ > 00 00 00 00 00 EE A0 40 CD CD CD CD CD CD CD CD
{132} normal block at 0x004C47F8, 16 bytes long.
Data: < @ > 00 00 00 00 00 8D C2 40 CD CD CD CD CD CD CD CD
{131} normal block at 0x004C47A8, 16 bytes long.
Data: < @ > 00 00 00 00 00 A6 BC 40 CD CD CD CD CD CD CD CD
{130} normal block at 0x004C4758, 16 bytes long.
Data: < @ > 00 00 00 00 00 E4 96 40 CD CD CD CD CD CD CD CD
{129} normal block at 0x004C4708, 16 bytes long.
Data: < D @ > 00 00 00 00 00 44 90 40 CD CD CD CD CD CD CD CD
Object dump complete.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <iomanip>
#include "Resistor.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
using namespace std;
int Resistor::objectCount=0;
void SortResistance(Resistor **resistor)
{
int i, j, flag = 1; // set flag to 1 to start first pass
int numLength = 10;
Resistor *temp; //pointer that will help to swap the pointers
for(i = 1; (i <= 10) && flag; i++)
{
flag = 0;
for (j=0; j < (9); j++)
{
if (resistor[j+1]->getNominalResistance() <resistor[j]->getNominalResistance())
{
temp = resistor[j]; // swap elements
resistor[j] = resistor[j+1];
resistor[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
return;
}
int main()
{
Resistor *ptrresistor[10];
//char chr;
bool maxNumRes = false;
//Displaying Welcome screen to the user
cout<<" 10 Resistor objects will be created based on random numbers.\n";
cout<<"**********************************************************************\n\n";
int i =0;
while(!maxNumRes)
{
if(Resistor::objectCount <10 ) //Checking whether the static MaxSize member is reached its maximum i.e. 10
{
ptrresistor[i]=new Resistor();
//delete [] ptrresistor;
}
else
maxNumRes =true;
i++;
}
//cout<<"Following 10 objects were sorted lowest to highest.\n\n";
for(int j=0;j<Resistor::objectCount ;j++)
{
cout<< setw (5) << left<<"Resistor "<< setw(3)<< left<<j+1
<<"Nominal Resistance:\t"<<ptrresistor[j]->getNominalResistance()<<"\n"<<endl;
}
SortResistance(&ptrresistor[0]);
cout<<"\n**********************************************************************\n";
cout<<"Resistor list sorted lowest to highest.\n\n";
for(int j=0;j<Resistor::objectCount ;j++)
{
cout<< setw (5) << left<<"Resistor "<< setw(3)<< left<<j+1
<<"Nominal Resistance:\t"<<ptrresistor[j]->getNominalResistance()<<"\n"<<endl;
}
//delete [] ptrresistor;
_CrtDumpMemoryLeaks();
system("pause");
return 0;
}
//Resistor .cpp
#include "Resistor.h"
#include <string>
using namespace std;
//Constructor
Resistor::Resistor(double p_cap,double p_tol)
{
nominal_resistance = 1000 + rand()%((10000 - 1000) +1);
//tolerance=1 + rand()%((20 - 1) +1);
objectCount = objectCount+1;
}
//functions
void Resistor::setNominalResistance(double p_cap)
{
nominal_resistance=p_cap;
}
//void CResistor::setTolerance(double p_tol)
//{
// tolerance=p_tol;
//}
double Resistor::getNominalResistance()
{
return nominal_resistance;
}
//double CResistor::getTolerance()
//{
// return tolerance;
//}
Resistor::~Resistor()
{
objectCount=objectCount-1;
}
|