Re: Mentor for C self study wanted

2007-10-26 Thread james

On Thu, 2007-10-25 at 23:19 +0200, Harald Schmalzbauer wrote:
 Am Donnerstag, 25. Oktober 2007 20:22:26 schrieb Aryeh M. Friedman:
   Absolutely.  (I just didn't mention it before because
   obviously Harald already has a beginner's book on the
   C programming language.)
 
  Herald does in fact have one that sucks (it does a terrible job on type
  sizes for example [doesn't mention that they may very on different
 
 Well, probably it's not that bad.
 
 First, thanks to all for your help.
 
 KR has been laying arround here for at least 2 years. I hated it.
 It instructs me to use functions like printf without explaining it, and the 
 examples are not really motivating. So every time I tried to write something 
 on my own I was stoped by the simple printf, for example.

YOU DARE NOT WORSHIP AT THE CHURCH OF KR

No, but seriously, it took me a good three years of picking up KR and
trying to get through the first chapter before I eventually started
seeing why it's referred to as *the* book.
 
 I'm sure it's a very good book as a reference, but it couldn't motivate me as 
 a real C beginner (not a bloody programming beginner though).
 
But if it doesn't work for you, that's okay :) It *is* a good reference,
though. There's annotation for the standard libraries in an appendix.

 So I bought two new books, the first, which I've started with, 
 is Markt+Technik, jetzt lerne ich C (ISBN-13 978-3-8272-4210-5).
 Indeed, it hasn't made clear that short and int are different, they just 
 explained short and long (and double long) and I thought short is a synonym 
 for int.
 But it explains in some short sentences the most important 
 behaviour/requirements for the functions we use. It still leaves me alone 
 when it comes to compilers, but after only three evenings so far I think that 
 I made real progress. Writing a simple practice just works :)
 And I already know that float x; x=10/3 is 3 not 3.. I can't remember 
 reading that in the KR in the first quarter of the book.
 I'm still quiet happy with it.
 
 The next book is O'Reillys C in a nutshell (ISBN 3-89721-344-3). I'll open it 
 if I have structs and pointers practiced...
 
If you've got the cash to spare, try out O'Reilly's Practical C
Programming. That book is very friendly, contains a tonne of great
examples, and a lot of on the spot quizzes. 

I was sold on it when I read the rear cover. It says something like:
KR teaches there are nine rules of precedence in C. We teach two: the
first is that addition comes before subtraction, the second is use
parentheses for everything else.

That kind of simplicity isn't something you see everywhere.

 And of course I'll replay the KR if I have some more basics.
 
 Thanks,
 
 -Harry
 
  machines])... since he is paying me a small amount to help him in detail
  I am going to recommend KR to him (with the caution is is meant for
  experienced programmers)
 
 
 

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread Oliver Fromme
Harald Schmalzbauer wrote:
  
  #include stdio.h
  
  void main()

That's not a C program.  :-)

The return value of the main function of a valid C program
must be int.  And of course, your main function should
end with return 0; or exit(0); (the latter requires
#include stdlib.h at the top).

By the way, I recommend you get a copy of the C standard
and use it for reference.  You can buy a digital copy (PDF)
at  http://webstore.ansi.org/  (Search for 9899-1999),
it's $30.  Alternatively ask Google for C99 draft to get
a free copy of a draft of the standard, which isn't very
different from the final standard.

You can also buy a hardcopy of the standard (i.e. a book),
but it was ~ $300 last time I looked.

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH  Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

When your hammer is C++, everything begins to look like a thumb.
-- Steve Haflich, in comp.lang.c++
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread Erik Trulsson
On Thu, Oct 25, 2007 at 05:02:00PM +0200, Oliver Fromme wrote:
 Harald Schmalzbauer wrote:
   
   #include stdio.h
   
   void main()
 
 That's not a C program.  :-)
 
 The return value of the main function of a valid C program
 must be int.  And of course, your main function should
 end with return 0; or exit(0); (the latter requires
 #include stdlib.h at the top).
 
 By the way, I recommend you get a copy of the C standard
 and use it for reference.  You can buy a digital copy (PDF)
 at  http://webstore.ansi.org/  (Search for 9899-1999),
 it's $30.  Alternatively ask Google for C99 draft to get
 a free copy of a draft of the standard, which isn't very
 different from the final standard.
 
 You can also buy a hardcopy of the standard (i.e. a book),
 but it was ~ $300 last time I looked.

That may be the price if you order a paper copy directly from ANSI/ISO,
but the C standard has also been published as a regular book
(ISBN 978-0-470-84573-8) which is not quite so expensive.

For a beginner the standard itself is probably a bit too heavy-going.
The book usually recommended is 'The C programming language, Second edition'
by Kernighan and Ritchie. ( http://cm.bell-labs.com/cm/cs/cbook/ )



-- 
Insert your favourite quote here.
Erik Trulsson
[EMAIL PROTECTED]
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread Bill Moran
In response to Oliver Fromme [EMAIL PROTECTED]:

 Harald Schmalzbauer wrote:
   
   #include stdio.h
   
   void main()
 
 That's not a C program.  :-)
 
 The return value of the main function of a valid C program
 must be int.  And of course, your main function should
 end with return 0; or exit(0); (the latter requires
 #include stdlib.h at the top).
 
 By the way, I recommend you get a copy of the C standard
 and use it for reference.  You can buy a digital copy (PDF)
 at  http://webstore.ansi.org/  (Search for 9899-1999),
 it's $30.  Alternatively ask Google for C99 draft to get
 a free copy of a draft of the standard, which isn't very
 different from the final standard.
 
 You can also buy a hardcopy of the standard (i.e. a book),
 but it was ~ $300 last time I looked.

If we're recommending books, I can't say enough good things
about the Kernighan and Richie C book:
http://www.amazon.com/C-Programming-Language-2nd/dp/0131103628/ref=pd_bbs_2/105-1904293-7155604?ie=UTF8s=booksqid=1193326006sr=1-2

-- 
Bill Moran
http://www.potentialtech.com
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread Oliver Fromme
Erik Trulsson wrote:
  Oliver Fromme wrote:
   By the way, I recommend you get a copy of the C standard
   and use it for reference.  You can buy a digital copy (PDF)
   at  http://webstore.ansi.org/  (Search for 9899-1999),
   it's $30.  Alternatively ask Google for C99 draft to get
   a free copy of a draft of the standard, which isn't very
   different from the final standard.
   
   You can also buy a hardcopy of the standard (i.e. a book),
   but it was ~ $300 last time I looked.
  
  That may be the price if you order a paper copy directly from ANSI/ISO,

Yes.

  but the C standard has also been published as a regular book
  (ISBN 978-0-470-84573-8) which is not quite so expensive.

OK, I didn't know about that one.  Personally I chose to
order the PDF copy (which was $15 a few years ago, IIRC,
now it's $30) after working for some time with the free
draft.

  For a beginner the standard itself is probably a bit too heavy-going.

Right, it's certainly not suitable as a tutorial or as an
introduction for a beginner.  But it's useful as a reference
when you need to know the official details about a certain
part of the language (e.g. where do sequence points occur,
exactly?).

  The book usually recommended is 'The C programming language, Second edition'
  by Kernighan and Ritchie. ( http://cm.bell-labs.com/cm/cs/cbook/ )

Absolutely.  (I just didn't mention it before because
obviously Harald already has a beginner's book on the
C programming language.)

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH  Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

Documentation is like sex; when it's good, it's very, very good,
and when it's bad, it's better than nothing.
-- Dick Brandon
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread cpghost
On Thu, 25 Oct 2007 17:22:11 +0200
Erik Trulsson [EMAIL PROTECTED] wrote:

 For a beginner the standard itself is probably a bit too heavy-going.
 The book usually recommended is 'The C programming language, Second
 edition' by Kernighan and Ritchie.
 ( http://cm.bell-labs.com/cm/cs/cbook/ )

Yes, that's the *definitive* book! Add to it The C Answer Book
by Tondo/Gimpel (title in german: Das C Loesungsbuch), with goes
along with KR 2nd Ed. and you have everything you need.

-cpghost.

-- 
Cordula's Web. http://www.cordula.ws/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread Aryeh M. Friedman

 Absolutely.  (I just didn't mention it before because
 obviously Harald already has a beginner's book on the
 C programming language.)
   

Herald does in fact have one that sucks (it does a terrible job on type
sizes for example [doesn't mention that they may very on different
machines])... since he is paying me a small amount to help him in detail
I am going to recommend KR to him (with the caution is is meant for
experienced programmers)

-- 
Aryeh M. Friedman
FloSoft Systems
Developer, not Business, Friendly
http://www.flosoft-systems.com

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread Bahman M.
On 2007-10-25 Bill Moran wrote:
 In response to Oliver Fromme [EMAIL PROTECTED]:
 
  Harald Schmalzbauer wrote:

#include stdio.h

void main()
  
  That's not a C program.  :-)
  
  The return value of the main function of a valid C program
  must be int.  And of course, your main function should
  end with return 0; or exit(0); (the latter requires
  #include stdlib.h at the top).
  
  By the way, I recommend you get a copy of the C standard
  and use it for reference.  You can buy a digital copy (PDF)
  at  http://webstore.ansi.org/  (Search for 9899-1999),
  it's $30.  Alternatively ask Google for C99 draft to get
  a free copy of a draft of the standard, which isn't very
  different from the final standard.
  
  You can also buy a hardcopy of the standard (i.e. a book),
  but it was ~ $300 last time I looked.
 
 If we're recommending books, I can't say enough good things
 about the Kernighan and Richie C book:
 http://www.amazon.com/C-Programming-Language-2nd/dp/0131103628/ref=pd_bbs_2/105-1904293-7155604?ie=UTF8s=booksqid=1193326006sr=1-2
 
No doubt the most valuable book on programming I've ever read.  Not
only it teaches programming but also a style of thinking and designing.

-- 
Bahman Movaqar

You can't write poems about trees when the woods are full of policemen.
-Bertolt Brecht
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-25 Thread Harald Schmalzbauer
Am Donnerstag, 25. Oktober 2007 20:22:26 schrieb Aryeh M. Friedman:
  Absolutely.  (I just didn't mention it before because
  obviously Harald already has a beginner's book on the
  C programming language.)

 Herald does in fact have one that sucks (it does a terrible job on type
 sizes for example [doesn't mention that they may very on different

Well, probably it's not that bad.

First, thanks to all for your help.

KR has been laying arround here for at least 2 years. I hated it.
It instructs me to use functions like printf without explaining it, and the 
examples are not really motivating. So every time I tried to write something 
on my own I was stoped by the simple printf, for example.

I'm sure it's a very good book as a reference, but it couldn't motivate me as 
a real C beginner (not a bloody programming beginner though).

So I bought two new books, the first, which I've started with, 
is Markt+Technik, jetzt lerne ich C (ISBN-13 978-3-8272-4210-5).
Indeed, it hasn't made clear that short and int are different, they just 
explained short and long (and double long) and I thought short is a synonym 
for int.
But it explains in some short sentences the most important 
behaviour/requirements for the functions we use. It still leaves me alone 
when it comes to compilers, but after only three evenings so far I think that 
I made real progress. Writing a simple practice just works :)
And I already know that float x; x=10/3 is 3 not 3.. I can't remember 
reading that in the KR in the first quarter of the book.
I'm still quiet happy with it.

The next book is O'Reillys C in a nutshell (ISBN 3-89721-344-3). I'll open it 
if I have structs and pointers practiced...

And of course I'll replay the KR if I have some more basics.

Thanks,

-Harry

 machines])... since he is paying me a small amount to help him in detail
 I am going to recommend KR to him (with the caution is is meant for
 experienced programmers)



-- 
OmniSEC  -  UNIX und Windows Netzwerke - Sicher
Harald Schmalzbauer
Flintsbacher Str. 3
80686 München
+49 (0) 89 18947781
+49 (0) 160 93860101
USt-IdNr.: DE253184753


signature.asc
Description: This is a digitally signed message part.


Re: Mentor for C self study wanted

2007-10-24 Thread Giorgos Keramidas
On 2007-10-23 23:24, Harald Schmalzbauer [EMAIL PROTECTED] wrote:
 Thanks all,
 here was my example, just for completeness, I found mentors for my
 needs.

 #include stdio.h

 void main()
 {
   short nnote;

   // Numerischen Notenwert einlesen
   printf(Bitte numerischen Schulnotenwert eingeben: );
   scanf(%d,nnote);

You are passing %d to scanf() so it expects to find enough 'storage'
in its pointer argument for an 'int'.  If 'short' happens to have a
smaller size (as is commonly the case), scanf() will overwrite random
memory locations after 'nnote'.  On systems where 'nnote' is stored in
the stack (because it's an automatic/local variable of main()), you are
risking stack corruption (and a SEGFAULT *may* happen).

It's also a very good idea to check the return code of scanf():

int nnote;

if (scanf(%d, nnote) != 1) {
error;
}

   switch (nnote)
   {
 case 1: printf(Die Note %d entspricht sehr gut.,nnote);
 break;
 case 2: printf(Die Note %d entspricht gut.,nnote);
 break;
 case 3: printf(Die Note %d entspricht befriedigend.,nnote);
 break;
 case 4: printf(Die Note %d entspricht ausreichend.,nnote);
 break;
 case 5: printf(Die Note %d entspricht mangelhaft.,nnote);
 break;
 case 6: printf(Die Note %d entspricht ungen?gend.,nnote);
 break;
 default: printf(%d ist keine zul?ssige Schulnote!);

There's no `int' argument to the printf() call of the default clause.

This will either cause printf() to print random garbage, or try to
access memory regions which are unmapped and SEGFAULT.

 P.S.:
 I found that declaring nnote as int soleves my problem, but I couldn?t
 understand why.
 Another one was the result of default: nnote was -1077942208 instead
 of 9 for example.

It was never assigned to 9 :)

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread cpghost
On Tue, 23 Oct 2007 20:44:52 +0200
Harald Schmalzbauer [EMAIL PROTECTED] wrote:

 The first one was for example the attached code: Why does it segfault?

Mailman ate the attachment... Can't see it here.

-cpghost.

-- 
Cordula's Web. http://www.cordula.ws/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Bill Moran
In response to cpghost [EMAIL PROTECTED]:

 On Tue, 23 Oct 2007 20:44:52 +0200
 Harald Schmalzbauer [EMAIL PROTECTED] wrote:
 
  The first one was for example the attached code: Why does it segfault?
 
 Mailman ate the attachment... Can't see it here.

I may be out of line, but I think if you're using FreeBSD as your
learning platform, that it wouldn't be a problem to ask this list.

Although, you'll have to include your code inline to get past the
sanitizers.

-- 
Bill Moran
http://www.potentialtech.com
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Harald Schmalzbauer
Am Dienstag, 23. Oktober 2007 22:24:54 schrieb Bill Moran:
 In response to cpghost [EMAIL PROTECTED]:
  On Tue, 23 Oct 2007 20:44:52 +0200
 
  Harald Schmalzbauer [EMAIL PROTECTED] wrote:
   The first one was for example the attached code: Why does it segfault?
 
  Mailman ate the attachment... Can't see it here.

 I may be out of line, but I think if you're using FreeBSD as your
 learning platform, that it wouldn't be a problem to ask this list.
 Harald Schmalzbauer wrote:
  Am Dienstag, 23. Oktober 2007 21:43:52 schrieben Sie:
  Hello Aryeh,
 
  I'm willing to pay fair fees, but are you interested in micro payment
  ;) ?
 
  For other reasons I love micro payments.
 
  Serious, I'll have the one or other short question per week (I'm
  usually busy, just making spare time lessons from my book (UTC-1 spare
  time)).
 
  Just so I know what level to present on what is your background in CS
  and programming?
 
  CS??
  I'm able to solve problems analytically, but I don't know any language
  really well.
  I know bourne shell, csh, pascal, and basic. And a tiny bit asm, but
  that's been on ZX81.

 Although, you'll have to include your code inline to get past the
 sanitizers.

Thanks all,

here was my example, just for completeness, I found mentors for my needs.

Thanks a lot to all!


#include stdio.h

void main()
{
  short nnote;

  // Numerischen Notenwert einlesen
  printf(Bitte numerischen Schulnotenwert eingeben: );
  scanf(%d,nnote);

  switch (nnote)
  {
case 1: printf(Die Note %d entspricht sehr gut.,nnote);
break;
case 2: printf(Die Note %d entspricht gut.,nnote);
break;
case 3: printf(Die Note %d entspricht befriedigend.,nnote);
break;
case 4: printf(Die Note %d entspricht ausreichend.,nnote);
break;
case 5: printf(Die Note %d entspricht mangelhaft.,nnote);
break;
case 6: printf(Die Note %d entspricht ungenügend.,nnote);
break;
default: printf(%d ist keine zulässige Schulnote!);
  }
  printf(\n);
}

P.S.:
I found that declaring nnote as int soleves my problem, but I couldnÄt 
understand why.
Another one was the result of default: nnote was -1077942208 instead of 9 for 
example.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Harald Schmalzbauer
Am Dienstag, 23. Oktober 2007 23:24:09 schrieb Harald Schmalzbauer:
[*snip*]
  Although, you'll have to include your code inline to get past the
  sanitizers.

 Thanks all,

 here was my example, just for completeness, I found mentors for my needs.

 Thanks a lot to all!


 #include stdio.h

 void main()
 {
   short nnote;

   // Numerischen Notenwert einlesen
   printf(Bitte numerischen Schulnotenwert eingeben: );
   scanf(%d,nnote);

   switch (nnote)
   {
 case 1: printf(Die Note %d entspricht sehr gut.,nnote);
 break;
[snip]
 default: printf(%d ist keine zulässige Schulnote!);
   }
   printf(\n);
 }

 P.S.:
 I found that declaring nnote as int soleves my problem, but I couldnÄt
 understand why.
 Another one was the result of default: nnote was -1077942208 instead of 9
 for example.

Ok, the last one is a typo, I forgot ...ote!,%d);.
But interesting that ther's some output. Constant output

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Derek Ragona

At 04:24 PM 10/23/2007, Harald Schmalzbauer wrote:

Am Dienstag, 23. Oktober 2007 22:24:54 schrieb Bill Moran:
 In response to cpghost [EMAIL PROTECTED]:
  On Tue, 23 Oct 2007 20:44:52 +0200
 
  Harald Schmalzbauer [EMAIL PROTECTED] wrote:
   The first one was for example the attached code: Why does it segfault?
 
  Mailman ate the attachment... Can't see it here.

 I may be out of line, but I think if you're using FreeBSD as your
 learning platform, that it wouldn't be a problem to ask this list.
 Harald Schmalzbauer wrote:
  Am Dienstag, 23. Oktober 2007 21:43:52 schrieben Sie:
  Hello Aryeh,
 
  I'm willing to pay fair fees, but are you interested in micro payment
  ;) ?
 
  For other reasons I love micro payments.
 
  Serious, I'll have the one or other short question per week (I'm
  usually busy, just making spare time lessons from my book (UTC-1 spare
  time)).
 
  Just so I know what level to present on what is your background in CS
  and programming?
 
  CS??
  I'm able to solve problems analytically, but I don't know any language
  really well.
  I know bourne shell, csh, pascal, and basic. And a tiny bit asm, but
  that's been on ZX81.

 Although, you'll have to include your code inline to get past the
 sanitizers.

Thanks all,

here was my example, just for completeness, I found mentors for my needs.

Thanks a lot to all!


#include stdio.h

void main()
{
  short nnote;

  // Numerischen Notenwert einlesen
  printf(Bitte numerischen Schulnotenwert eingeben: );
  scanf(%d,nnote);

  switch (nnote)
  {
case 1: printf(Die Note %d entspricht sehr gut.,nnote);
break;
case 2: printf(Die Note %d entspricht gut.,nnote);
break;
case 3: printf(Die Note %d entspricht befriedigend.,nnote);
break;
case 4: printf(Die Note %d entspricht ausreichend.,nnote);
break;
case 5: printf(Die Note %d entspricht mangelhaft.,nnote);
break;
case 6: printf(Die Note %d entspricht ungenügend.,nnote);
break;
default: printf(%d ist keine zulässige Schulnote!);
  }
  printf(\n);
}

P.S.:
I found that declaring nnote as int soleves my problem, but I couldnÄt
understand why.
Another one was the result of default: nnote was -1077942208 instead of 9 for
example.


if you check the man page on scanf:
 d Matches an optionally signed decimal integer; the next pointer must
   be a pointer to int.

You shouldn't try to put a short into an int.  Always declare the correct 
size for variables.  Your segv is because scanf was trying to put an int 
where it won't fit.


You will get the same result if you go off the end of an array.

-Derek

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
MailScanner thanks transtec Computers for their support.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread cpghost
On Tue, 23 Oct 2007 23:24:09 +0200
Harald Schmalzbauer [EMAIL PROTECTED] wrote:

 #include stdio.h
 
 void main()
 {
   short nnote;
^

   // Numerischen Notenwert einlesen
   printf(Bitte numerischen Schulnotenwert eingeben: );
   scanf(%d,nnote);
  ^

 I found that declaring nnote as int soleves my problem, but I
 couldnÄt understand why.
 Another one was the result of default: nnote was -1077942208 instead
 of 9 for example.

There's a mismatch here: scanf(%d, ...) expects a pointer to int,
while nnote is a pointer to a short. Normally, an int occupies more
bytes in memory than a short (typically sizeof(int) == 4 on 32bit
platforms, and sizeof(int) == 8 on 64bit platforms; while typically
sizeof(short) == 2).

So scanf(3) tries to store the result into 4 bytes, but you've provided
a pointer to only 2 bytes of memory. Where will the other 2 bytes be
stored by scanf? In your example, short nnote is an automatic variable:
i.e. it's stored on the stack. So the other 2 bytes will be also saved
on the stack, on a place that's not reserved for this. There could be
anything there, like, say, a part of the return address for the
function, or it could be on some page in memory that's read-only or
non-allocated. In either case, the program behaviour is undefined, and
this normally means it dumps core.

So either replace short nnote with int nnote, OR change %d
to the appropriate format string identifier for short int %hd
(look up man scanf for a list of those identifiers), both in
scanf and printf calls.

-cpghost.

-- 
Cordula's Web. http://www.cordula.ws/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Bruce Cran

Derek Ragona wrote:

At 04:24 PM 10/23/2007, Harald Schmalzbauer wrote:

Am Dienstag, 23. Oktober 2007 22:24:54 schrieb Bill Moran:
 In response to cpghost [EMAIL PROTECTED]:
  On Tue, 23 Oct 2007 20:44:52 +0200
 
  Harald Schmalzbauer [EMAIL PROTECTED] wrote:
   The first one was for example the attached code: Why does it 
segfault?

 
  Mailman ate the attachment... Can't see it here.

 I may be out of line, but I think if you're using FreeBSD as your
 learning platform, that it wouldn't be a problem to ask this list.
 Harald Schmalzbauer wrote:
  Am Dienstag, 23. Oktober 2007 21:43:52 schrieben Sie:
  Hello Aryeh,
 
  I'm willing to pay fair fees, but are you interested in micro 
payment

  ;) ?
 
  For other reasons I love micro payments.
 
  Serious, I'll have the one or other short question per week (I'm
  usually busy, just making spare time lessons from my book (UTC-1 
spare

  time)).
 
  Just so I know what level to present on what is your background 
in CS

  and programming?
 
  CS??
  I'm able to solve problems analytically, but I don't know any 
language

  really well.
  I know bourne shell, csh, pascal, and basic. And a tiny bit asm, but
  that's been on ZX81.

 Although, you'll have to include your code inline to get past the
 sanitizers.

Thanks all,

here was my example, just for completeness, I found mentors for my needs.

Thanks a lot to all!


#include stdio.h

void main()
{
  short nnote;

  // Numerischen Notenwert einlesen
  printf(Bitte numerischen Schulnotenwert eingeben: );
  scanf(%d,nnote);

  switch (nnote)
  {
case 1: printf(Die Note %d entspricht sehr gut.,nnote);
break;
case 2: printf(Die Note %d entspricht gut.,nnote);
break;
case 3: printf(Die Note %d entspricht befriedigend.,nnote);
break;
case 4: printf(Die Note %d entspricht ausreichend.,nnote);
break;
case 5: printf(Die Note %d entspricht mangelhaft.,nnote);
break;
case 6: printf(Die Note %d entspricht ungenügend.,nnote);
break;
default: printf(%d ist keine zulässige Schulnote!);
  }
  printf(\n);
}

P.S.:
I found that declaring nnote as int soleves my problem, but I couldnÄt
understand why.
Another one was the result of default: nnote was -1077942208 instead 
of 9 for

example.


if you check the man page on scanf:
 d Matches an optionally signed decimal integer; the next pointer must
   be a pointer to int.

You shouldn't try to put a short into an int.  Always declare the 
correct size for variables.  Your segv is because scanf was trying to 
put an int where it won't fit.


You will get the same result if you go off the end of an array.

-Derek



It's well worth increasing the number of warnings enabled when writing C 
code, to catch any errors early on.  -Wall catches this sort of mistake.


Compiling your code gives the following output:

 gcc -Wall test.c -o test
test.c:4: warning: return type of 'main' is not 'int'
test.c: In function 'main':
test.c:9: warning: format '%d' expects type 'int *', but argument 2 has 
type 'short int *'


--
Bruce Cran
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Bruce Cran

cpghost wrote:


There's a mismatch here: scanf(%d, ...) expects a pointer to int,
while nnote is a pointer to a short. Normally, an int occupies more
bytes in memory than a short (typically sizeof(int) == 4 on 32bit
platforms, and sizeof(int) == 8 on 64bit platforms; while typically
sizeof(short) == 2).


I think short and int stay the same on both 32 and 64 bit platforms, 
while it's only long that gets bumped to 8 bytes.  At least that seems 
to be what happens on FreeBSD amd64.


--
Bruce
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread cpghost
On Tue, 23 Oct 2007 23:36:40 +0100
Bruce Cran [EMAIL PROTECTED] wrote:

 cpghost wrote:
 
  There's a mismatch here: scanf(%d, ...) expects a pointer to int,
  while nnote is a pointer to a short. Normally, an int occupies more
  bytes in memory than a short (typically sizeof(int) == 4 on 32bit
  platforms, and sizeof(int) == 8 on 64bit platforms; while typically
  sizeof(short) == 2).
 
 I think short and int stay the same on both 32 and 64 bit platforms, 
 while it's only long that gets bumped to 8 bytes.  At least that
 seems to be what happens on FreeBSD amd64.

Hmmm... yep, you're right, I'm wrong! I've switched compilers
too often recently. Yes, on gcc sizeof(int) == 4 on both 32bit and
64bit. Thanks for pointing this out: I stay corrected. ;)

-- 
Cordula's Web. http://www.cordula.ws/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Heiko Wundram (Beenic)
Am Dienstag, 23. Oktober 2007 23:24:09 schrieb Harald Schmalzbauer:
 #include stdio.h

 void main()
 {
   short nnote;

   // Numerischen Notenwert einlesen
   printf(Bitte numerischen Schulnotenwert eingeben: );
   scanf(%d,nnote);

man 3 scanf (most important thing to look at with any such problem is the 
C-library documentation, which is excellent on FreeBSD) says that for %d 
the passed pointer has to be a pointer to integer, which nnote is not. 
nnote is a pointer to short, which points to 2 bytes, whereas a pointer to 
integer is a pointer to 4 bytes of storage.

Generally, nnote is reserved by the compiler on the stack (as it's a local 
variable) with two bytes (but this depends on your platform), and nnote 
points to the beginning of this area.

As you are probably running on a little-endian architecture, the layout that 
scanf presumes is (from low to high):

--- increasing addresses
lsbyte 2 3 msbyte
^
|-- nnote points here

of which only the first two are interpreted as nnote by the rest of the 
program; the upper two are different stack content (probably a return address 
to the C initialization code calling main(), or a pushed stack pointer, or 
such, as your procedure defines no other locals, see below).

Now, when scanf assigns the four bytes, it'll properly enter the lower two 
bytes of the integer into lsbyte 2 (which is nnote, in the same byte 
order), but overwrite two bytes that are above it.

When main() finishes, the (now broken) saved address (of which 3 msbyte is 
the lower half) is popped, which leads to the SIGSEGV you're seeing.

In case you were on big-endian, the result would be different (i.e., the order 
would be reversed, so that nnote would always be zero or minus one in case 
you entered small integral values in terms of absolute value), but 
effectively, the return address would be overwritten as well, breaking it.

This is effectively what can be called a buffer-overflow.

Just to finish this: the proper format would be %hd, for which the flag h 
signifies that the pointer is a pointer to a short int, also documented in 
man 3 scanf.

Why aren't you seeing this behaviour with printf (i.e., why can you pass a 
short but still specify %d)? Because C defines that functions that take a 
variable number of arguments (of which printf is one such) get each argument 
as type long (the type that's at least as big as a pointer on the current 
platform), so when passing a short as argument to a var-args function, the 
C-compiler inserts code which makes sure that the value is promoted to a long 
in the argument stack for printf. scanf is also a varargs function, but 
you're not passing the value of nnote, but rather a pointer to it, which 
(should) already be as wide as a long.

Finally, looking at (parts of) the assembly that gcc generates (on a 
little-endian i386 machine):

.globl main
.type   main, @function
main:
leal4(%esp), %ecx
andl$-16, %esp
pushl   -4(%ecx)
pushl   %ebp

; Set up the pointer to the local frame (EBP on i386). All locals are
; relative to EBP in a function.
movl%esp, %ebp

; ECX is the first (hidden) local.
pushl   %ecx

subl$20, %esp
subl$12, %esp
pushl   $.LC0
callprintf
addl$16, %esp
subl$8, %esp

; Load the effective address of EBP-6, i.e., nnote, into EAX, which
; is pushed for scanf. scanf will thus write its output on EBP-6 up to
; EBP-3, where EBP-4 and EBP-3 are part of the value that's been
; pushed in the pushl %ecx above.
leal-6(%ebp), %eax

pushl   %eax
pushl   $.LC1
callscanf

...

; Restore the value at EBP-4 (i.e., the ECX that was pushed above) into
; ECX at function exit. This value has been corrupted by the integer
; assignment due to scanf.
movl-4(%ebp), %ecx

leave

; Restore the stack pointer from the (invalidated) %ecx, i.e. produce a
; bogus stack pointer.
leal-4(%ecx), %esp

ret

This produces a segfault, after the return to the C initialization code, 
simply because the stack pointer is totally bogus.

 P.S.:
 I found that declaring nnote as int soleves my problem, but I couldnÄt
 understand why.

Everything clear now? ;-)

-- 
Heiko Wundram
Product  Application Development
-
Office Germany - EXPO PARK HANNOVER
 
Beenic Networks GmbH
Mailänder Straße 2
30539 Hannover
 
Fon+49 511 / 590 935 - 15
Fax+49 511 / 590 935 - 29
Mobil  +49 172 / 437 3 734
Mail   [EMAIL PROTECTED]


Beenic Networks GmbH
-
Sitz der Gesellschaft: Hannover
Geschäftsführer: Jorge Delgado
Registernummer: HRB 61869
Registergericht: Amtsgericht Hannover
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread Garrett Cooper

Bruce Cran wrote:

cpghost wrote:


There's a mismatch here: scanf(%d, ...) expects a pointer to int,
while nnote is a pointer to a short. Normally, an int occupies more
bytes in memory than a short (typically sizeof(int) == 4 on 32bit
platforms, and sizeof(int) == 8 on 64bit platforms; while typically
sizeof(short) == 2).


I think short and int stay the same on both 32 and 64 bit platforms, 
while it's only long that gets bumped to 8 bytes.  At least that seems 
to be what happens on FreeBSD amd64.


--
Bruce
No... you're only safe using int32, int64, etc. Just for grins try 
compiling a program like this:


#include stdio.h

int main() {
   printf(%d\n, sizeof(int));
   return 0;
}
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Mentor for C self study wanted

2007-10-23 Thread cpghost
On Tue, 23 Oct 2007 23:24:09 +0200
Harald Schmalzbauer [EMAIL PROTECTED] wrote:

 #include stdio.h
 
 void main()
 {
   short nnote;
 
   // Numerischen Notenwert einlesen
   printf(Bitte numerischen Schulnotenwert eingeben: );
   scanf(%d,nnote);
 
   switch (nnote)
   {
 case 1: printf(Die Note %d entspricht sehr gut.,nnote);
 break;
 case 2: printf(Die Note %d entspricht gut.,nnote);
 break;
 case 3: printf(Die Note %d entspricht befriedigend.,nnote);
 break;
 case 4: printf(Die Note %d entspricht ausreichend.,nnote);
 break;
 case 5: printf(Die Note %d entspricht mangelhaft.,nnote);
 break;
 case 6: printf(Die Note %d entspricht ungenügend.,nnote);
 break;
 default: printf(%d ist keine zulässige Schulnote!);
  ^ ^

No matching int for %d here. It'll print garbage. Change to:
default: printf(%hd ist keine...!, nnote);
   

   }
   printf(\n);
 }
 
 P.S.:
 I found that declaring nnote as int soleves my problem, but I
 couldnÄt understand why.
 Another one was the result of default: nnote was -1077942208 instead
 of 9 for example.

The reason for this is that the number of arguments after printf's
format string MUST match the number of %-place holders (unless
you're using exotic stuff like %n, of course). If printf misses
some arguments, it will fetch them from a place that is
implementation dependant (and that almost always means:
you'll get garbage).

Sorry for overlooking your second question... ;)

-cpghost.

-- 
Cordula's Web. http://www.cordula.ws/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]