🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.apple2.gno
Article #268

Re: Crash............

#268
From: Amrit Chauhan
Date: Mon, 16 Nov 1992 16:43
107 lines
4661 bytes
Soenke Behrens writes
> chauhan@aristotle.ils.nwu.edu (Amrit Chauhan) writes:

> >I am in the process of learning C, and obviously, I have some really dinky C
> >programs.  Well, this one crashes...in very weird ways.  Take a look:

> Ok, let's have a look.

I'm slowly opening my eyes :)

> >/* prob8.c -- Programming Exercises (Chapter VI, Problem 8) */
> >#include <stdio.h>
> >#define MAX 7
> >main()
> >{

> Don't do that. It's not illegal, but better be explicit. I.e.

> int
> main (int argc, char *argv[])
> {

> even if you do not use argc and argv.

Hmm, I should probably get in the practice of doing this.  What
would I do if I wasn't writing a shell program?  I mean, would you
still need to be explicit about what gets passed to main?  C Primer
Plus never really gets into this (now on Chapter 7 and haven't seen
anything on this yet...but there has been talk of it later in the
book).  If I was writing say a desktop program, what would I use the
parameters of main for?

> >        int i, integers[MAX];
> >        printf("Please enter 8 integer values below:\n");

> Aha. Integers is an array with 7 elements, not 8. Note: with
> int array[n] you get an array of n elements, which can be accessed using
> array[0] through array[n-1].

> So, what you are doing is trash memory.

I see.  I was thinking that I had 8 integer values and I had 0-7 array
locations, but I didn't express that in my loop...I reference i[8] which
is trash....hmmm, I wonder why I didn't catch this knowing that I could
only go from 0-7.  Well, thanks for pointing that out.

> >        for(i = 0; i <= MAX; i++)
> >                scanf("%d", &integers[i]);

> Which would be wrong, anyway. When dealing with arrays in a loop, use
> asymmetric bounds. Example:
> for (i = 0; i < n; i++)
> where n is the "size" of our array. The lower bound is exactly "on mark",
> the upper bound is one above the end of the array. The good thing about this
> is that it's intuitive (as you can use n as bound as opposed to n-1). There
> are more advantages, I won't go into them.

Yeah, I've seen some already.  I dont' know what I was thinkin' :)  It must
have been because I was tired...yeah, that's the ticket.

> I strongly recommend the following book to you: C Traps and Pitfalls, by  
Andrew
> Koenig, ... umm ... I gave it to a friend ... I think Addison Wesley.
> Also good books: C - A Reference Manual (better than K&R 2 IMHO), and  
"Writing
> Efficient Programs" by Jon Louis  Bentley, Prentice Hall.

Thanks for the advise...I'll look into the books...I may have to put it off
for a bit longer, but I will remember to look.

> Plus, of course, all the others, but that get's expensive :) (All the others
> being: Algorithms in C, The Art of Programming by Knuth, Programming Pearls,
> etc. etc.)

..and don't we know how big MY bank account is :)  yeah right..

> >}
> 
> And here you should return (EXIT_SUCCESS) (after including <stddef.h> of
> course), as you have defined main as returning an int (by not specifying
> anything). That's a good idea, anyway, otherwise the shell gets a random
> result back.

again, the book C Primer Plus never emphasises this until like Chapter 8 or
9.  I am thinking that I will do this from now on...but I guess the author
didn't really think much of this because the programs were so simple...that's
what I call bad teaching technique. :)

> No problem. Let me repeat: Get that C Traps and Pitfalls. If I had had that
> when learning C, I'd had saved myself a lot of trouble.

Thanks again Soenke...I appreciate this LONG post...much more than I expected.
I'll put this into effect as I mull through the next eight chapters.  Catch you  
on IRC. :)

> Soenke
> -- 
> ===========================================================================
> More than silver, more than gold, I need   !    behrenss@informatik.
> electricity. I need it for my dreams.      !    tu-muenchen.de
> (RACTER)                                   !  FIDO: 2:246/13 Soenke Behrens

--
< >  +++++++++++++++++++++++++++++  < >
<++> Amrit Chauhan		   <++>
<++> achauhan@math.macalstr.edu    <++>
< >  +++++++++++++++++++++++++++++  < >
Technical Details
Message-ID: <BxtIH1.3wF@news.iastate.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!destroyer!news.iastate.edu!news
References: <behrenss.721912181@hphalle6>

Show Full Headers
Complete Article Headers
Newsgroups: comp.sys.apple2.gno
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!destroyer!news.iastate.edu!news
From: achauhan@warsaw.math.macalstr.edu (Amrit Chauhan)
Subject: Re: Crash............
Message-ID: <BxtIH1.3wF@news.iastate.edu>
Sender: news@news.iastate.edu (USENET News System)
Organization: Iowa State University, Ames IA
References: <behrenss.721912181@hphalle6>
Date: Mon, 16 Nov 1992 16:43:43 GMT
Lines: 110