Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« on: February 07, 2007, 09:15:27 PM »
Quote

mel_zoom wrote:
samuraicrow:

"AFAIK structures cannot be passed by value and must be passed by reference"

Well, I was looking at this already and have written a test program that works. Did I compile this wrong?



You passed the structure on the stack instead of passing a pointer to it, and that is perfectly legal and valid. Most of the time we try to avoid this, especially in tight loops as building up and breaking down the stack behind the scenes is slower than just passing a pointer.

It is refreshing to see somebody express a desire to learn C and actually follow through. Even better is that you are making real effort to actually learn the language. In a year you will be telling everybody else how it's done  :-)

 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #1 on: February 07, 2007, 10:45:36 PM »
Only use casts whenever they are absolutely needed.

And speaking of structures and stuff, ive got a question for the gurus.

I often have things in structures that I would like to dump to file and retrieve later. I usually take the sizeof the struct and copy every byte to file, and then later read every byte and copy it into memory and refrence the struct. It is my understanding that one can only be assured of the memory location of the first member in the struct as the optimiser may pad the struct to fit in the pipeline more efficiently. This does not seem to be a problem in practice, when the application is only running on one processor type, but I think this leads to problems passing files between programs compiled on different architectures.

One obvious solution is to hit every member in the struct and write to file, then read back and repack the struct at a later time. This is kind of a pain. Is there a painless way to correctly read and write a struct to a file in a portable manner?
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #2 on: February 07, 2007, 11:18:27 PM »
@Karlos, thanks for the tips man, it's usefull for me and i'm sure it's good review for others as well. For the very few projects I do that require portability, ive hit every member in the struct and converted to network byte order and vice versa. It's a pain in the butt, especially when you have a project with a lot of different data sets. I mostly write stuff for the 8051 and use keil, which doesnt seem to do strange things with structs, but I hate my self every time I IO a struct raw....

Lately I have been messing around more with C++ on windows with MFC. I'm not a c++ guy yet but I am learning. I have seen many examples of people serializing classes to file. Especially CString classes and some of the linked list classes CList, etc. Does c++ help in serializing data in a portable manner? If i declare a class with several structs in it, will a c++ approach help make it easier to serialise to file in a portable manner?

I guess my question sounds kind of dumb, but I am wondering if doing things c++ manner can help make life easier in passing data around in files between the app compiled on different architectures. I'm not a big fan of the bloated XML libs that people bolt on to their apps. I see the usefullness of it, but XMl seems like a waste unless you need massive compatability between apps written in different languages.

 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #3 on: February 07, 2007, 11:46:53 PM »
Quote

lou_dias wrote:

But other languages have 1 clear way of doing every certain thing.  As long as these other languages give you all the functionality you need, why not use them with the saftey on rather than C/C++ with is playing with loaded guns and the safety(s) off.


.Net framework has 100 ways to do the same thing. Which one is the best way? Ive written several apps in VB.Net, don't get me wrong, I like it. The OOP is actually nice in vb now, but still, writing to file or defining a bitmap to draw on the screen or talking with sockets have dozens of different ways to go about it. The VB and c# forums are chock full of guys bickering about the "proper" way to go about doing mundane things like every other language.

If a language was not Touring capable, it would be almost useless except for very specific things. You can do anything in VB/Perl/PHP/Python/Forth/Fortran/Befunge/Conrads game of life/ etc, but C/C++ gives the programmer the ultimate flexability to do anything with the absolute most efficency. 99 out of 100 programmers won't beat the C compiler when writing an algorithm in assembler.
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #4 on: February 09, 2007, 01:07:09 PM »
Why post vb.net stuff in a thread about C? If you want to go over some vb.net stuff, why not do it in a vb.net thread? I'm game. I could use some vb.net review as I have yet to actually move over to that environment ( and there are lots of us out there ).
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #5 on: February 12, 2007, 10:37:30 AM »
c may be a pain for dealing with strings withouth a nice string lib but at least its not like vb.net on opening a file handle:

FILE * bla = fopen("c:\koft\pov.c","w");

vs

Dim bla As New System.IO.FileStream("c:\koft\POV.c", System.IO.FileAccess.Write)

(;
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #6 on: February 12, 2007, 12:18:14 PM »
Quote

lou_dias wrote:

LOL, you make it sound like you typed every letter of that code.  The IDE's auto-complete and selection as soon as you hit the "." reduces the actually typing greatly.

Also, a simple:
imports System.IO

at the top of your code (similar to a #include) and all you'd need in the code is:

Dim bla as New StreamWriter("c:\koft\POV.c")


that sample was from sample from koft.net

Yea, the IDE helps a lot. VisualStudio is top notch, and I use it for all my c/c++ development regardless if my stuff is targeted to linux or windows.

I know there are shortcuts but for a vb.net newbie coming from 10 years of vb experience ( started with 2.0 ) i found myself doing a lot of where.iswhatimlooking.for.inthe.framework.omg.its.all.thewayover.here?

and of course I hated that none of my code could easily migrate to .net. ):

Do we have a way in vb.net now to access lots of memory without using the dreded approach of multiple arrays each having less than 32k members or having to use a stinking collection class and stuffing data into strings to "hack" the ability to use megabytes of ram?
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #7 on: February 12, 2007, 02:06:01 PM »
Ive seen a lot of people get into coding and from what I have seen is that the language almost doesn't matter anyway. A newbie will either grab it by the horns and slog though it or give up.

A person who can't grasp dealing with functions or compiling hello world will flail just as badly regardless of whether they started out with c or perl or python or basic, etc.

C is a good start, it weeds out those who would give up regardless and sets a foundation that can be expanded to anywhere.
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show all replies
    • http://koft.net
Re: Learning C with the Amiga
« Reply #8 on: February 22, 2007, 04:49:13 PM »
Many years back, a girl invited me to come to her place with offerings of food to teach her in the ways of programming. We both worked for a medical supply store, she did data entry, I wrote the software she slaved with. We hit it off and after a while I discovered she had tourettes and months later I was dumped via email. Karlos beware...  :-D