Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.c Subject: Re: That depends... (Was: Regarding assignment to struct) Date: Sat, 3 May 2025 05:11:25 -0000 (UTC) Organization: A noiseless patient Spider Lines: 35 Message-ID: <20250502220618.480@kylheku.com> References: <51ba1k5h5lkj75qvfuj0ferlddpb6bi0n8@4ax.com> <20250502211854.795@kylheku.com> Injection-Date: Sat, 03 May 2025 07:11:29 +0200 (CEST) Injection-Info: dont-email.me; posting-host="5e2693d8db794596c9e694e70f971fe4"; logging-data="2979503"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jT1+HH+UC43eJrhpkdzj7Tb7CJqBXer4=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:oyHoN+fAfAReRSXpvpO5NwjmSUg= Bytes: 2569 On 2025-05-03, Kaz Kylheku <643-408-1753@kylheku.com> wrote: > - some structures cannot be copied; duplicating objects can > be nontrivial when they manage resources or are sensitive > to their own address (like have pointers to parts of themselves). > E.g. you can't just have a FILE parameter and pass (*stdout) to it. > Under object-based/oriented programming in C, we usually pass > around pointers to objects. Treating OOP objects by value requires > techniques found in C++: you need handlers to be invoked when > objects are copied ("copy constructors"). Expanding on this point, C++ goes to town with pass-by-value interfaces involving objects. For instance the std::basic_string template allows C++ programs to treat dynamic strings as just values to be tossed around: std::string path_combine(std::string dir, std::string name) { return dir + "/" + name; } C++ experience informs us that you can get away with doing this with PODs (plain old datatypes---structs with no constructors or destructors) only to a point. But in C, that's all you have. You are limited in C by not being able to endow the type with the brains for what to do when it is copied: like for instance to share a pointer to some associated data with the new copy, bumping up a reference count. That tends to exert "downward pressure" on the technique. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca