Welcome, Guest. Please login or register.

Author Topic: Beginners 'C' programing group  (Read 13094 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Beginners 'C' programing group
« on: February 13, 2004, 05:10:10 AM »
Quote

Karlos wrote:
@thread

If c++ is your thing, the source for the above program (but not the class libraries themselves ;-) ) is all here.


That's mighty pretty, Karlos, but I must quibble.
in:
if (thing) delete thing;

The test is redundant (delete NULL is safe).

if(!thing)
{
    errorPopUpThingy("failed to alloc thing");
}

won't ever happen unless you use operator new(no_throw)
else new throws bad_alloc which you'll need to catch.
 

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Beginners 'C' programing group
« Reply #1 on: February 14, 2004, 06:35:20 AM »
C is easy and difficult.
For the most part you don't really need to know what is actually going on and you can just go with the semantics. But, once you get into pointers it's good to know what is going on "under the hood" so you know why things like:

char * clone(char * string)
{
   char result;
   strcpy(&result, string);
   return &result;
}

are so bad.

(The finger that points at the moon, is not the moon)
(Build not thy house upon the shifting sands)
 

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Beginners 'C' programing group
« Reply #2 on: February 14, 2004, 10:58:54 PM »
Quote

Cymric wrote:
@Fluffy:

This code will segfault a Unix-machine, and crash an Amiga faster than a politician can shift positions. Indeed, pointers are tricky ;-)


What's really amazing is how often code like this WON'T bring the Amiga or many other systems down right away. If this code is executed at the end of a long sequence of calls, and it doesn't write past the end of the stack (or even if it does for that matter) and the stack never goes that deep again, there is no reason why the data can't live for ever and be manipulated at will. It's just memory.

However, it could bring down a completely different task on the Amiga if it writes past the end of the stack, or maybe, one day when you add a feature you step on this landmine and try to find the bug in your code for days before finding this pre-existing unexploded bug in some code that got there long before you did.

(BTW I just compiled this with a version of gcc at work that we use for our embedded systems. It didn't even warn!)
 

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Beginners 'C' programing group
« Reply #3 on: February 15, 2004, 12:49:32 AM »
Quote

Karlos wrote:

Actually, it brings amigaos down with a thump almost immediately. Remember, the stack grows downwards on amiga/680x0 so as the copy writes to succesively higher addresses,


... oh yeah! See how confusing this can be? :-)
 

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Beginners 'C' programing group
« Reply #4 on: February 15, 2004, 06:14:03 AM »
Quote

iamaboringperson wrote:
Re: Pointers in C

One problem I had when I started learning C, was understanding how functions worked. I could define them etc.  however I treated function calls like 'gosub' or 'goto' in basic.



And also missed out on the joys of function pointers by which you could tell someone else where to goto!!

So, it seems that avoiding pointers is probably a good thing to do for beginners, but programming for the Amiga is riddled with pointers cos they're soooooo useful (especially on slower CPUs when passing large structs by value would be painful).

Another thing that came to mind is the problem of overflow. Since C is pretty close to the metal, it is necessary to know about the number of bits you have in your ints. It's not an arbitrary size and precision environment.

Ooooh, and endianism!!

Oh, and compiler optimizations that blow non volatiles out of your hardware banging code, and ... all sorts of good stuff.

Lot's of gotcha's in C/C++. Still great fun though.