473,883 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Seriously struggling with C

RG
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops (feeling embarrassed
among you elite programmers). My prof. would post program questions
and the solutions online. For practice I would try to do the problems.
I would reach to a certain point in the code, for example as far as
error trapping, but when the loop arrives, like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,I am just not proficient in it and the class is moving ahead.
Eventually i would have to look at the solution and wondering to
myself, the reason i could not think of it. What ticks me off is that
other kids are getting this stuff easily, while I am having a hard
time.Kindly advise me on what actions I shoul take. I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.
Thanks for your time and consideration.

RG

Feb 20 '06
160 4808
On 2006-02-22, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
It's not very high up on my list, either. I use printf() and
deep thought more often useful. I find stack traces useful
sometimes, but I can get those without a debugger. I also find a
debugger useful for viewing core dumps.

It's not as though I never do "low-level" programming either.
One of my projects, for example, is an entire (educational)
operating system. In developing that, I don't think I've used a
debugger more than once or twice (and I do have one that works
with it).


I'd have to second this; I wrote nearly all the guts of my instructional
operating system before the debugger was ready. Other kernels I've
worked on I've never bothered with more than a stack trace.

The debugger isn't much help on the hard problems anyhow. If you have
a well-designed and well-tested system, all the obvious bugs you can
find by simple inspection with the debugger have already been shaken
out, and what's left are more subtle interactions between subsystems,
concurrency bugs, and unexpected UB. A conventional debugger doesn't
give much leverage with these. One could envision debuggers that
might, but that'd be a research project.

--
- David A. Holland
(the above address works if unscrambled but isn't checked often)
Feb 23 '06 #91
On 2006-02-23, Ian Collins <ia******@hotma il.com> wrote:
Richard G. Riley wrote:

Littering code with printfs or the quivalent plain sucks unless its
for logging purposes and is well DEFd out : even then it can
unecessrily break p the flow and readability of the code.
I agree.
Its why debuggers exist. Only the most trivial
or tiny code can be maintained or properly examined with messy and
time consuimg printfs. printfs only show what you *think* you need to
know : not the true state of memory, locals, stacks, memory blocks.

Even the most complex code can be maintained with the aid of
comprehensive unit tests, no need for printfs. If these are done well,
you will seldom, if ever, have to use your debugger.


Tell me : when any code of any reasonable level of complexity (maybe
long code, maybe lots of calls, maybe clever optimized bit
manipulations, maybe clever equations for image manipulation, maybe
calls to less robust areas of legacs system, goes wrong, what do you
do?

Do you , as the OP said, just "think the problem away"? Me, and
especially with C, I like to step through and get a feeling : no
amount of unit tests can remove the benefit of that.

There seems to be a trend in this thread from sone to suggest that
formal testing, automated testing, big designs are all a panacea for
the debugger shy programmer. I guess this is where we would differ :
it is of parameount importance for a programmer to be fully
comfortable with his code and to see it in action (IMO of course). No
amount of specialised framework testing will remove the benefit of that.
The last C project I did - a compiler/shortcode interpreter for
a scripting language - I hardly used a debugger at all, and when I
did, it was to get a stack-trace and line-number because that
doesn't happen for free when the code went whoopsie.

If you can get away with it fine. Its certainly not something that
would generally be encouraged in any programing environment I have
been involved in.

What is 'it' in this context?

It must be a personal thing. For me the debugger is as crucual a part
as the editor : I would normally always step through the
debugger just to sniff out any issues with uninitialised stuff,
pointer run throughs etc. Its why IDEs put so much effort into the
debugger part these days.

That just shows you don't have tests.....


No it doesnt. It shows that I test as I develop using the dbgr as an
aditional aid to writing as far from bug free code as I can. Tweaking
run time parameters, examining memory, checkling loop counts etc etc
etc.

cheers,
--
Remove evomer to reply
Feb 23 '06 #92
On 2006-02-23, Rod Pemberton <do*********@so rry.bitbucket.c mm> wrote:

"Richard G. Riley" <rg***********@ gmail.com> wrote in message
news:46******** ****@individual .net...
On 2006-02-22, CBFalconer <cb********@yah oo.com> wrote:
> "Richard G. Riley" wrote:
>>
> ... snip ...
>>
>> It must be a personal thing. For me the debugger is as crucual a
>> part as the editor : I would normally always step through the
>> debugger just to sniff out any issues with uninitialised stuff,
>> pointer run throughs etc. Its why IDEs put so much effort into the
>> debugger part these days.
>
> Yes, I can vaguely remember doing such things about 25 or 30 years
> ago. I outgrew it. I find a sound design and functional breakdown
> works much better.
>
Do you write standalone SW that only you maintain?

I find it incredulous that as a programmer, a debugger isnt a very
important tool on your list.


I wrote my first program in 1981 (or was it '79?). Anyway, in all that time
through maybe fourteen languages, I've only had to use a debugger

twice.

A am truly astonished.
Once for a compiler issue and the other to track data between multiple
processes. Printf's or it's equivalent is sufficient. Since I've
essentially never needed it, I have no choice but to consider the use of a
debugger to be a serious indicator that you're doing something
wrong.
I am even more astonished.
Perhaps, you need to develop a set of coding rules or a style guide to help
correct the errors your encountering?
Rod Pemberton

So you write hundreds and hunders, maybe thousands of lines of code
and you never need a debugger to find a problem? You can keep the
entire working set in your head in complicated sitations?

I must say I have never mez someone able to work for that long without
using a debugger.

Maybe we mean something different?

--
Remove evomer to reply
Feb 23 '06 #93
Richard G. Riley schrieb:
On 2006-02-23, Ian Collins <ia******@hotma il.com> wrote:
Richard G. Riley wrote:
Littering code with printfs or the quivalent plain sucks unless its
for logging purposes and is well DEFd out : even then it can
unecessril y break p the flow and readability of the code.
I agree.
Its why debuggers exist. Only the most trivial
or tiny code can be maintained or properly examined with messy and
time consuimg printfs. printfs only show what you *think* you need to
know : not the true state of memory, locals, stacks, memory blocks.


Even the most complex code can be maintained with the aid of
comprehensi ve unit tests, no need for printfs. If these are done well,
you will seldom, if ever, have to use your debugger.


Tell me : when any code of any reasonable level of complexity (maybe
long code, maybe lots of calls, maybe clever optimized bit
manipulations, maybe clever equations for image manipulation, maybe
calls to less robust areas of legacs system, goes wrong, what do you
do?


Switch on logging and trace functionality, find the module
corrupting the data, run the tests for this module.
Then I know at which of thousands of objects it goes wrong
during which phase and can pinpoint the routine.
Looking at the code usually suffices to see what is going
wrong.
If the above is not sufficient, I may step into the whole
thing with the debugger to see whether one of the "this
point cannot be reached" points has been reached. printf
style debugging gives me the same info. Then, I discuss
with colleagues why the preconditions were not sufficient.
This is most of the time a design flaw or a problem of
unexpected consequences (which are the same). If I had to use
the debugger, the best course of action is to write
better tests or tracing.

Do you , as the OP said, just "think the problem away"? Me, and
especially with C, I like to step through and get a feeling : no
amount of unit tests can remove the benefit of that.
I have done that for years and found that I wasted valuable
time that way.
Using the debugger is nice for stepping through undocumented
legacy systems if you need one specific information but
eventually you are better off documenting the thing or throwing
it out.
Whenever I would really have needed a debugger, the situation
was too complex or unstable ("Heisenbugs ") to actually use one.

There seems to be a trend in this thread from sone to suggest that
formal testing, automated testing, big designs are all a panacea for
the debugger shy programmer. I guess this is where we would differ :
it is of parameount importance for a programmer to be fully
comfortable with his code and to see it in action (IMO of course). No
amount of specialised framework testing will remove the benefit of that.


No. I am by no means debugger shy but having to use the debugger
means that I am down to the last resort.
In truth, I aim to have to look up the more involved functions
of my debugger every time -- because the intervals are sufficiently
long.

The last C project I did - a compiler/shortcode interpreter for
a scripting language - I hardly used a debugger at all, and when I
did, it was to get a stack-trace and line-number because that
doesn't happen for free when the code went whoopsie.

If you can get away with it fine. Its certainly not something that
would generally be encouraged in any programing environment I have
been involved in.


What is 'it' in this context?
It must be a personal thing. For me the debugger is as crucual a part
as the editor : I would normally always step through the
debugger just to sniff out any issues with uninitialised stuff,
pointer run throughs etc. Its why IDEs put so much effort into the
debugger part these days.


That just shows you don't have tests.....


No it doesnt. It shows that I test as I develop using the dbgr as an
aditional aid to writing as far from bug free code as I can. Tweaking
run time parameters, examining memory, checkling loop counts etc etc
etc.


None of us claim to be superprogrammer s.
With the right sort of experience, you just learn that
there is a way of working and programming which makes the
debugger not entirely superfluous but minimizes the need
for one.
I, for one, get done more this way.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 23 '06 #94
On 2006-02-23, Dik T. Winter <Di********@cwi .nl> wrote:
In article <11************ **********@g47g 2000cwa.googleg roups.com> "Vladimir S. Oka" <no****@btopenw orld.com> writes:
Richard G. Riley wrote: ...
Its why debuggers exist. Only the most trivial
or tiny code can be maintained or properly examined with messy and
time consuimg printfs. printfs only show what you *think* you need to
know : not the true state of memory, locals, stacks, memory blocks.


You seem to completely ignore the world of deeply embedded devices.


I did not see the original, but there is more to it. Recently I have
written quite a few C programs that either gave incorrect output (blatantly
incorrect) or crashed with a "segmentati on fault". Debuggers are in these
cases generally useless (I found). In both cases the problem was

with the

Find a good tutorial on using one. This is where debuggers can be
invaluable.
logic of the program, not with errors with respect to C. In the second

Debuggers are not there to find problems with "the language". They are
there to find problems with data assignments, logic and program flow.
cases the debugger proved to be of no use at all, the only thing the
debugger (gdb) said when the error occurred was that almost none of the
variables could be accessed (the problem proved to be overly deep
recursion due to a problem with the logic). And the first was
almost
And a good debugger would hilite this very quickly if you set the
right watchpoints.
always due to a problem with the logic. That is the kind of thing a
debugger will not help. (The programs all were about combinatorial
problems.)
I would find a debugger useful here.

For instance, in one case I declared an array of int's rather than
double's. How is a debugger going to help me to find the problem?


It can and it cant. It can show you where resulting cast/assign mismatches
go wrong as you examine the data. In this case though, assuming you
hadnt cast everythign to death, the compiler should have been a help.
--
Remove evomer to reply
Feb 23 '06 #95
On 2006-02-23, Dik T. Winter <Di********@cwi .nl> wrote:
Again not received the original, that is why I respond to this.

In article <dt**********@m alatesta.hpl.hp .com> Chris Dollin <ke**@hpl.hp.co m> writes:
Richard G. Riley wrote:
In addition printfs dont give you watchpoints, breakpoints etc. I cant
even believe we are having this discussion to be honest, although I
suppose you're not necessarily defending it : just that it can be done
- on that we are agreed.
Oh, I /am/ defending it. In my (limited) C experience, I have not
needed to resort frequently to a C debugger. I would expect to need
one less nowadays than I used to, as well.


What I am missing here is that using breakpoints debugging code can be
*more* time consuming than using printf's to give the state. I once


Nothing is set in stone. All techniques can be more consuming if not
chosen wisely. But I can honestly say that using printfs has ever been
quicker except for in the most trivial cases.
had to debug a program I had written (80k+ lines of code). On some
machine it did not work. It appeared that on the umpteenth occurrence
of some call to some routine something was wrong. It is impossible
to detect such using breakpoints or watchpoints. Using proper
printf's
This is simply not true. Since you must have some idea where the
problem is to insert the "printf" then you have some idea where to set
your breakpoint to detect "naughty data" : then you can do a stack
trace to see where this data originated.
and scrutinising the output will get you much faster the answer to why
it did not work.


I must admit this would be slower for me.

--
Remove evomer to reply
Feb 23 '06 #96
Richard G. Riley wrote:

Even the most complex code can be maintained with the aid of
comprehensi ve unit tests, no need for printfs. If these are done well,
you will seldom, if ever, have to use your debugger.

Tell me : when any code of any reasonable level of complexity (maybe
long code, maybe lots of calls, maybe clever optimized bit
manipulations, maybe clever equations for image manipulation, maybe
calls to less robust areas of legacs system, goes wrong, what do you
do?

Fix the test that breaks, or failing that, add a test that breaks then
fix the problem. If you do TDD well, you will have as near to 100% test
coverage as you can get.
Do you , as the OP said, just "think the problem away"? Me, and
especially with C, I like to step through and get a feeling : no
amount of unit tests can remove the benefit of that.
If you have developed test first, you know and trust your code. Each
line as been added to pass a test, so you don't have to step through it.
When I started TDD, I used to run my tests form within the debugger so
I could step through if required. Over time I found I wasn't using the
debugger at all,so I stopped using it.
There seems to be a trend in this thread from sone to suggest that
formal testing, automated testing, big designs are all a panacea for
the debugger shy programmer. I guess this is where we would differ :
it is of parameount importance for a programmer to be fully
comfortable with his code and to see it in action (IMO of course). No
amount of specialised framework testing will remove the benefit of that.
The best way for a programmer and more importantly, his customer, be
fully comfortable with your code is to have a complete set of automated
tests. Nothing else gives you the confidence to refactor code.

That just shows you don't have tests.....


No it doesnt. It shows that I test as I develop using the dbgr as an
aditional aid to writing as far from bug free code as I can. Tweaking
run time parameters, examining memory, checkling loop counts etc etc
etc.

These aren't what I'd call tests, my tests must be automatic.

--
Ian Collins.
Feb 23 '06 #97
On 2006-02-23, Ian Collins <ia******@hotma il.com> wrote:
Richard G. Riley wrote:

Even the most complex code can be maintained with the aid of
comprehensiv e unit tests, no need for printfs. If these are done well,
you will seldom, if ever, have to use your debugger.

Tell me : when any code of any reasonable level of complexity (maybe
long code, maybe lots of calls, maybe clever optimized bit
manipulations, maybe clever equations for image manipulation, maybe
calls to less robust areas of legacs system, goes wrong, what do you
do?

Fix the test that breaks, or failing that, add a test that breaks then
fix the problem. If you do TDD well, you will have as near to 100% test
coverage as you can get.
Do you , as the OP said, just "think the problem away"? Me, and
especially with C, I like to step through and get a feeling : no
amount of unit tests can remove the benefit of that.

If you have developed test first, you know and trust your code. Each
line as been added to pass a test, so you don't have to step through it.
When I started TDD, I used to run my tests form within the debugger so
I could step through if required. Over time I found I wasn't using the
debugger at all,so I stopped using it.


Hmm. Personal thing. I could never do this. Anythign half complex with
optimised pointer usage would always see me examining every last thing
for potential "x+1" overrruns.
There seems to be a trend in this thread from sone to suggest that
formal testing, automated testing, big designs are all a panacea for
the debugger shy programmer. I guess this is where we would differ :
it is of parameount importance for a programmer to be fully
comfortable with his code and to see it in action (IMO of course). No
amount of specialised framework testing will remove the benefit of that.

The best way for a programmer and more importantly, his customer, be
fully comfortable with your code is to have a complete set of automated
tests. Nothing else gives you the confidence to refactor code.


I disagree on this I must say. I find automated tests tend to give a
false sense of security. They do contribute of course : but are often
seen as an indicator of infallability. SOmetimes betetr to stick a
monkey at the keyboard with a BIG hammer!!!! :)

That just shows you don't have tests.....


No it doesnt. It shows that I test as I develop using the dbgr as an
aditional aid to writing as far from bug free code as I can. Tweaking
run time parameters, examining memory, checkling loop counts etc etc
etc.

These aren't what I'd call tests, my tests must be automatic.

--
Remove evomer to reply
Feb 23 '06 #98
Richard G. Riley wrote:

(Replying to Rod Pemberton)
So you write hundreds and hunders, maybe thousands of lines of code
and you never need a debugger to find a problem? You can keep the
entire working set in your head in complicated sitations?


I can't, of course, speak for Rod, but I currently work within a codebase
of over 100_000 lines (of Java, so the issues are somewhat different)
which is co-maintained by four other people and used by a significant number
of users (I'd be more specific but it's open-source so how can you tell
who's using it?).

While there /have/ been a couple of times when I've had to resort to
the (Eclipse) debugger to track down a problem, it's by no means
routine. As for "keep the entire working set in your head in complicated
sitations", well, I dunno. Is that what I'd need to do? I try not to
end up in complicated situations.

I suspect we have a domain issue as well as a style issue.

--
Chris "was stirred, now shaken" Dollin
RIP Andreas "G'Kar" Katsulas, May 1946 - February 2006
Feb 23 '06 #99
Richard G. Riley wrote:

If you have developed test first, you know and trust your code. Each
line as been added to pass a test, so you don't have to step through it.
When I started TDD, I used to run my tests form within the debugger so
I could step through if required. Over time I found I wasn't using the
debugger at all,so I stopped using it.

Hmm. Personal thing. I could never do this. Anythign half complex with
optimised pointer usage would always see me examining every last thing
for potential "x+1" overrruns.

I guess you've never tried TDD?
The best way for a programmer and more importantly, his customer, be
fully comfortable with your code is to have a complete set of automated
tests. Nothing else gives you the confidence to refactor code.

I disagree on this I must say. I find automated tests tend to give a
false sense of security. They do contribute of course : but are often
seen as an indicator of infallability. SOmetimes betetr to stick a
monkey at the keyboard with a BIG hammer!!!! :)

The sense isn't false if the tests are good and written first. In my
opinion, test added after the code is written are second rate.

--
Ian Collins.
Feb 23 '06 #100

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2102
by: Stan | last post by:
Hey everyone, I've got a computer science class and we're working on C++. I am struggling with nested loops and have a really simple assignment, but I can't get it to run the loop. I need to run a loop that has someone guess at a number 5 times and if they get 45, it tells them how many guesses they needed. It also has to tell them if they leave the range of guesses from 0 to 100. I need help. Can somebody help me please!!!
4
2010
by: Rowan | last post by:
Hi there, it's me again. I am having trouble with a view again. I am trying to do a calculation, but there are some checks that need to be taken into consideration. Maybe a view is not the right way to deal with this. I don't know. This is the beginning of my query. SELECT coalesce(f.filenumber, i.filenumber) as filenumber, i.InvoiceNumber, i.InvoiceValue, il.lineid, MPF = .21 * (il.UnitCost * il.UnitQty + il.AddMMV - il.MinusMMV -...
26
6772
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() { char *ptr;
4
5625
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can do a bsearch on the long member (nKey) but I am struggling to do a search using the string member. The code I am running appears below. It doesn't crash or anything. It is just that when I do the last bsearch using "192.168.1.3" I SHOULD find...
5
4067
by: | last post by:
I am really struggling to conceptually understand classes in C# especially the use of the strange keywords 'static' 'void' and 'override'.... Is there a 'idiots' way of understanding these concepts broadly before even touching a line of code.... Thanks Jason
2
1281
by: mjeaves | last post by:
Hello there, Hope someone can help. I have data arriving in a table, a Cron job is triggered when the data arrives. ---- table_1 id (unique)
97
5589
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in the first place. Anyone else heard about this development? The Master
0
1050
by: Steve | last post by:
I'm struggling to get proper runtime and designtime dpi values. In the forms .designer.vb I've added the following code after InitializeComponent() Me.AutoScaleDimensions = New System.Drawing.SizeF(96.0F, 96.0F) Me.AutoScaleMode = Windows.Forms.AutoScaleMode.Dpi And in my code behind the form, I've added,
11
22896
by: briankind | last post by:
Hello i have these radio buttons and i wish to have the corresponding textboxes to be visible when the radiobutton is selected. any Help? snippets. thanks thanks in adv Bry
0
9933
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11125
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9568
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.