Question:
C++ help: Pointer to Dynamic array-will not clear?
2009-05-06 05:32:50 UTC
The program asks for the array size, dynamically allocates an array with that size, and finds average, median, and mode.
It works on the first run, but when the program repeats it adds up the average or the allocated memory between runs. When I try clearing the memory using 'delete [] prt' I get an error:

"Windows has triggered a breakpoint in MovieStat.exe.
This may be due to a corruption of the heap, which indicates a bug in MovieStat.exe or any of the DLLs it has loaded."

#include
#include
using namespace std;

void selsort(int *, int);
int statmode(int *, int);
int countof(int *,int, int);

int main()
{
char again=0;
int Stud; //Stores number of array elements
int average;
int avertotal=0; //find total for average
int *studprt; //pointer that holds array
int mode=0;

do{
cout<<"how many students were surveyed?"<cin>>Stud;
studprt=new int(Stud);
cout<<"Enter the number of movies seen for each student."<cout<for(int i=0;i{
cout<<"How many movies has Student "<<(i+1)<<" seen?"< cin>>studprt[i];
avertotal+=*(studprt+i);
}

average=avertotal/Stud;
cout<<"Average: "<selsort(studprt,Stud);
statmode(studprt,Stud);

delete [] studprt; <------This gives me an error.
studprt=0; <------I try to reset the pointer

if(average>0)
cout<<"size is off"<else
cout<<"size is okay"<
cout<<"Start Over? (y or n)"<cin>>again;
}while (again != 'n');
system("pause");
return 0;
}

void selsort(int *arra,int size)
{
int val;
int index;
double median=0.0;
double middiv=0.0;
int midmod=0;
int evencal=0.0;
double oddcal=0.0;

for(int scan=0;scan<(size-1);scan++)
{
index=scan;
val=arra[scan];
for(int count=scan+1;count {
if (arra[count] {
val=arra[count];
index=count;
}
}
arra[index]=arra[scan];
arra[scan]=val;
}
//for(int i=0;i //{
//cout<<*(arra+i)<<" ";}
//cout< midmod=size%2;
cout< if(midmod==0)//even
{
middiv=size/2;
evencal=middiv;
median=(*(arra+evencal)+*(arra+(evencal-1)))/2.0;
}
else //odd
{
middiv=static_cast(size)/2.0;
oddcal=middiv+0.5;
median=oddcal;
}
cout<<"Median: "<}

int statmode(int *arra, int size)
{
//for(int i=0;i //{
//cout<<*(arra+i)<<" ";}
//cout<int mode = 0, count = 0;
int i;
for (i=0;i {
if (countof(arra,size,*(arra+i))>count)
{
mode = *(arra+1);
count = countof(arra,size, *(arra+i));
}
}
cout<<"Mode: "<return mode;
}

int countof(int *arr, int size,int value)
{
int i;
int count = 0;
for (i = 0; i < size; ++i)
{
if (*(arr+i) == value)
count++;
}
return count;
}

OUTPUT:
how many students were surveyed?
2
Enter the number of movies seen for each student.

How many movies has Student 1 seen?
2
How many movies has Student 2 seen?
2
Average: 2
Median: 2.0
Mode: 2
Start Over? (y or n)
y
how many students were surveyed?
2
Enter the number of movies seen for each student.

How many movies has Student 1 seen?
1
How many movies has Student 2 seen?
1
Average: 3
Median: 1.0
Mode: 1
Start Over? (y or n)
y
how many students were surveyed?
4
Enter the number of movies seen for each student.

How many movies has Student 1 seen?
2
How many movies has Student 2 seen?
1
How many movies has Student 3 seen?
1
How many movies has Student 4 seen?
1
Average: 2
Median: 1.0
Mode: 1
Start Over? (y or n)
Three answers:
2009-05-06 05:44:02 UTC
Remove the [] and that should fix your problem. You are telling it to delete an array of that object where you only allocated it one array of ints:



studprt=new int(Stud); // there is only one int array

delete [] studprt;
cja
2009-05-06 07:00:00 UTC
This is your problem:



    studprt=new int(Stud);



This sets studprt to a pointer to int, initialized to Stud. E.g., if Stud = 5 before the statement was executed, then after this statement *studprt = 5. You've allocated space for only 1 int, and set the contents of that location to Stud, which I don't think is what you intended.



Try changing it to this:



    studprt=new int[Stud];



to allocate Stud uninitialized ints. Then your delete statement will be correct.
vandyke
2016-10-20 04:45:09 UTC
in case you write "snack = snack + a million", you lose the unique pointer back by means of the call to new[], meaning you could now no longer call delete[], meaning you have leaked reminiscence. Edit: particular, you could subtract what you have extra and return to the pointer to the 1st element of the array, wherein case delete[] can nonetheless be observed as. it particularly is merely an invaluable form concept in C programming, to chop up uncooked regulations used to maintain dynamic storage and uncooked regulations used as iterators. Conceptually, they are 2 countless issues. besides the fact that in this actual occasion, the author forgot to jot down delete [] snack; besides. As valgrind tells me, ==11923== 152 (fifty six direct, ninety six oblique) bytes in a million blocks are certainly misplaced in loss record 4 of four notice that in case you employ C++ boxes (at the same time with vectors) or C++ clever regulations, you will no longer ought to call delete or in any different case cope with dynamic reminiscence manually. i've got under no circumstances had to apply delete in C++ at artwork.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...