### Help with a perfect number detector using pointers

Howdy folks, like the title says I have a homework assignment to code a perfect number detector in range 0 to a number input by the user.

just in case: <http://en.wikipedia.org/wiki/Perfect_number>

It runs, but sometimes freezes while checking numbers nearby 50, 250, and 450. Even stranger, I input 100, got a glitch; I tried again, and it ran right through. Do I have pointer issues? Math issues? Visual Studio issues?

Code below (I've left line 23 uncommented so you can see where it freezes while running.)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101`` ``````#include using namespace std; bool is_perfect(int &num); int main() { int *num; int *perfects; int *p_count; num = new int; p_count = new int; perfects = new int; *p_count = 0; int *i; i = new int; cout << "Please enter the number to check: "; cin >> *num; for ((*i)=1; (*i)<=(*num); (*i)++) { cout << "inside main loop @ " << *i << endl; if (is_perfect(*i)==true) { cout << " perfect found!" << endl; *(perfects + (*p_count)) = (*i); (*p_count)++; } } cout << "**************************" << endl; cout << "Perfect numbers <" << *num << endl; for ((*i)=0;(*i)<(*p_count); (*i)++) { cout << *(perfects + (*i)) << " "; } cout << endl << "**************************"; cin.get(); cin.get(); } bool is_perfect(int &num) { int *n; n = # int *divs; int *L_divs; divs = new int; L_divs = new int; *L_divs = 0; int *i; i = new int; //cout << "is_perfect() evaluating " << *n << "..." << endl; for ((*i)=1; (*i)<(*n); (*i)++) { if (((*n)%(*i))==0) { //cout << " adding divisor " << *i << endl; *(divs+*L_divs) = *i; (*L_divs)++; } } int *sum; sum = new int; *sum = 0; //cout << " before adding *sum=" << *sum << endl; for ((*i)=0; (*i)<(*L_divs); (*i)++) { (*sum) += *(divs+*i); //cout << " sum is now " << *sum; } //cout << endl << " sum of divisors: " << *sum << endl; if ((*sum) == (*n)) { //cout << " returns true" << endl; return true; } else { //cout << " returns false"; return false; } delete n; delete divs; delete L_divs; delete sum; delete i; }``````

¿why the fuck are you using dynamic allocation?

`*(perfects + (*p_count)) = (*i);` is accessing out of bounds.
Well I'd recommend not using pointers like that for starters.
Believe me, I'd love to not be using dynamic allocation like this, but it's part of the assignment.

`*(perfects + (*p_count)) = (*i);`
This line must be working or else it wouldn't ever return proper output...

Is there some property of declaring `new` space for something that I'm clearly oblivious of? I'm kind of stumped, it only freezes one in five times with input: 100, but always when input: 1000.
 ``12`` ``````*(perfects + (*p_count)) = (*i); This line must be working or else it wouldn't ever return proper output... ``````

Well, following:
 ``123`` ``````int x[5]; x[8] = 10; cout << x[8];``````

works too, but still can lead to unexpected behavior.
Right so its some dynamic glitch we didn't cover in lecture cuz its a beginner class. Cool!

Thanks for helping guys!
No, it's you writing out of bounds, which causes undefined behaviour.
`perfects = new int;` ¿how much space are you reserving?
Topic archived. No new replies allowed.