![]() |
|
#1
|
|||
|
|||
Programs failing to execute...?I'm attempting to write a program for the queens problem (which I've not completed yet) but the problem is that although it has no compile-time errors but when I run the executable from the command prompt nothing happens and I return to the prompt
Please help. Regards, Netnut. |
|
#2
|
||||
|
||||
Re: Programs failing to execute...?Quote:
Hi, netnut. As far as I understood, you have done some printing to the console in your code and that's what you expect to see, right? Well, my best guess is that your program doesn't run on the branches doing the printing, and it just encountered an error and finished (perhaps due to a check you did). Anyway, without seeing your code we can only make supposition. It really doesn't matter that your code is not complete, and we don't expect (or care) it to be perfect especially in this early stage, but if you could just post what you have now, we could make a more accurate guess of what is wrong. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
Re: Programs failing to execute...?Quote:
I will answer your question with a question. (Let's have a little role-playing music please.) I have a long complicated program that has 325 functions. When I compile the program, I get no compiler errors or warning messages whatsoever. When I execute the program nothing happens. (That is, it just returns to the command prompt and does not print out anything.) Please tell me what is going on. Well, the first thing you might want to see is the code. Not all 10,000 lines of course. You might tell me to try boil it down to a smaller number of functions with fewer total lines of code that still exhibits the problem. OK, I say, here is a much reduced program: CPP / C++ / C Code:
Furthermore, I say that the other functions (init() and run()) are quite long and complex, and they contain proprietary code, so I can't post them. (I was given these functions from the Lead Software Engineer of another project, and I was told that they work correctly.) I don't see any way that they could be inhibiting the output that my program has clearly indicated. Please tell me what is going on. Why doesn't the program at least print the "goodbye" message? Well, If I won't show you the other functions, how can you help me? The answer is: you can't. Then how can I debug this blasted thing? Answer: I must do it myself. As a starter, you tell me to put print statements at the beginning and before and after each function call. Something like this: CPP / C++ / C Code:
Guess what? Now when I run the program I see the following, and then it goes back to the command line: Code:
What happened? It never returned from run(). So, what do I do now? Well, I go into run(), and I put a printf() statement at the beginning to show that it entered the routine. I sprinkle printf() statement throughout the function to see if I can tell how far it got. In particular, before any function invocations, even library functions (memcpy(), scanf(), strcat(), ... anything). I also put printf statements before any conditional statements (if, while, etc) so show what the values of the variables are at each point so that I can see, not only that the program got that far, but can show what the program is seeing as it goes through the logic. This kind of stuff is easier and quicker to do than it is to describe. It is also quicker, I claim, than posting a request for help and waiting for a helpful response. (But it's always OK to ask; I am just trying to let you see how to make best use of your time.) It is called debugging. Of all the time that I spend in programming activities, I would estimate that I spend 10% writing code and 80% debugging. (And the other 30% of my time documentating. And the other 40% of my time arguing with the Marketing department.) Of course if my programming were perfect, I might spend less time debugging. Interestingly enough, the percentages might not change much, since may be harder to find bugs in a "good" program than in an obviously "bad" program. In any case, I think that debugging skills are at least eight times as important as programming skills at any level of proficiency. Just my opinion, of course. Your Mileage May Vary. Regards, Dave P.S. The same approach that I suggested here also can give clues about what the heck is wrong with programs that exhibit other run-time errors that prevent successful completion of the program (segment fault; access violation, floating point overflow, etc.) |
|
#4
|
|||
|
|||
Re: Programs failing to execute...?Well, thanks Dave & LuciWiz (not necessarily in that order).
Maybe I titled the thread wrong. 'Cuz I wanted out of this thread was discussion as to how to avoiding these nuisances from happening. I definately did not want submit my code for reviews or to be debugged. As of now I have corrected the problem (without knowing what the problem was!) and the program is executing fine giving the expected output etc. etc. I have one more question though: Why did the program terminate in less than a second on the first instance, but when I included a single cout statement in the recursive function I had to stop it ...it was running without giving me an impression of ever stopping? |
|
#5
|
|||
|
|||
Re: Programs failing to execute...?Quote:
Maybe I disguised the purpose of my post too well. My points were: 1. You didn't give enough information for anyone to help you directly. (We could have guessed at reasons, but no one could actually debug your program without seeing more.) If you want us to give us something (help) you might have to give us more than you did (information). 2. You can sometimes get to the bottom of things yourself. (But it is always OK to ask; especially if the program got "fixed" by some technique but you really don't understand what is happining.) Try the following: CPP / C++ / C Code:
What happens? The answer is: "It depends." For some compilers and operating systems the program returns almost immediately to the command line. But the behavior is undefined (and, after all, how could any language definition, like the C Standard, require any particular behavior in cases where a buggy program causes a program stack overflow?) Now try this, modified in accordance to the debugging principles that I tried to suggest: CPP / C++ / C Code:
Run this. Don't stop the printout, just let it run. Eventually it will overflow the program stack and whatever happened in the first case will now happen. It probably took about as many recursive calls to overflow the stack as before, but now it takes longer since the program is printing out something every time. Regards, Dave |
|
#6
|
|||
|
|||
Re: Programs failing to execute...?Thanks Dave (& in that particular order
Ok, here is the code that I have developed, now I'd like suggestions from you all to optimize it (dunno why need optimization 'cuz it takes less than a second to run on my system), infact I'd like to learn to optimize (never done it before I guess) Remember: The problem is to arrange 8 queens on a 8x8 chessboard (I'll generalize later and arrange N-queens on a NxN chessboard). As far as I see many solutions generated are symmetrical, is there a way to avoid those? Would that be optimization? So that I only get "unique" soulutions? CPP / C++ / C Code:
Regards, Netnut. EDIT: This is the first time that I have posted a code I hope I did it the right way, didn't I? |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with simple C programs... Whats wrong? | ovadoggvo | C Programming Language | 4 | 19-May-2005 08:15 |
| 1 line php cover function changes result of called programs | schgid | MySQL / PHP Forum | 0 | 03-May-2005 11:15 |
| Piping external program's stderr? | teerik | C Programming Language | 2 | 01-Feb-2005 13:55 |
| help in c programs | kitin21 | C Programming Language | 4 | 08-Sep-2004 08:38 |
| Apache prompt user for download JSp instead execute them ! | Saad | Apache Web Server Forum | 0 | 09-Feb-2004 11:14 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The