Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #160147 > unrolled thread
| Started by | Manfred <noname@add.invalid> |
|---|---|
| First post | 2021-04-17 19:12 +0200 |
| Last post | 2021-04-28 00:02 -0700 |
| Articles | 9 on this page of 89 — 20 participants |
Back to article view | Back to comp.lang.c
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: No, C is not a simple language Manfred <noname@add.invalid> - 2021-04-17 19:12 +0200
Re: No, C is not a simple language silas poulson <Silas8642@hotmail.co.uk> - 2021-04-17 18:33 -0700
Re: No, C is not a simple language Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-04-18 02:10 +0000
Re: No, C is not a simple language silas poulson <Silas8642@hotmail.co.uk> - 2021-04-17 22:35 -0700
Re: No, C is not a simple language Real Troll <real.troll@trolls.com> - 2021-04-18 17:00 +0100
Re: No, C is not a simple language jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-18 21:52 +0200
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-18 16:46 +0000
Re: No, C is not a simple language Manfred <noname@add.invalid> - 2021-04-18 20:30 +0200
Re: No, C is not a simple language Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-18 12:46 -0700
Re: No, C is not a simple language Manfred <noname@add.invalid> - 2021-04-18 22:53 +0200
Re: No, C is not a simple language Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-04-19 04:20 -0700
Re: No, C is not a simple language Thiago Adams <thiago.adams@gmail.com> - 2021-04-19 08:29 -0700
Re: No, C is not a simple language Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-19 13:21 -0700
Re: No, C is not a simple language Richard Damon <Richard@Damon-Family.org> - 2021-04-19 18:03 -0400
Re: No, C is not a simple language Bart <bc@freeuk.com> - 2021-04-20 00:50 +0100
Re: No, C is not a simple language David Brown <david.brown@hesbynett.no> - 2021-04-20 08:32 +0200
Re: No, C is not a simple language Richard Damon <Richard@Damon-Family.org> - 2021-04-20 07:17 -0400
Re: No, C is not a simple language David Brown <david.brown@hesbynett.no> - 2021-04-20 14:25 +0200
Re: No, C is not a simple language Bart <bc@freeuk.com> - 2021-04-20 14:09 +0100
Re: No, C is not a simple language Richard Damon <Richard@Damon-Family.org> - 2021-04-20 10:19 -0400
Re: No, C is not a simple language James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-20 11:50 -0400
Re: No, C is not a simple language Richard Damon <Richard@Damon-Family.org> - 2021-04-20 12:06 -0400
Re: No, C is not a simple language James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-20 19:13 -0400
Re: No, C is not a simple language Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-05-11 05:29 -0700
Re: No, C is not a simple language David Brown <david.brown@hesbynett.no> - 2021-05-11 14:37 +0200
Re: No, C is not a simple language Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-05-11 05:18 -0700
Re: No, C is not a simple language Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-05-11 05:14 -0700
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-19 07:18 +0000
Re: No, C is not a simple language Manfred <noname@add.invalid> - 2021-04-19 17:15 +0200
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-20 04:05 +0000
Re: No, C is not a simple language Manfred <noname@add.invalid> - 2021-04-20 18:27 +0200
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-20 16:41 +0000
Re: No, C is not a simple language jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-20 19:44 +0200
Re: No, C is not a simple language Thiago Adams <thiago.adams@gmail.com> - 2021-04-20 11:04 -0700
Re: No, C is not a simple language jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-20 20:23 +0200
Re: No, C is not a simple language Thiago Adams <thiago.adams@gmail.com> - 2021-04-20 12:22 -0700
Re: No, C is not a simple language Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-20 19:37 +0000
Re: No, C is not a simple language Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-20 18:30 +0000
Re: No, C is not a simple language jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-20 20:47 +0200
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-20 13:29 -0700
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-21 04:37 +0000
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-21 15:26 -0700
Re: No, C is not a simple language Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-04-22 05:59 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-22 11:58 -0700
Re: No, C is not a simple language Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-04-23 07:23 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-24 14:26 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-24 14:28 -0700
Re: No, C is not a simple language Richard Damon <Richard@Damon-Family.org> - 2021-04-20 14:46 -0400
Re: No, C is not a simple language Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-20 20:18 +0000
Re: No, C is not a simple language Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-05-11 05:31 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-20 13:19 -0700
Re: No, C is not a simple language Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-20 20:29 +0000
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-20 13:39 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-20 14:29 -0700
Re: No, C is not a simple language Ian Collins <ian-news@hotmail.com> - 2021-04-21 08:56 +1200
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-20 14:07 -0700
Re: No, C is not a simple language jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-23 12:10 +0200
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 12:37 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 12:38 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 12:43 -0700
Re: No, C is not a simple language jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-23 22:38 +0200
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 13:44 -0700
Re: No, C is not a simple language Ian Collins <ian-news@hotmail.com> - 2021-04-24 10:56 +1200
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 16:09 -0700
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-26 09:30 +0000
Re: No, C is not a simple language Ian Collins <ian-news@hotmail.com> - 2021-04-30 21:11 +1200
Re: No, C is not a simple language Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-24 03:51 +0000
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-24 12:48 -0700
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-26 09:24 +0000
Re: No, C is not a simple language jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-26 21:24 +0200
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-27 07:26 +0000
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-26 09:27 +0000
Re: No, C is not a simple language "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2021-04-20 19:53 +0200
Re: No, C is not a simple language Thiago Adams <thiago.adams@gmail.com> - 2021-04-20 11:04 -0700
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-26 09:02 +0000
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-26 09:43 +0000
Re: No, C is not a simple language Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-04-26 03:08 -0700
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-26 10:35 +0000
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-26 13:08 +0000
Re: No, C is not a simple language Richard Damon <Richard@Damon-Family.org> - 2021-04-26 09:30 -0400
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-26 14:22 +0000
Re: No, C is not a simple language scott@slp53.sl.home (Scott Lurndal) - 2021-04-26 18:57 +0000
Re: No, C is not a simple language Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-04-26 06:55 -0700
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-26 14:15 +0000
Re: No, C is not a simple language Juha Nieminen <nospam@thanks.invalid> - 2021-04-27 04:37 +0000
Re: No, C is not a simple language Robert Latest <boblatest@yahoo.com> - 2021-04-27 07:22 +0000
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-19 21:42 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-22 00:26 -0700
Re: No, C is not a simple language "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-28 00:02 -0700
Page 5 of 5 — ← Prev page 1 2 3 4 [5]
| From | Robert Latest <boblatest@yahoo.com> |
|---|---|
| Date | 2021-04-26 14:22 +0000 |
| Message-ID | <ieo0kqFn0d5U3@mid.individual.net> |
| In reply to | #160357 |
Richard Damon wrote: > That is also, if you really think about it, a major breaking change in > the definition of the struct. Absolutely. This is why if you write a library to be used by someone else, and which uses a struct type in its API, you should provide creation and destruction functions for that struct. As long as your struct is simple, this is trivial. When it grows in complexity, you are thankful for your foresight. -- robert
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2021-04-26 18:57 +0000 |
| Message-ID | <l2EhI.42463$9F5.13178@fx43.iad> |
| In reply to | #160360 |
Robert Latest <boblatest@yahoo.com> writes: >Richard Damon wrote: >> That is also, if you really think about it, a major breaking change in >> the definition of the struct. > >Absolutely. This is why if you write a library to be used by someone else, and >which uses a struct type in its API, you should provide creation and >destruction functions for that struct. As long as your struct is simple, this >is trivial. When it grows in complexity, you are thankful for your foresight. Add padding to the struct, and if necessary a version field. As functionality is added ot the struct over time, the version field indicates what level of functionality is provided and the padding allows future expansion without breaking existing linked applications.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.arthur.mclean@gmail.com> |
|---|---|
| Date | 2021-04-26 06:55 -0700 |
| Message-ID | <e045a547-399a-416c-95cb-92a302a9a3can@googlegroups.com> |
| In reply to | #160355 |
On Monday, 26 April 2021 at 14:08:12 UTC+1, Juha Nieminen wrote: > In comp.lang.c++ Robert Latest <bobl...@yahoo.com> wrote: > > ["Followup-To:" header set to comp.lang.c.] > > Juha Nieminen wrote: > >> In comp.lang.c++ Robert Latest <bobl...@yahoo.com> wrote: > >>> Your argument above is based on NOT designing your data structures correctly > >>> in C, and now you say C++ handles better but only IF you do it correctly? > >>> Doesn't make sense. > >> > >> Are you telling be that every single time you create a struct in a C program > >> you always write a "constructor" and "destructor" for it, > > > > Of course not. > That's exactly my point: > > Suppose you have a huge amount of code, with lots and lots of structs, structs > as members of other structs, and a lot of code that uses those structs. > > Then one day you decide to add a dynamically allocated array (such as a > dynamically allocated string) to one of the structs, which is used in > lots of other structs, which themselves are used in other structs, and > so on... > > Because of a chance in this one struct, suddenly lots of other structs > "inherit" its need for manual construction and destruction, and tons of > code that uses those other structs need to be refactored to now call the > constructors and destructors of all the structs that now require it. > All because of this one struct. > This can happen. One case is where a string is changed from a fixed-width field to a char *. But a catastropic propagation of the change is unlikely. That's because most structs are either inherently unlikely to change during the lifetime of the project, or they are only used in a few places in relatively shallow hierachies. Or they are aleady encapsulated with destructors.
[toc] | [prev] | [next] | [standalone]
| From | Robert Latest <boblatest@yahoo.com> |
|---|---|
| Date | 2021-04-26 14:15 +0000 |
| Message-ID | <ieo07nFn0d5U2@mid.individual.net> |
| In reply to | #160355 |
["Followup-To:" header set to comp.lang.c.] Juha Nieminen wrote: > > Suppose you have a huge amount of code, with lots and lots of structs, > structs as members of other structs, and a lot of code that uses those > structs. In other words, a mess. > Then one day you decide to add a dynamically allocated array (such as a > dynamically allocated string) to one of the structs, which is used in lots of > other structs, which themselves are used in other structs, and so on... I get your point. BTW I take "lots of structs" to mean "lots of struct types," not "lots of instances" > Because of a chance in this one struct, suddenly lots of other structs > "inherit" its need for manual construction and destruction, and tons of code > that uses those other structs need to be refactored to now call the > constructors and destructors of all the structs that now require it. All > because of this one struct. Correct. At fault is not the language, nor the lack of a garbage collector, but an ill-designed and unmaintainable pile of code which is badly in need of refactoring anyway. Are you speaking of a real-life example? Dozens of different struct types that all include one another and none of which (originally) have dynamically allocated members? -- robert
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-04-27 04:37 +0000 |
| Message-ID | <s684eu$qg3$1@gioia.aioe.org> |
| In reply to | #160359 |
In comp.lang.c++ Robert Latest <boblatest@yahoo.com> wrote: >> Because of a chance in this one struct, suddenly lots of other structs >> "inherit" its need for manual construction and destruction, and tons of code >> that uses those other structs need to be refactored to now call the >> constructors and destructors of all the structs that now require it. All >> because of this one struct. > > Correct. At fault is not the language, nor the lack of a garbage collector, but > an ill-designed and unmaintainable pile of code which is badly in need of > refactoring anyway. Are you speaking of a real-life example? Dozens of > different struct types that all include one another and none of which > (originally) have dynamically allocated members? But you yourself said that you don't automatically add manual constructors and destructors to every struct you write, just in case. I don't even understand what you mean by "well-designed code" then. Is "well-designed" C code one where every struct has a constructor and destructor function (that you always call for every single instance)? Or is it something else?
[toc] | [prev] | [next] | [standalone]
| From | Robert Latest <boblatest@yahoo.com> |
|---|---|
| Date | 2021-04-27 07:22 +0000 |
| Message-ID | <iepse2F3s1nU1@mid.individual.net> |
| In reply to | #160396 |
["Followup-To:" header set to comp.lang.c.] Juha Nieminen wrote: > In comp.lang.c++ Robert Latest <boblatest@yahoo.com> wrote: >>> Because of a chance in this one struct, suddenly lots of other structs >>> "inherit" its need for manual construction and destruction, and tons of >>> code that uses those other structs need to be refactored to now call the >>> constructors and destructors of all the structs that now require it. All >>> because of this one struct. >> >> Correct. At fault is not the language, nor the lack of a garbage collector, >> but an ill-designed and unmaintainable pile of code which is badly in need >> of refactoring anyway. Are you speaking of a real-life example? Dozens of >> different struct types that all include one another and none of which >> (originally) have dynamically allocated members? > > But you yourself said that you don't automatically add manual constructors > and destructors to every struct you write, just in case. Of course not. When you design a struct type you should already know what it is supposed to represent and if the represented things lend themselves to dynamic allocation. If so, better write construction / deconstriction functions. It hardly adds significant amounts of code > I don't even understand what you mean by "well-designed code" then. Is > "well-designed" C code one where every struct has a constructor and > destructor function (that you always call for every single instance)? Or is > it something else? A certain amount of foresight is part of good planning. If you jump straight into coding without paying attention to possible issues of extending and re-use you may face some refactoring effort later. This of course goes for anything, not just changing from static to dynamic allocation in C. I suspect you once had to refactor some code with a few structs because you forgot this planning and have now worked yourself into a fret about some contrived example with "lots and lots" of struct types that need to be adapted. I don't believe it's a real world case, but if it is, it's poorly planned. -- robert
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2021-04-19 21:42 -0700 |
| Message-ID | <s5lm36$1o58$1@gioia.aioe.org> |
| In reply to | #160147 |
On 4/17/2021 10:12 AM, Manfred wrote:
> (cross-posting to comp.lang.c to let the heat fire up)
>
> On 4/17/2021 7:56 AM, Juha Nieminen wrote:
>> I know that this is probably going to piss off avid C programmers,
>> but I don't really care all that much.
>
> Excellent start
>
> [...]
>>
>> I myself happen to know both languages quite well, and in fact I
>> program in both languages as my profession, and I would say that
>> I am very knowledgeable and proficient in both languages and have
>> no trouble in developing in either one. I am of the strong opinion
>> that no, C is not a "simple" language, and that C++ makes so many
>> things so much simpler, easier and safer.
>>
>> I was reminded of this in a quite concrete manner recently when I was
>> trying to help a beginner programmer who had decided to try his hands
>> on C, and wanted to create a simple text editor, which would read a
>> text file, allow the user to edit the text in several ways, and to
>> save the file.
>>
>> It quite quickly turned very frustrating for *both* of us ...
>
> The first comment that comes to mind is that probably part of the
> frustration stems from your approach (no offense) to try and use in C a
> design that would be suitable for C++ instead. Despite their name, the
> two are quite different languages. The similarities are about their
> runtime footprint, not about how they are used.
>
> For example, at a first glance the design of string object is not that
> great for C.
> A common pattern that pops to the mind is to use opaque structs (see
> e.g. FILE*), and have something like:
>
> typedef struct string_t STRING;
>
> STRING* alloc_string(); // creates the empty string
> STRING* set_string(STRING* s, const char* value);
> void free_string(STRING* s);
> STRING* concat_string(STRING* s1, STRING* s2);
> ...
>
> This way there would be no ambiguity about how the STRING object has to
> be used.
> And yes, in C you have to do all yourself - C is a low level language,
> and it is meant for low level stuff. It is hardly the right tool for
> anything meant to interface with a human user - it is (still) great for
> system programming and back-end processing, IMO.
>
> As second, I agree with Jacob that a text editor is a simple tool, but
> it is not easy to make expecially for a beginner.
>
>
> [snip]
I would not call C easy. Instead, I would refer to it as being more to
"the point", perhaps? Things have to be "explicit", it can be more
verbose. I say that because C does not have RAII. Also, C is really nice
to create plugins wrt creating portable bindings to other languages.
Usually in C, things tend go like the following when coding up a new
"object", so to speak:
Typing in the newsreader, sorry for typos.
________________________________
#include <stdio.h>
#include <stdlib.h>
struct foo
{
int a;
void* mem;
};
int foo_create(struct foo* const self)
{
if ((self->mem = malloc(sizeof(42))))
{
self->a = 84;
printf("foo_create(%p)\n", (void*)self);
return 1;
}
return 0;
}
void foo_destroy(struct foo const* const self)
{
printf("foo_destroy(%p)\n", (void*)self);
free(self->mem);
}
void foo_output(struct foo const* self, FILE* output)
{
printf("foo_output(%p)\n", (void*)self);
fprintf(output, "struct foo(%p)->a = %d\n", (void*)self, self->a);
fprintf(output, "struct foo(%p)->mem = %p\n", (void*)self, self->mem);
}
int main()
{
struct foo foo;
if (foo_create(&foo))
{
foo_output(&foo, stdout);
foo_destroy(&foo);
}
return 0;
}
________________________________
See how I have to make an explicit call to foo_destroy? This can be
handled in C++ in a "cleaner/safer" manner. Again, typing in the
newsreader sorry for typos. This would be a simple C++ wrapper to the C
API above:
________________________________
struct foo_wrapper
{
struct foo m_foo;
foo_wrapper() { if (! foo_create(&m_foo)) throw; }
~foo_wrapper() { foo_destroy(&m_foo); }
void output(FILE* output_) const
{
foo_output(&m_foo, output_);
}
};
________________________________
Then we can go:
{
foo_wrapper foo;
foo.output(stdout);
}
There. If it fails to create a foo in foo_create, it throws. Otherwise,
foo_destroy is guaranteed to be called when foo goes out of scope. So,
C++ can be more convenient, and safer. No need to use all of C++, but it
does have its moments.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2021-04-22 00:26 -0700 |
| Message-ID | <s5r8fk$1pn0$1@gioia.aioe.org> |
| In reply to | #160235 |
On 4/19/2021 9:42 PM, Chris M. Thomasson wrote:
> On 4/17/2021 10:12 AM, Manfred wrote:
>> (cross-posting to comp.lang.c to let the heat fire up)
>>
>> On 4/17/2021 7:56 AM, Juha Nieminen wrote:
>>> I know that this is probably going to piss off avid C programmers,
>>> but I don't really care all that much.
>>
>> Excellent start
>>
>> [...]
>>>
>>> I myself happen to know both languages quite well, and in fact I
>>> program in both languages as my profession, and I would say that
>>> I am very knowledgeable and proficient in both languages and have
>>> no trouble in developing in either one. I am of the strong opinion
>>> that no, C is not a "simple" language, and that C++ makes so many
>>> things so much simpler, easier and safer.
>>>
>>> I was reminded of this in a quite concrete manner recently when I was
>>> trying to help a beginner programmer who had decided to try his hands
>>> on C, and wanted to create a simple text editor, which would read a
>>> text file, allow the user to edit the text in several ways, and to
>>> save the file.
>>>
>>> It quite quickly turned very frustrating for *both* of us ...
>>
>> The first comment that comes to mind is that probably part of the
>> frustration stems from your approach (no offense) to try and use in C
>> a design that would be suitable for C++ instead. Despite their name,
>> the two are quite different languages. The similarities are about
>> their runtime footprint, not about how they are used.
>>
>> For example, at a first glance the design of string object is not that
>> great for C.
>> A common pattern that pops to the mind is to use opaque structs (see
>> e.g. FILE*), and have something like:
>>
>> typedef struct string_t STRING;
>>
>> STRING* alloc_string(); // creates the empty string
>> STRING* set_string(STRING* s, const char* value);
>> void free_string(STRING* s);
>> STRING* concat_string(STRING* s1, STRING* s2);
>> ...
>>
>> This way there would be no ambiguity about how the STRING object has
>> to be used.
>> And yes, in C you have to do all yourself - C is a low level language,
>> and it is meant for low level stuff. It is hardly the right tool for
>> anything meant to interface with a human user - it is (still) great
>> for system programming and back-end processing, IMO.
>>
>> As second, I agree with Jacob that a text editor is a simple tool, but
>> it is not easy to make expecially for a beginner.
>>
>>
>> [snip]
>
> I would not call C easy. Instead, I would refer to it as being more to
> "the point", perhaps? Things have to be "explicit", it can be more
> verbose. I say that because C does not have RAII. Also, C is really nice
> to create plugins wrt creating portable bindings to other languages.
> Usually in C, things tend go like the following when coding up a new
> "object", so to speak:
>
> Typing in the newsreader, sorry for typos.
> ________________________________
>
> #include <stdio.h>
> #include <stdlib.h>
>
>
> struct foo
> {
> int a;
> void* mem;
> };
>
> int foo_create(struct foo* const self)
> {
> if ((self->mem = malloc(sizeof(42))))
> {
> self->a = 84;
> printf("foo_create(%p)\n", (void*)self);
> return 1;
> }
>
> return 0;
> }
>
> void foo_destroy(struct foo const* const self)
> {
> printf("foo_destroy(%p)\n", (void*)self);
> free(self->mem);
> }
>
> void foo_output(struct foo const* self, FILE* output)
> {
> printf("foo_output(%p)\n", (void*)self);
> fprintf(output, "struct foo(%p)->a = %d\n", (void*)self, self->a);
> fprintf(output, "struct foo(%p)->mem = %p\n", (void*)self, self->mem);
> }
>
>
> int main()
> {
> struct foo foo;
>
> if (foo_create(&foo))
> {
> foo_output(&foo, stdout);
>
> foo_destroy(&foo);
> }
>
> return 0;
> }
> ________________________________
>
>
> See how I have to make an explicit call to foo_destroy? This can be
> handled in C++ in a "cleaner/safer" manner. Again, typing in the
> newsreader sorry for typos. This would be a simple C++ wrapper to the C
> API above:
> ________________________________
>
> struct foo_wrapper
> {
> struct foo m_foo;
>
> foo_wrapper() { if (! foo_create(&m_foo)) throw; }
> ~foo_wrapper() { foo_destroy(&m_foo); }
>
> void output(FILE* output_) const
> {
> foo_output(&m_foo, output_);
> }
> };
>
> ________________________________
>
> Then we can go:
>
> {
> foo_wrapper foo;
> foo.output(stdout);
> }
>
> There. If it fails to create a foo in foo_create, it throws. Otherwise,
> foo_destroy is guaranteed to be called when foo goes out of scope. So,
> C++ can be more convenient, and safer. No need to use all of C++, but it
> does have its moments.
First learning about RAII, even when I learned about ScopeGuard, back in
my C++ days, for some reason, makes me think of the following song:
https://youtu.be/UZ2-FfXZlAU
Even though I was working with C/C++ well before this song was created,
it makes think way back when I was a little kid using basic on my Atari.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2021-04-28 00:02 -0700 |
| Message-ID | <s6b1a3$152b$1@gioia.aioe.org> |
| In reply to | #160147 |
On 4/17/2021 10:12 AM, Manfred wrote: > (cross-posting to comp.lang.c to let the heat fire up) > > On 4/17/2021 7:56 AM, Juha Nieminen wrote: >> I know that this is probably going to piss off avid C programmers, >> but I don't really care all that much. > > Excellent start > > [...] >> >> I myself happen to know both languages quite well, and in fact I >> program in both languages as my profession, and I would say that >> I am very knowledgeable and proficient in both languages and have >> no trouble in developing in either one. I am of the strong opinion >> that no, C is not a "simple" language, and that C++ makes so many >> things so much simpler, easier and safer. >> >> I was reminded of this in a quite concrete manner recently when I was >> trying to help a beginner programmer who had decided to try his hands >> on C, and wanted to create a simple text editor, which would read a >> text file, allow the user to edit the text in several ways, and to >> save the file. >> >> It quite quickly turned very frustrating for *both* of us ... > > The first comment that comes to mind is that probably part of the > frustration stems from your approach (no offense) to try and use in C a > design that would be suitable for C++ instead. Despite their name, the > two are quite different languages. The similarities are about their > runtime footprint, not about how they are used. > > For example, at a first glance the design of string object is not that > great for C. > A common pattern that pops to the mind is to use opaque structs (see > e.g. FILE*), and have something like: > > typedef struct string_t STRING; > > STRING* alloc_string(); // creates the empty string > STRING* set_string(STRING* s, const char* value); > void free_string(STRING* s); > STRING* concat_string(STRING* s1, STRING* s2); > ... > > This way there would be no ambiguity about how the STRING object has to > be used. > And yes, in C you have to do all yourself - C is a low level language, > and it is meant for low level stuff. It is hardly the right tool for > anything meant to interface with a human user - it is (still) great for > system programming and back-end processing, IMO. > > As second, I agree with Jacob that a text editor is a simple tool, but > it is not easy to make expecially for a beginner. > > > [snip] Wrt using GC as the everlasting crutch: https://youtu.be/ao-Sahfy7Hg
[toc] | [prev] | [standalone]
Page 5 of 5 — ← Prev page 1 2 3 4 [5]
Back to top | Article view | comp.lang.c
csiph-web