Welcome, Guest. Please login or register.

Author Topic: Learning C with the Amiga  (Read 32482 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2281
  • Country: us
  • Gender: Male
    • Show all replies
Re: Learning C with the Amiga
« on: January 25, 2007, 08:20:25 PM »
Quote

mel_zoom wrote:
Starting with the basics, Ive been looking at variable "types". Lets see if I got this right

A "char" is 1 byte in size
A "short int" is at least as big as a "char"
An "int" is at least as big as a "short int"
A "long int" is at least as big as an "int"

From what Ive read so far, different systems agree on the "char" but differ on the rest, but typical sizes for current systems are

"char" 1 byte
"short int" 2 bytes
"int" 4 bytes
"long int" 4 bytes

Also all of the above can only represent whole number values  and may be specified as "unsigned" or "signed" too but with "signed" being the default for anything other than a char?

Is this correct so far?


Correct so far.

Old source codes made for SAS C and DICE C compilers treat int as a 16-bit word since the original 68000 processor was 16-bit.  For reasons like this you might want to include exec/types.h at the beginning of your Amiga-specific source codes and use BYTE, WORD, and LONG for the respective 8, 16, and 32-bit signed types and UBYTE, UWORD, and ULONG for the respective unsigned types.


One more thing you might want to look into before you get too involved are the SDI Headers which will let you generate Amiga-specific code in such a way that it can be recompiled on AmigaOS 3.x, 4.0, and MorphOS and their respective different versions of the compilers with few (if any) additional changes.
 

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2281
  • Country: us
  • Gender: Male
    • Show all replies
Re: Learning C with the Amiga
« Reply #1 on: February 04, 2007, 06:24:33 PM »
Quote

Cymric wrote:
Quote
Karlos wrote:
Wrong again. Constant items (pointers included) may be allocated in write protected pages of memory on some implementations (ok, not amigaos), physically preventing it from being altered after it has been assigned.

Quite true. The following code will run happily on an Amiga:

Code: [Select]
   char *s = "Hi!";
    ....
    s[1] = 'a';
    ....
   

but will crash on a Unix computer because the string 'Hi!' is saved in memory which cannot be written to. The program will crash with a 'segmentation fault', a sure indicator of the program poking in memory where it shouldn't be. The problem is that there is nothing wrong with the above code: it's the implementation by the compiler which fscks things up.



Actually AmigaOS 4.0 final DOES do memory protection on constant data by making it read-only.  It's only versions 3.x and earlier that don't work correctly.

For a better implementation of strings than is present in the C language use the Better String library.  Beware of the dual-licence it is under, though, since neither GPL nor the most recent BSD licence allow for the programmer to place the code under a different licence than it was originally under.
 

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2281
  • Country: us
  • Gender: Male
    • Show all replies
Re: Learning C with the Amiga
« Reply #2 on: February 07, 2007, 08:38:02 PM »
1.  Returning multiple parameters in C requires passing a structure by pointer.  And yes, an array is only good when passing values of the same type.

2.  AFAIK structures cannot be passed by value and must be passed by reference unless C++ has some feature for creating structures/classes on the stack.  It's not recommended to stuff the stack with lots of parameter data on the Amiga since the Amiga has a fixed stack space for each program.

Arrays are stored in memory as pointers so they are always passed by pointer.  (When using the word "reference" you are referring to a C++/Java term that doesn't strictly apply to C.)

3.  I'm not sure why you would want to waste the stack space in doing so.  Normally structures are dynamically allocated with malloc() and passed by pointer to the appropriate function.

Oh, and by the way, you can embed arrays in structures if you like.
 

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2281
  • Country: us
  • Gender: Male
    • Show all replies
Re: Learning C with the Amiga
« Reply #3 on: February 12, 2007, 05:40:51 PM »
... or if you want non-buffer-overflowing strings in C and don't mind being GPL or BSD licenced, download the Better String library for C/C++.  It supports length-terminated strings with adjustable buffer sizes for proper and fast concatination.
 

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2281
  • Country: us
  • Gender: Male
    • Show all replies
Re: Learning C with the Amiga
« Reply #4 on: February 23, 2007, 05:38:47 PM »
"Putting out" refers to having sex.  He obviously wants to be banned from this website but realized he crossed the line and changed it.