unix/C++ segmentation fault

Reply

Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

unix/C++ segmentation fault

 
0
  #1
Jan 19th, 2005
hi there. i am new to C++ AND unix. this assignment that i have is really really easy, but the specifics of C++ and getting around in Unix is making it really hard and time consuming. i have a really small program that should be working, but i am getting the wrong output AND at the end of the output i get a Segmentation fault.

i have a function called series that is declared and works fine. i have inserted a cout<< to check that the function is working right. but i am still getting the wrong output AND this error. am i doing something illegal? using the function result as the array index? i know i am not telling you much about the code, i am just wondering if there are any GLARING unix or C++ no-nos that anyone sees.

  1. int main (){
  2. const int MAX_NUM = 60;
  3.  
  4. const int MAX_CALLS = 41;
  5.  
  6. int num_calls [MAX_CALLS];
  7.  
  8. int k = 0;
  9.  
  10. for(int i=1; i<=MAX_NUM; i++){
  11. num_calls[i] = 0;
  12. cout<< series(i) <<endl;
  13. if ((series (i)) < MAX_CALLS){
  14. num_calls[series (i)]++;
  15. }
  16. else{
  17. k++;
  18. }
  19. }
  20.  
  21. for(int l=1; l<MAX_CALLS; l++){
  22. cout<< l << " " << num_calls[l] <<endl;
  23. }
  24.  
  25. cout<< "and " << k << "numbers have series length greater than 40" <<endl;
  26. }

thanks
crq
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 50
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: unix/C++ segmentation fault

 
0
  #2
Jan 19th, 2005
Hi,

First, and this is a style thing... I hate it when people place the brace { on the same line as the expression... just does not look nice to me.

Adjusted:


  1. int main ()
  2. {
  3. const int MAX_NUM = 60;
  4.  
  5. const int MAX_CALLS = 41;
  6.  
  7. int num_calls [MAX_CALLS];
  8.  
  9. int k = 0;
  10.  
  11. for(int i=1; i<=MAX_NUM; i++)
  12. {
  13. num_calls[i] = 0;
  14. cout<< series(i) <<endl;
  15. if ((series (i)) < MAX_CALLS)
  16. {
  17. num_calls[series (i)]++;
  18. }
  19. else
  20. {
  21. k++;
  22. }
  23. }
  24.  
  25. for(int l=1; l<MAX_CALLS; l++)
  26. {
  27. cout<< l << " " << num_calls[l] <<endl;
  28. }
  29.  
  30. cout<< "and " << k << "numbers have series length greater than 40" <<endl;
  31. }


I notice that you declare main() to be of type INT, and you do not return any number to the OS. I wonder if that is hanging you up.

I would have tried, but you didn't bring along your whole program, and I wasn't about to code it further. I couldn't compile because I had no idea what series is, and at 11:30pm, I am not to creative.

Christian
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: unix/C++ segmentation fault

 
0
  #3
Jan 19th, 2005
THANKS for the style tips. i agree with you. i like lots of spacing in my coding. however, i have been beaten over the head with doing it the exact opposite ....

  1. for(int i=1; i<MAX_CALLS; i++){ h[i]=0} ....
  2. ...........
  3.  
  4. .......
that stuff makes me crazy, but it seems to be the going style where i am. anything else is a bit "novice" or something ... silly, though it may be.

i think i have figured it out, though. i had some uninitialized array stuff that was making my code not work ... and yes i need to check on the number part. either i left it off when i copied and pasted ... or i lost it somewhere in my coding. i am so new at unix, that i don't know how to go to my file, and open it in the editior. all i know how to do is VIEW the file, copy it. then open a text editor window and paste it in there EVERY time i want to make a change after i compile and run ... ARRRRGGGHHH!!

thanks, though.
crq
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: unix/C++ segmentation fault

 
0
  #4
Jan 19th, 2005
>I hate it when people place the brace { on the same line as the expression
Get over it. Style issues are a matter of opinion, and you shouldn't push your opinions on other people.

>I wonder if that is hanging you up.
That's legal C++. 0 will be returned by default if the compiler supports that feature from the standard. Of course, if Turbo C++ v.old_as_dirt is the OP's compiler, it's undefined behavior.

>cout<< series(i) <<endl;
What is series? Is it a type? Is it a function? Most likely your problem is in the code that you neglected to post.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 3
Reputation: sykobag is an unknown quantity at this point 
Solved Threads: 0
sykobag sykobag is offline Offline
Newbie Poster

Re: unix/C++ segmentation fault

 
0
  #5
Jan 19th, 2005
In the unix that I used last semester in school the editor was called "pico" just type that on the command line and the text editor comes up. I don't know if that is standard unix or if it was on the server that I was using but you can always give it a shot.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: unix/C++ segmentation fault

 
0
  #6
Jan 19th, 2005
thanks for replying!

i actually found the problem ... and it was a goof-up silly mistake. i was doing things in an array element that hadn't been initalized. i am still in java mode it seems (elements of int arrays are by default initalized to 0). somehow that was producing the segmentation fault. i still don't know what that term means, but it turned out to be something silly. i am so unfamiliar with C++ and with Unix that i was sure my mistake was the result of something that i wouldn't know to do.

oh, series is a function. was declared above. i had tested it and i had the problem isolated to 2 lines because of the cout<< calls that i had going.

thanks for the help .. i am sure i will need more in the coming weeks.

thanks
crq
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: unix/C++ segmentation fault

 
0
  #7
Jan 19th, 2005
Originally Posted by sykobag
In the unix that I used last semester in school the editor was called "pico" just type that on the command line and the text editor comes up. I don't know if that is standard unix or if it was on the server that I was using but you can always give it a shot.
hey, sykobag,

i am using pico too. i type pico and a blank window comes up. the problem that i have is this ... when i quit a session, save my file and want to come back to it. if i log back on and type pico, i get a blank editor. how do i call pico for the file that i want to edit instead of calling a blank window each time?

thanks
crq
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 3
Reputation: sykobag is an unknown quantity at this point 
Solved Threads: 0
sykobag sykobag is offline Offline
Newbie Poster

Re: unix/C++ segmentation fault

 
0
  #8
Jan 19th, 2005
Originally Posted by crq
hey, sykobag,

i am using pico too. i type pico and a blank window comes up. the problem that i have is this ... when i quit a session, save my file and want to come back to it. if i log back on and type pico, i get a blank editor. how do i call pico for the file that i want to edit instead of calling a blank window each time?

thanks
crq
at the bottom of the pico window there should be a load from file or something its been a while since i used it but it will load from a file all of the text you edited before
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: unix/C++ segmentation fault

 
0
  #9
Jan 19th, 2005
Originally Posted by sykobag
at the bottom of the pico window there should be a load from file or something its been a while since i used it but it will load from a file all of the text you edited before
SUPER!! i will try that. it would be a real time saver. i have been copying and pasting and opening and closing ... a real pain.

THANKS!

crq
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: unix/C++ segmentation fault

 
0
  #10
Jan 19th, 2005
if you're opening pico from a command line, couldn't you just type the path to the file as an argument?

pico /path/to/file.c
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC