Memory Problem part 2

D

Debaser

Okay, I'm one error away from making this work. Here's my main():

int main(void)
{
size_t x;
long y;
long samples[] = { 10, 50, 100, 500, 1000, 5000, 10000, 50000,
100000, 500000, 1000000 };

/* Seed the random number generator */
srand(time(0));

/* Run test */
for(x = 0; x < sizeof(samples)/sizeof(long); x++) {

/* Allocate memory */
long *nums = new long[samples[x]];

/* Fill array to be sorted with random numbers */
for (y = 0; y < samples[x]; y++) {
nums[y] = rand() % samples[x];
}
cout << "Insertion Sort Time with '" << samples[x] << "'
elements: "
<< insertion_sort(nums, samples[x]) << endl;
.....

many other sort algorithms follow with the exact same call...
Yes, I realize it's costly to fill the array each time, but the speed
of main() is not an issue...it's just a HW assignment. Just for the
record: I know better. Then at the end of the for loop we have:

.....
/* Fill array to be sorted with random numbers */
for (y = 0; y < samples[x]; y++) {
nums[y] = rand() % samples[x];
}
cout << "Bucket Sort Time with '" << samples[x] << "'
elements: "
<< bucket_sort(nums, samples[x]) << endl;

/* Debug */
for (int z = 0; z < samples[x]; z++ )
cout << nums[z] << "\t";
cout << endl;

/////////// The numbers print out sorted nums fine but the delete call
////////// crashes the program....WHY????

delete [] nums;

}
return EXIT_SUCCESS;
}


Please overlook code efficiency or cryptic variable names. This was
orginally written in C and the same thing happened with free() using
calloc() instead of new.

I apologize in advance for my ignorance.

-Mike-
 
V

Victor Bazarov

Debaser said:
Okay, I'm one error away from making this work.

Heheh... I would be a millionaire if I had a nickel every time I heard
myself say that...
Here's my main():

int main(void)
{
size_t x;
long y;
long samples[] = { 10, 50, 100, 500, 1000, 5000, 10000, 50000,
100000, 500000, 1000000 };

/* Seed the random number generator */
srand(time(0));

/* Run test */
for(x = 0; x < sizeof(samples)/sizeof(long); x++) {

/* Allocate memory */
long *nums = new long[samples[x]];
[...]

/////////// The numbers print out sorted nums fine but the delete call
////////// crashes the program....WHY????

Are you sure that the value of 'nums' is the same you obtained from using
'new[]'? The only way to tell is to preserve it and compare just before
the deletion.

If it *is* the same, you're very likely crossing the array boundary
somewhere, in one of the functions where you pass the 'nums' array, and
very likely with a negative index (like -1 or -2), which causes the heap
control block to be written over. The heap manager then tries to get the
info from the screwed up control block and fails/bails.
delete [] nums;

}
return EXIT_SUCCESS;
}


Please overlook code efficiency or cryptic variable names. This was
orginally written in C and the same thing happened with free() using
calloc() instead of new.

Probably has the same invalid memory access bug...
I apologize in advance for my ignorance.

Don't.

V
 
D

Debaser

Thanks Victor for being so friendly. There is one sort algorithm that
might be doing what you say. b* contains the sorted array. Check this:

....
/* put back into original array */
for (x = 0; x < inSize; x++) {
input[x] = b[x];
}

delete [] b;
delete [] c;
....

I thought I was transferring values, not addresses. See part 1 for
beginning of this function (counting_sort). I'll keep looking for
fishy equality statements.

Thanks again,
-Mike-
 
V

Victor Bazarov

Debaser said:
Thanks Victor for being so friendly. There is one sort algorithm that
might be doing what you say. b* contains the sorted array. Check this:

...
/* put back into original array */
for (x = 0; x < inSize; x++) {
input[x] = b[x];
}

delete [] b;
delete [] c;
...

I thought I was transferring values, not addresses. See part 1 for
beginning of this function (counting_sort). I'll keep looking for
fishy equality statements.

No, this part looks OK. You *are* transferring values here, AFAICS.

You could definitely find the problem if you write your own "array" class
which will check the index before letting you access the element.

Or use some kind of tool which will catch those out-of-bounds accesses,
especially writes.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top