Stack overflow problem.....

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 559 Views - Last Post: 30 October 2012 - 06:55 PM Rate Topic: -----

#1 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Stack overflow problem.....

Posted 30 October 2012 - 10:03 AM

There is a stack overflow error when I run it on visual studio. How do I fix it? What is the problem? It is on the line containing: return factNminusR * factorialNminusR ( nMinusRtotal - 1 ); (line 37) or around there. Is it because the calculation is too big?


  //File Name: lab6.cpp
  //Name: Zac Carlson
  //Date: 10/29
 
  #include<iostream>
  /*#include<sys/time.h>*/
  #include<cstdlib>
 
  using namespace std;
 
  int factorialN ( int nNum ){
     int factN = nNum;
     if ( nNum == 1 ){
        return 1;
     }
     else{
        return factN * factorialN ( nNum - 1 );
     }
  }
 
  int factorialR ( int rNum ){
     int factR = rNum;
     if ( rNum == 1){
        return 1;
     }
     else{
        return factR * factorialR ( rNum - 1 );
     }
  }
 
  int factorialNminusR ( int nMinusRtotal ){
     int factNminusR;
     if ( nMinusRtotal == 1 ){
        return 1;
     }
     else{
        return factNminusR * factorialNminusR ( nMinusRtotal - 1 );
     }
  }
 
  int main(){
     int nNum, rNum, cNr, nMinusRtotal;
     cout << "Using the statistics equation c(n,r) enter a number for n: ";
     cin >> nNum;
     cout << "Now enter a number for r: ";
     cin >> rNum;
     nMinusRtotal = nNum - rNum;
     cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));
     cout << "Your calculation for c(n,r) is: " << cNr << endl;
 
  /*struct timeval stop, start;
       gettimeofday(&start, NULL);
            //Time your function here
       gettimeofday(&stop, NULL);
       cout << "Time: " << stop.tv_usec - start.tv_usec << endl;*/
  return 0;
  }



Is This A Good Question/Topic? 0
  • +

Replies To: Stack overflow problem.....

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Stack overflow problem.....

Posted 30 October 2012 - 10:09 AM

Well, it is not a stack overflow error. You are returning factNminusR * something, but you have not initialized factNminusR. That's causing the problem.
Was This Post Helpful? 0
  • +
  • -

#3 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Stack overflow problem.....

Posted 30 October 2012 - 10:14 AM

View Postaresh, on 30 October 2012 - 10:09 AM, said:

Well, it is not a stack overflow error. You are returning factNminusR * something, but you have not initialized factNminusR. That's causing the problem.


Sorry. I did fix it in the actual code just not on this post. Therefore, it should say: int factNminusR = nMinusRtotal;
I'll rewrite the code and it still causes a stack overflow:
  //File Name: lab6.cpp
  //Name: Zac Carlson
  //Date: 10/29
 
  #include<iostream>
  #include<sys/time.h>
  #include<cstdlib>
 
  using namespace std;
 
  int factorialN ( int nNum ){
     int factN = nNum;
     if ( nNum == 1 ){
        return 1;
     }
     else{
        return factN * factorialN ( nNum - 1 );
     }
  }
 
  int factorialR ( int rNum ){
     int factR = rNum;
     if ( rNum == 1){
        return 1;
     }
     else{
        return factR * factorialR ( rNum - 1 );
     }
  }
 
  int factorialNminusR ( int nMinusRtotal ){
     int factNminusR = nMinusRtotal;
     if ( nMinusRtotal == 1 ){
        return 1;
     }
     else{
        return factNminusR * factorialNminusR ( nMinusRtotal - 1 );
     }
  }
 
  int main(){
     int nNum, rNum, cNr, nMinusRtotal;
     cout << "Using the statistics equation c(n,r) enter a number for n: ";
     cin >> nNum;
     cout << "Now enter a number for r: ";
     cin >> rNum;
     nMinusRtotal = nNum - rNum;
     cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));
     cout << "Your calculation for c(n,r) is: " << cNr << endl;
 
  struct timeval stop, start;
       gettimeofday(&start, NULL);
            //Time your function here
       gettimeofday(&stop, NULL);
       cout << "Time: " << stop.tv_usec - start.tv_usec << endl;
  return 0;
  }


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,384
  • Joined: 25-December 09

Re: Stack overflow problem.....

Posted 30 October 2012 - 12:41 PM

Did you try to run your program through your debugger? Your debugger will tell you exactly where it detects the problem and you can then look at your variables at the time of the crash.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Stack overflow problem.....

Posted 30 October 2012 - 01:47 PM

I think I figured it out. I'm sorry if it was confusing Jim, but I was aware of the problem because of both putty and the debugger of Visual Studio. I just wasn't too sure on what it exactly it meant. As I assumed, it is because the line I mentioned outputs too large of memory for the memory value I assigned. If I didn't say this right, I apologize, and I would guess you get what I mean. Therefore, I need to assign a larger value to that variable. I don't know which one yet, but I will try to figure it out. If you want to help, by all means. If not, no worries, and I will write back on this if I run into issues.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,384
  • Joined: 25-December 09

Re: Stack overflow problem.....

Posted 30 October 2012 - 02:05 PM

I suggest you think about this line:
   cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));

Do you really need to call these three recursive functions on the same line. You are probably burning through your available stack memory half way thru this mess.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Stack overflow problem.....

Posted 30 October 2012 - 05:29 PM

View Postjimblumberg, on 30 October 2012 - 02:05 PM, said:

I suggest you think about this line:
   cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));

Do you really need to call these three recursive functions on the same line. You are probably burning through your available stack memory half way thru this mess.

Jim


Jim,

I was able to figure it out. I did do adjustments to make it less messy and I don't know if that was the actual cause because after I figured out the problem, it works just fine. The problem was I was putting values for n and r that would cause the solution to be negative. Once I started to put n bigger than r it works fine.

Second, about your "mess" comment. One thing that I don't like, and this may be the problem with emails and emotions through them, but to tell somebody you are trying to help that their code is a mess is not very encouraging. I could not picture my college instructor telling me or somebody else that when helping them their work is a mess. If I come off sensitive, so be it. I very well may be sensitive, but at the same time I take words pretty serious I guess you could say. I'm not saying you are good help, because you are. You've helped me with this assignment as well as a previous one. Also, last time, I complained about how I've been spoken to regarding help my post got ceased and I got the -2 reputation as you see now. Therefore, if you, or anybody does this again, I'm going to laugh as I did the last one.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,753
  • Joined: 27-December 08

Re: Stack overflow problem.....

Posted 30 October 2012 - 05:41 PM

Quote

Second, about your "mess" comment. One thing that I don't like, and this may be the problem with emails and emotions through them, but to tell somebody you are trying to help that their code is a mess is not very encouraging.

Take a step back for a minute. Nobody is coming here simply to bash you or your code. A lot of our members are very direct when helping, not to be rude, but simply so to get the point across to fix things and move onto the next problem. If somebody is doing something the wrong way, should it be encouraged or corrected? Nobody here is flaming you, or coming here simply to bash you or your code. Take it in strides and try to find the helpful advice in it all. This is the internet, after all.

Quote

Also, last time, I complained about how I've been spoken to

The last time, you called someone a dick head. Quoting from your post below.

Quote

Xupicor or DIC head, however you want to be called, the second name (nick name) goes with your attitude.


If you really feel somebody is attacking you, then you are welcome to shoot myself or another team member a PM to discuss further.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7640
  • View blog
  • Posts: 12,880
  • Joined: 19-March 11

Re: Stack overflow problem.....

Posted 30 October 2012 - 05:50 PM

View PostZacCarlson, on 30 October 2012 - 07:29 PM, said:

I'm not saying you are good help, because you are. You've helped me with this assignment as well as a previous one.


So, I'm thinking "thank you, Jim" might be a good place to go from here?
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,439
  • Joined: 29-May 08

Re: Stack overflow problem.....

Posted 30 October 2012 - 05:52 PM

macosxnerd101 did you call their code "baby" ugly?
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Stack overflow problem.....

Posted 30 October 2012 - 06:00 PM

Quote

I did do adjustments to make it less messy and I don't know if that was the actual cause because after I figured out the problem, it works just fine.


Quote

but to tell somebody you are trying to help that their code is a mess is not very encouraging.


You don't like that jim called your code messy; however, you figured out the solution after you made the code "less messey".

Don't take everything as an "attack", we simply say what will guide your to find a solution without spoon feeding it to you.
Was This Post Helpful? 1
  • +
  • -

#12 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Stack overflow problem.....

Posted 30 October 2012 - 06:24 PM

View Postmacosxnerd101, on 30 October 2012 - 05:41 PM, said:

Quote

Second, about your "mess" comment. One thing that I don't like, and this may be the problem with emails and emotions through them, but to tell somebody you are trying to help that their code is a mess is not very encouraging.

Take a step back for a minute. Nobody is coming here simply to bash you or your code. A lot of our members are very direct when helping, not to be rude, but simply so to get the point across to fix things and move onto the next problem. If somebody is doing something the wrong way, should it be encouraged or corrected? Nobody here is flaming you, or coming here simply to bash you or your code. Take it in strides and try to find the helpful advice in it all. This is the internet, after all.

Quote

Also, last time, I complained about how I've been spoken to

The last time, you called someone a dick head. Quoting from your post below.

Quote

Xupicor or DIC head, however you want to be called, the second name (nick name) goes with your attitude.


If you really feel somebody is attacking you, then you are welcome to shoot myself or another team member a PM to discuss further.


Look back at the post and try to understand why I said it. I was not calling him a dick head. I originally thought "DIC head" was what he purposely made for his name. Then I saw that it's the name for everybody. Which concludes that this site should think about changing that portion. I hope this makes sense.

View Postjon.kiparsky, on 30 October 2012 - 05:50 PM, said:

View PostZacCarlson, on 30 October 2012 - 07:29 PM, said:

I'm not saying you are good help, because you are. You've helped me with this assignment as well as a previous one.


So, I'm thinking "thank you, Jim" might be a good place to go from here?


And your team talking about not spoon feeding me? What is this garbage? Are you my mother, my father?
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7640
  • View blog
  • Posts: 12,880
  • Joined: 19-March 11

Re: Stack overflow problem.....

Posted 30 October 2012 - 06:26 PM

*
POPULAR

Just a suggestion, dude. I wasn't aware that common courtesy was "garbage".
Was This Post Helpful? 5
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,753
  • Joined: 27-December 08

Re: Stack overflow problem.....

Posted 30 October 2012 - 06:31 PM

Just to add- the whole purpose of DIC is to help others learn how to program. This involves a pedagogic approach to teach people how to fish, so to speak, rather than feeding them for a day. Part of this involves constructively criticizing code. Having read through your threads, I believe this has been done by all who have attempted to help you. Again, you are welcome to shoot me a PM if you wish to discuss further.
Was This Post Helpful? 1
  • +
  • -

#15 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Stack overflow problem.....

Posted 30 October 2012 - 06:48 PM

View Postmacosxnerd101, on 30 October 2012 - 06:31 PM, said:

Just to add- the whole purpose of DIC is to help others learn how to program. This involves a pedagogic approach to teach people how to fish, so to speak, rather than feeding them for a day. Part of this involves constructively criticizing code. Having read through your threads, I believe this has been done by all who have attempted to help you. Again, you are welcome to shoot me a PM if you wish to discuss further.

I'm done with all this. Look at the name, DIC, say it out loud with out saying it as it should be, as in, "D", "I, "C". Say it as a whole, "DIC". Do you see my point? If you don't, I'm sorry to hear that. Have a nice day.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2