Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 28 |
Nodes: | 6 (0 / 6) |
Uptime: | 62:01:00 |
Calls: | 424 |
Calls today: | 2 |
Files: | 1,025 |
Messages: | 91,150 |
Posted today: | 1 |
Wouldn't it be quicker and easier to write a simple program to test
this rather than wait for someone to compose a response? You already
have 90% of the code written.
In article <51ba1k5h5lkj75qvfuj0ferlddpb6bi0n8@4ax.com>,
Barry Schwarz <schwarzb@delq.com> wrote:
...
Wouldn't it be quicker and easier to write a simple program to test
this rather than wait for someone to compose a response? You already
have 90% of the code written.
That depends on what the actual personal goal is.
Somehow, I don' think Lew's goal(s) (and reason for posting) are what you think they are.
But... I remembered this (apparently) seldom-used feature of being able
to pass a struct by value up and down the chain (down, as a function argument, up as a function return value). As I've not done this before,
and I've not /seen/ it done in other peoples code, I wonder how viable
a solution it might be.
[...]
My goal is to add a feature to one of my older programs. The feature will require passing a number of objects down and up the function call chain, and I'm looking at various alternatives to accomplish this.
ATM, it looks like I either go with "global" objects, or I change the argument list of several functions to include about 5 more objects each. Because these additional objects all relate to one another, I'm thinking
of grouping them in a struct, and passing a pointer to the struct back
and forth.
But... I remembered this (apparently) seldom-used feature of being able
to pass a struct by value up and down the chain (down, as a function argument, up as a function return value). As I've not done this before,
and I've not /seen/ it done in other peoples code, I wonder how viable
a solution it might be.
If it is legal, then why isn't it used more often?
Is it a readability/
maintainability issue, or is it something else?
- 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").
But... I remembered this (apparently) seldom-used feature of being able
to pass a struct by value up and down the chain (down, as a function argument, up as a function return value). As I've not done this before,
and I've not /seen/ it done in other peoples code, I wonder how viable
a solution it might be.
If it is legal, then why isn't it used more often? Is it a readability/ maintainability issue, or is it something else?
If it is legal, then why isn't it used more often? Is it a readability/ maintainability issue, or is it something else?
On Fri, 02 May 2025 20:44:45 +0000, Kenny McCormack wrote:
In article <51ba1k5h5lkj75qvfuj0ferlddpb6bi0n8@4ax.com>,
Barry Schwarz <schwarzb@delq.com> wrote:
...
Wouldn't it be quicker and easier to write a simple program to test
this rather than wait for someone to compose a response? You already
have 90% of the code written.
That depends on what the actual personal goal is.
Somehow, I don' think Lew's goal(s) (and reason for posting) are what you
think they are.
My goal is to add a feature to one of my older programs. The feature will require passing a number of objects down and up the function call chain, and I'm looking at various alternatives to accomplish this.
ATM, it looks like I either go with "global" objects, or I change the argument list of several functions to include about 5 more objects each. Because these additional objects all relate to one another, I'm thinking
of grouping them in a struct, and passing a pointer to the struct back
and forth.
But... I remembered this (apparently) seldom-used feature of being able
to pass a struct by value up and down the chain (down, as a function argument, up as a function return value). As I've not done this before,
and I've not /seen/ it done in other peoples code, I wonder how viable
a solution it might be.
If it is legal, then why isn't it used more often? Is it a readability/ maintainability issue, or is it something else?
On 2025-05-03, Lew Pitcher <lew.pitcher@digitalfreehold.ca> 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.
For what it's worth, those are my thoughts on the question.
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.
[...]
On 03.05.2025 07:11, Kaz Kylheku wrote:
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;
}
Hmm.. - my C++ days were long ago but I seem to recall that the
suggestion for passing non-trivial objects by-value was that they
should instead be passed by 'const &' (as a "quasi-by-value") for
performance reasons.