What is Ref?

Exactly my thoughts. I was thinking that a nobroadcast would be less confusing to many, but well Ref is much shorter.

I now wonder whether there is a performance benefit of explicitly passing Ref around. Or maybe inside recursion where Julia shouldn’t modify copies

1 Like

I don’t think C struct assignments work as you suggest. The struct variable is just a pointer to the head of the struct. Maybe it works differently in C++ . . .

1 Like

I do believe I am correct. In fact, the common C idiom of not declaring a struct in local scope, but instead a pointer to it, and then using malloc to obtain a memory location for the struct (and an address to the pointer), exists exactly because of the C memory model I mentioned.

I was wrong, sorry. Haven’t programmed in C in ages. I’m surprised that it allows block copy of struct contents with =. Was it always this way? IIRC, you can’t (or couldn’t) do this with strings or other arrays. In those cases, the variable name truly is the pointer to the first array element, right?

In C there is no “string” type there’s just “pointer to char”

1 Like

Ah, now I understand the source of the confusion, more than ten years ago I have studied deeply the C99 standard and no other, however there are even older standards, of course. I just checked and my C99 Reference book says previous compilers did not allow struct and union attribution, so yes, it was not always possible.

You cannot do this with arrays (in C99, I’m not up to par with newer standards), but this has nothing to do with what is in the variable, local variable declared to be arrays are allocated in the stack and “popped up” after the function finishes, so technically, they contain the whole object inside themselves (not a pointer), it is just that attribution is not defined for them (will give an error). You can check they actually contain the whole object in themselves by the amount of C threads in forums that just boil down to “I returned a pointer from an array variable I had declared inside the function and outside the function it does not work (has other values, gives segfault, etc…).”

Strings in C are “always” char *, so they are always a pointer, and act accordingly (I mean, you can declare a string as an array of char, but then everything that deals with strings will only want the address of the first character and in the end it will be the same as having defined it as a pointer from the start).

Fascinating trying to recollect how things used to be. I learned C on the job starting in 1984. At that time, apparently, C compilers were generally allowing struct assignment with =, but it was not allowed according to the ‘bible’ (Kernighan & Ritchie’s The C Programming Language), which I studied. A detailed discussion of the history is at

https://stackoverflow.com/questions/17513736/what-does-it-mean-that-automatic-structures-and-arrays-may-now-also-be-initializ

and at

https://www.thecodingforums.com/threads/struct-assignment-historical-question.443010/

You also couldn’t pass structures by value when calling a function, according to K&R. An interesting quote from Larry Jones:

struct parameters, return values, and assignment were added to the Unix C compilers just about the time that K&R was actually published (the “Recent Changes to C” paper noting them is dated Nov. 15, 1978), leading to a great deal of frustration many years later when MS-DOS compilers appeared that implemented K&R exactly and thus didn’t support them.