ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
There would, of course, have to be a third line in that example, to finish the thought:
*refobj = swap;
These are simple assignment-statements; nothing "recursive" about it. (C is not that clever.) A block of memory (referenced by a pointer) is copied to a temporary location (which is local), then it is assigned from a location referenced by a second pointer. Then, presumably, the second location will next be assigned from the temporary. Thus swapping the content of the two locations.
Interesting expansion of that. Where is it taking the memory to perform that? I.e. if one wishes to not use as much stack, they could code it as: (Providing obj and refobj were properly initialized that is)
Yeah, you're right I should've tried the code before suggesting it. My question really was where the memory comes from, but now it appears the answer is that you have to have at least one place to put it; in this case on the stack. If one desires a non-stack option, then they would have to either scope swap higher than the function or allocate *swap from within the function.
These are simple assignment-statements; nothing "recursive" about it.
By recursive I meant that assigning one structure to another is equivalent, apart from potential differences in padding bytes, to assigning each member of the structure to corresponding member of the other structure. If such a member is structure itself, this rule is applied recursively.
Compilers end up doing equivalent of memcpy, even though I think they would be allowed not to copy padding bytes.
... and the reason why I sought to very-politely correct you on that point is ... "C" is just not that smart. To "C," a structure is a contiguous block of memory; nothing more or less. It's exactly like a simple scalar variable, only bigger. (Recall that "C" doesn't even have a proper notion of a "string," only pchar.)
So, in C, if you assign something to something-else, a contiguous range of bytes gets copied verbatim from here to there; nothing more or less. Hence, no opportunity for "recursion," which implies deep-knowledge of what the contents of the structure actually represent. Which is exactly what other languages, including C++, can do. In "C," it's just an overglorified memcpy().
In the case at bar, the temporary, being a local variable, lives on the stack. The other blocks of storage are accessed by means of pointers which also live on the stack.
So, in C, if you assign something to something-else, a contiguous range of bytes gets copied verbatim from here to there; nothing more or less. Hence, no opportunity for "recursion," which implies deep-knowledge of what the contents of the structure actually represent. Which is exactly what other languages, including C++, can do. In "C," it's just an overglorified memcpy().
This is clearly not true:
Code:
long long a;
unsigned char b;
b = a;
a = b;
Here's a relevant portion of the C99 standard:
Quote:
Originally Posted by ISO/IEEC 9899:TC3
§6.5.16.1¶2 In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.
Performing a conversion implies understanding of the type.
Yeah, you're right I should've tried the code before suggesting it. My question really was where the memory comes from, but now it appears the answer is that you have to have at least one place to put it; in this case on the stack. If one desires a non-stack option, then they would have to either scope swap higher than the function or allocate *swap from within the function.
The memory came from the calling function. <passing argument 1> and <passing argument 2> are pointers to the memory used by the calling function which this function refers to to do the swapping. So the memory could have been on the stack, heap or static data depending on how the calling function chose to allocate the memory.
(Note that swap was allocated on the stack by this function).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.