Author Topic: General C programming question  (Read 11238 times)

0 Members and 1 Guest are viewing this topic.

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3328
  • Country: ca
Re: General C programming question
« Reply #50 on: July 23, 2018, 04:50:02 pm »
i had 23 arguments in the class init + internal init + create class function ( create class & class init with overload ).
I spended so much time on the arguments, now all good.
Yes have to create the struct, only once, then can adjust.

Class is already a structure. In the constructor, you initialize its variables the best you can (or not at all), then, once the class is created, you set the member variables as you please.
 

Offline JanJansen

  • Frequent Contributor
  • **
  • Posts: 380
  • Country: nl
Re: General C programming question
« Reply #51 on: July 23, 2018, 04:54:09 pm »
I do my best to set variables only once.
aliexpress parachute
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6625
  • Country: nl
Re: General C programming question
« Reply #52 on: July 23, 2018, 05:27:10 pm »
There is the culprit you are a C++ programmer.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: General C programming question
« Reply #53 on: July 23, 2018, 07:05:48 pm »
i had 23 arguments in the class init + internal init + create class function ( create class & class init with overload ).
I spended so much time on the arguments, now all good.
Yes have to create the struct, only once, then can adjust.

Class is already a structure. In the constructor, you initialize its variables the best you can (or not at all), then, once the class is created, you set the member variables as you please.
Minor nitpick...

An "instance" (or "object") is created at runtime via a C++ constructor. But a "class" is something created by the programmer.

Also, constructors are a great thing because they're a systematic way to ensure that everything is initialized. However, a good rule of thumb for embedded C++ is to avoid doing real "work" in a constructor and only set member variables to values that are constant at link-time. Compile-time constants are perfectly fine and so are addresses of things that the linker will determine. But calling code that does computation within a constructor can be risky because it's difficult to predict the initialization order of static C++ objects, and constructors of static instances are called *before* main.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: General C programming question
« Reply #54 on: July 23, 2018, 07:07:19 pm »
There is the culprit you are a C++ programmer.
Piffle. C++ is awesome for embedded work. Better than straight C, IMO.
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6625
  • Country: nl
Re: General C programming question
« Reply #55 on: July 23, 2018, 08:47:30 pm »
There is the culprit you are a C++ programmer.
Piffle. C++ is awesome for embedded work. Better than straight C, IMO.
Whatever, I am not starting that endless debate again.
I was saying it since we are in a C topic  ::)
 

Offline JanJansen

  • Frequent Contributor
  • **
  • Posts: 380
  • Country: nl
Re: General C programming question
« Reply #56 on: July 24, 2018, 01:28:33 pm »
Ok the debate is on, it dont matters C or c++, you can use a struct, you can use the same trick in C, i should not have mentioned the word class to avoid debates.
aliexpress parachute
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: General C programming question
« Reply #57 on: July 24, 2018, 02:14:32 pm »
There was a C with objects, before C++.
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 5153
  • Country: nz
Re: General C programming question
« Reply #58 on: July 24, 2018, 04:46:05 pm »
There was a C with objects, before C++.

Really? Which one?

Stroustrup's own "C with Classes" was started in 1979, and C++ in 1983 was a rename rather than a new language.

Cox's Objective-C was officially started in 1984 I believe. I don't know if it was unofficial before that.
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: General C programming question
« Reply #59 on: July 24, 2018, 08:02:07 pm »
Lightspeed C/Think C, it was a thing for the Mac: https://en.wikipedia.org/wiki/THINK_C
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 5153
  • Country: nz
Re: General C programming question
« Reply #60 on: July 24, 2018, 09:57:59 pm »
Lightspeed C/Think C, it was a thing for the Mac: https://en.wikipedia.org/wiki/THINK_C

I used it, although I used THINK Pascal and MPW C and Pascal more. And CodeWarrior years later when the PowerPC hit.

THINK C came out in mid 1986 several years after C++ and Objective C (which both existed before the Mac was even released).
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: General C programming question
« Reply #61 on: July 25, 2018, 09:08:48 am »
THINK C came out in mid 1986 several years after C++ and Objective C (which both existed before the Mac was even released).
Existed before, but only in Stroustrup's head :-)

https://en.wikipedia.org/wiki/C++#History
Quote from: wikipedia
In 1985, the first edition of The C++ Programming Language was released, which became the definitive reference for the language, as there was not yet an official standard.[13] The first commercial implementation of C++ was released in October of the same year

The Mac's birthdate is 22, January 1984.

But yeah, my bad: in the Mac world C with objects came before C++.
« Last Edit: July 25, 2018, 04:55:24 pm by GeorgeOfTheJungle »
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 5153
  • Country: nz
Re: General C programming question
« Reply #62 on: July 25, 2018, 10:36:17 am »
THINK C came out in mid 1986 several years after C++ and Objective C (which both existed before the Mac was even released).
Existed before, but only in Stroustrup's head :-)

No, cfront was available as open source software from (and used within) AT&T.

Quote
https://en.wikipedia.org/wiki/C++#History
Quote from: wikipedia
In 1985, the first edition of The C++ Programming Language was released, which became the definitive reference for the language, as there was not yet an official standard.[13] The first commercial implementation of C++ was released in October of the same year

But yeah, my bad: in the Mac world C with objects came before C++.

That first commercial implementation of C++ was Comeau C++, a supported version of cfront.

But open source, unsupported, existed before it. You could get it if you asked AT&T (or Stroustrup).

Apple's first C++ implementation in MPW was also a licensed version of cfront.
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: General C programming question
« Reply #63 on: July 25, 2018, 03:51:28 pm »
Apple's first C++ implementation in MPW was also a licensed version of cfront.

But that came later, after LightSpeed/Think C. The first MPWs did not come with any C compiler. I still have the complete docs in binders somewhere. Remember Commando? The toolbox was written in Pascal, and the calling convention was Pascal, and the strings were Pascal style, it made not much sense to use anything else in the Mac, back then.
« Last Edit: July 25, 2018, 03:54:35 pm by GeorgeOfTheJungle »
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: General C programming question
« Reply #64 on: July 25, 2018, 04:18:46 pm »
Apple's first C++ implementation in MPW was also a licensed version of cfront.

But that came later, after LightSpeed/Think C. The first MPWs did not come with any C compiler. I still have the complete docs in binders somewhere. Remember Commando? The toolbox was written in Pascal, and the calling convention was Pascal, and the strings were Pascal style, it made not much sense to use anything else in the Mac, back then.
Yep, the original AppKit for the Mac was written in Object Pascal. One of my early projects (~1990) was writing a transpiler that converted Mac-specific Object Pascal code to "portable" C++ (which was still using cfront).
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: General C programming question
« Reply #65 on: July 25, 2018, 05:54:35 pm »
I knew I had seen that name before... (you know who) now I know where: c.s.m.p !!!  8)
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 5153
  • Country: nz
Re: General C programming question
« Reply #66 on: July 25, 2018, 09:52:40 pm »
Apple's first C++ implementation in MPW was also a licensed version of cfront.

But that came later, after LightSpeed/Think C.

I didn't say otherwise. MPW C++ was just mentioned as another example of using cfront, which was available in both commercial and non-commercial versions. MPW C++ was much later .. it waited for cfront 2.0.

Your claim was that THINK C (with objects) was before the first *commercial* C++. Which is true enough. But it was well after C++ was available non-commercially (on Unix).

The first C++ that wasn't based on cfront was Zortech C++ in I think late 1988 or early 1989. I probably still have the box and floppies somewhere. It was done by Walter Bright based on his earlier Lightspeed C. Symantec later bought it and ruined it. Walter of course later created D.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 5153
  • Country: nz
Re: General C programming question
« Reply #67 on: July 25, 2018, 10:05:58 pm »
I knew I had seen that name before... (you know who) now I know where: c.s.m.p !!!  8)

Many years ago.

I'm amazed there are still a lot of copies of this floating around:

http://www.ralentz.com/old/mac/humor/mpw-c-errors.html

https://groups.google.com/forum/#!original/comp.sys.mac.programmer/UkCjFPL4zEM/-lM87bJxPUcJ

A slightly different list had independently been posted by Alan Rosenthal a year earlier but I think I wasn't yet on usenet at that time and BIX was still my main internet forum.
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: General C programming question
« Reply #68 on: July 26, 2018, 06:39:53 pm »
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4413
  • Country: us
Re: General C programming question
« Reply #69 on: July 28, 2018, 07:10:11 am »
I remember... buying a triple-threat package containing BDS C, MINCE, and Scribble.Alas, I don't think I ever used any of them: the CP/M system that they were supposed to run on never quite came together, as I over-estimated my abilities to hack both mainframes and microcomputers at the same time...
 

Offline aandrew

  • Frequent Contributor
  • **
  • Posts: 280
  • Country: ca
Re: General C programming question
« Reply #70 on: July 28, 2018, 01:50:20 pm »
So if someone foolishly places all the functions in a large library in one file, the system has to allocate space for the entire library to run the program.  I've had to fix this when taking over million line codebases by writing a program to break the file up into pieces.  I did it because people were unable to run the programs because they ran out of memory on $30-40K workstations.

This hasn't been true for quite some time now. Your experience is valid but dated.

gcc has -ffunction-sections -fdata-sections which break every function and data into separate linker sections, and then the linker option -Wl,--gc-sections which removes all unused sections. I've used this for at least the last 5 years to keep my binaries small without resorting to one-func-per-file which is a practice I abhor because it leads to the hundred-file-project which is impossible to keep in my headspace.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf