Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!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 04:31:14 -0000 (UTC) Organization: A noiseless patient Spider Lines: 46 Message-ID: <20250502211854.795@kylheku.com> References: <51ba1k5h5lkj75qvfuj0ferlddpb6bi0n8@4ax.com> Injection-Date: Sat, 03 May 2025 06:31:15 +0200 (CEST) Injection-Info: dont-email.me; posting-host="5e2693d8db794596c9e694e70f971fe4"; logging-data="2901160"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+V+R6Dg+572XkAS2kFXmBlebeFVI6TTk8=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:s77tqH39hw9Rk73n8/HRlRPdEQY= Bytes: 3171 On 2025-05-03, Lew Pitcher wrote: > If it is legal, then why isn't it used more often? Is it a readability/ > maintainability issue, or is it something else? It's not used more often because - programmers are irrationallyafraid that there will be lots of overhead when the structure gets large. (In fact, passing large structs by value is implemented using a hidden pointer; a copy is not made unless the calee modifies the parameter. Returning likewise: caller passes a pointer to a location where to place the structure. It is not as bad as you might think.) Some of the fear is rational: you usually have a very good idea how pointer passing is implemented, but not necessarily how struct passing is. - 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"). - ABI concerns might prevent you from writing a public API that has functions that take and/or return structs by value for fear of causing complications to FFI users, or incompatibilities when differnet compiler are used. (We have such APIs in ISO C, though: ldiv and friends.) (In POSIX, I seem to recall, some platforms have made pthread_t a struct. Glibc on Linux might have had it that way? I think it's an unsigned int or long now. Anyway, in those implementations, pthread_equal takes two structs by value. And, aha, that's why there is pthread_equal; because pthread_t types might not be scalar values comparable with ==: i.e. pointers or integers.) -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca