LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2010, 08:07 PM   #31
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

Quote:
Originally Posted by adenial View Post
...
how in the world you can write your own debugger...
great job.
It is not trivial... but not something that is not possible only by the gods.

Get a spine... figure out the basics. Even after you become cognizant of some of the fundamentals, K&R may quake in their boots.

Actually, K&R are probably collecting SS benefits and laughing at you and me.

Well. at least I would be. Who gives one iota about the worries of how to deduce "if this, otherwise if that, otherwise ..."
 
Old 10-09-2010, 08:39 PM   #32
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by johnsfine View Post
Maybe because GDB is harder to learn than C. Maybe because when you're in the middle of learning C, you don't want to detour into learning GDB.
I don't consider the following code so hard to learn, even for a newbie.
Code:
gcc -g abc.c
gdb a.out
b main
r
n
But yes, to know that these five lines would help in determining where the control goes, one needs to read the first *few* pages (after introduction) of the (simpler) manual I pointed to him.

Quote:
Originally Posted by johnsfine View Post
So if you were expert enough to use GDB for trivial programs, GDB would work fine. But that same amount of learning in C itself would put you beyond the level that you would feel any need for GDB for such trivial programs.
GDB can be used for figuring out the control flow of the program which is IMHO enough for understanding why the output was wrong.

Quote:
Originally Posted by MTK358 View Post
Are you actually Sergei Steshenko in disguise?
Every person who tells you to read the manual looks like Sergei to you ? You still hate reading manuals as before ?

Last edited by Aquarius_Girl; 10-09-2010 at 11:39 PM. Reason: added statements
 
Old 10-10-2010, 07:11 AM   #33
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by anishakaul View Post
GDB can be used for figuring out the control flow of the program which is IMHO enough for understanding why the output was wrong.
Are we talking about the program in post #22 of this thread?

Did you see a bug in that program that would be made more understandable by seeing the flow of control? I didn't.

I don't know the intention of most of the computations, so I can't guess whether there are any bugs in the decision making or in the computations. The collection of if statements looks pretty bad when posted without CODE tags, and superficially looks like there are too few uses of else for the number and contiguity of the uses of if. But if you paste that into any tool that fixes the formatting, you can easily see that all the uses of if and all the apparent failures to use else are plausible for correct code. None of that looks particularly like a bug.

I'm assuming (because the OP gave so little detail about what was fixed in post #30) that the main problem was in the use of scanf. The output of the program was already plenty of information to see that scanf was producing unexpected results. Seeing the control flow in GDB would provide zero additional information about this problem. Using print instructions in GDB (when you know how and when they work consistently enough to trust) would be a trivial amount of extra info (beyond what the output of the program already gave) toward realizing that the use of scanf was the problem.

First rule of debugging: If you used scanf and your program doesn't work, probably the use of scanf was wrong. Check that first.

First rule of testing: If you used scanf and your program does work, probably you haven't tested the cases that reveal that your use of scanf was wrong. Use more imagination in selecting test cases.

BTW, I have a comment on most of the instances of missing else in that code. The typical example is:
Code:
    ...
    if(customer == 'u')
        discount = 7.5;
    else if(customer == 'i')
    ....
The else was missing in the actual code.

That tends to be a time/space trade off (the code averages faster with the else and smaller without it but the end result is the same).

This is a situation with no reason to care about either the time or the space of having vs. skipping that else. If you did care, then depending on the cache line alignment and cache status of the code and the expected distribution of values, the smaller apparently slower code without the else might actually be faster. So if I correctly understood dwhitney67's comment, I disagree. There was no reason to prefer if-else.

Quote:
Originally Posted by dwhitney67 View Post
Exercise your knowledge of the if-else construct a bit more. I see that you are aware of it, but you fail to use it when checking the value of the microprocessor type (mptype) and the customer value.
But the other part of that was a more reasonable suggestion:

Quote:
Alternatively, you could also look into using a switch directive.
I don't personally find the use of switch more readable in this example than the mutually exclusive if conditions. But I can understand the viewpoint that does see switch as more readable/maintainable.

Last edited by johnsfine; 10-10-2010 at 07:30 AM.
 
Old 10-10-2010, 07:38 AM   #34
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
John

I have no intention of arguing with you since I know you are more knowledgeable and experienced than me. I didn't read that code since it is not in code tags and neither I did see any efforts from the OP's side. You may be correct in knowing that scanf was at fault but OP didn't know this, so he could have given GDB a try and then posted here saying I tried gdb but couldn't understand anything. That would have shown some efforts from OP and then I might have tested the code myself and explained him the things.

When I started with C, I used to run GDB the way I mentioned to confirm that I have written the logic correctly. and after that confirmation I used to check for the silly mistakes like scanf/printf etc !
 
Old 10-10-2010, 07:44 AM   #35
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by anishakaul View Post
Every person who tells you to read the manual looks like Sergei to you ? You still hate reading manuals as before ?
Maybe.

I just think it's a bit extreme to have a person just beginning with programming to start using GDB, but...
 
0 members found this post helpful.
Old 10-10-2010, 09:46 AM   #36
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Are you actually Sergei Steshenko in disguise?
Still mentally lazy preferring to ask here instead of reading the documentation first ?
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need "usbinstall" answers _before_ I begin... molhar Slackware 3 05-08-2010 02:30 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
kdevdesigner Help! "QPainter::begin: Cannot paint null pixmap" LBS Linux - Software 4 05-18-2005 05:11 PM
What is the cause of the "L" at the begin of boot and how to solve it? captainstorm Linux - Newbie 4 07-22-2003 03:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration