Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I must admit that even with the additional information you provided, I don't fully understand your question. But <strong>let me say something general about the stack and the heap and C++:</strong></p> <p><strong>How an argument gets passed to the function is an implementation detail</strong> of a particular C++ compiler, ie. this can vary from one C++ compiler to the next. The method by which arguments are passed into functions is called a <em><strong>calling convention</strong></em>. Some compilers use neither the stack nor the heap for argument passing; instead, they use CPU registers if possible. (Watcom's C++ compiler is/was a prominent example of a compiler that favours registers to pass arguments to a function.)</p> <p>This means that any C++ compiler may create binaries that are not <em><strong>binary-compatible</strong></em> to another C++ compiler. (The C++ language standard does not prescribe a binary standard for compiled output, therefore different compilers may produce binary-incompatible output; but any C++ compiler is guaranteed to be binary-compatible at least to itself.) Thus, if you want to use a library, you have three options:</p> <ul> <li><p>choose a binary that matches your particular C++ compiler and linker;</p></li> <li><p>compile the library sources yourself, using your compiler; or </p></li> <li><p>choose a library binary that adheres to a certain binary standard (such as the DLL library format or Microsoft's COM standard) which is also supported by your C++ compiler and linker.</p></li> </ul> <p>In conclusion, <strong>your question about stack object vs. heap object does not make sense</strong>. There is no such thing as a "stack object" in C++. You have no explicit control how an argument is passed to a function, because that is something that the C++ compiler will decide itself how to do -- and while there seem to exist keywords and special syntax to control this behaviour (namely references, pointers, as well as the <code>auto</code> and <code>register</code> keywords), they <em>generally</em> won't give you any guarantee that a parameter will be passed in a specific way. If you know one <em>particular</em> compiler well, then you might be able to deduce how parameter passing works with this compiler... but <em>generally</em>, you cannot &mdash; and should not have to &mdash; know about this mechanism.</p> <p><strong>P.S.:</strong> I forgot to mention that the term "stack object" is not just meaningless in terms of parameter passing. Even more generally, there simply isn't any way to tell the compiler to allocate an object on the stack. While local variables usually <em>will</em> be allocated on the stack, there is no guarantee whatsoever for this. I suppose that is why you chose to escape into assembly language. You could then explicitly <code>push</code> and <code>pop</code> values to/from the stack (as managed by the CPU).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload