calling-convention

how to specify vc11 lambda calling convention

陌路散爱 提交于 2019-11-26 16:34:26
问题 I want to pass a lambda function pointer, which nested in a class, to the Windows API callback function. I found there is no place for me to specify the __stdcall keyword. Some people told me the compile only support __cdecl , but after I used nm command to dump the obj file, I found the compile will generate three helper function ( __stdcall , __cdecl , __fastcall ) concurrently. So my problem is, how can I specify the calling convention? Those following code are my test code. #include

How do C compilers implement functions that return large structures?

江枫思渺然 提交于 2019-11-26 16:06:59
问题 The return value of a function is usually stored on the stack or in a register. But for a large structure, it has to be on the stack. How much copying has to happen in a real compiler for this code? Or is it optimized away? For example: struct Data { unsigned values[256]; }; Data createData() { Data data; // initialize data values... return data; } (Assuming the function cannot be inlined..) 回答1: None; no copies are done. The address of the caller's Data return value is actually passed as a

Retrieving the calling method name from within a method [duplicate]

久未见 提交于 2019-11-26 15:21:30
Possible Duplicate: How can I find the method that called the current method? I have a method in an object that is called from a number of places within the object. Is there a quick and easy way to get the name of the method that called this popular method. Pseudo Code EXAMPLE: public Main() { PopularMethod(); } public ButtonClick(object sender, EventArgs e) { PopularMethod(); } public Button2Click(object sender, EventArgs e) { PopularMethod(); } public void PopularMethod() { //Get calling method name } Within PopularMethod() I would like to see the value of Main if it was called from Main ...

Why should default parameters be added last in C++ functions?

守給你的承諾、 提交于 2019-11-26 12:19:29
问题 Why should default parameters be added last in C++ functions? 回答1: To simplify the language definition and keep code readable. void foo(int x = 2, int y); To call that and take advantage of the default value, you'd need syntax like this: foo(, 3); Which was probably felt to be too weird. Another alternative is specifying names in the argument list: foo(y : 3); A new symbol would have to be used because this already means something: foo(y = 3); // assign 3 to y and then pass y to foo. The

What is __stdcall?

蹲街弑〆低调 提交于 2019-11-26 12:15:42
问题 I\'m learning about Win32 programming, and the WinMain prototype looks like: int WINAPI WinMain ( HINSTANCE instance, HINSTANCE prev_instance, PSTR cmd_line, int cmd_show ) I was confused as to what this WINAPI identifier was for and found: #define WINAPI __stdcall What does this do? I\'m confused by this having something at all after a return type. What is __stdcall for? What does it mean when there is something between the return type and function name? 回答1: __stdcall is the calling

Why does gcc use movl instead of push to pass function args?

若如初见. 提交于 2019-11-26 11:26:31
问题 pay attention to this code : #include <stdio.h> void a(int a, int b, int c) { char buffer1[5]; char buffer2[10]; } int main() { a(1,2,3); } after that : gcc -S a.c that command shows our source code in assembly. now we can see in the main function, we never use \"push\" command to push the arguments of the a function into the stack. and it used \"movel\" instead of that main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp movl $3, 8(%esp) movl $2, 4(%esp) movl $1, (%esp) call a

Pass function as a parameter

情到浓时终转凉″ 提交于 2019-11-26 09:49:56
问题 I\'ve written function \'A\' that will call one of a number of other functions. To save re-writing function \'A\', I\'d like to pass the function to be called as a parameter of function \'A\'. For example: function A{ Param($functionToCall) Write-Host \"I\'m calling : $functionToCall\" } function B{ Write-Host \"Function B\" } Function C{ write-host \"Function C\" } A -functionToCall C Returns: I\'m calling: C I am expecting it to return: I\'m calling: Function C. I\'ve tried various things

How are variable arguments implemented in gcc?

橙三吉。 提交于 2019-11-26 08:16:20
问题 int max(int n, ...) I am using cdecl calling convention where the caller cleans up the variable after the callee returns. I am interested in knowing how do the macros va_end , va_start and va_arg work? Does the caller pass in the address of the array of arguments as the second argument to max? 回答1: If you look at the way the C language stores the parameters on the stack, the way the macros work should become clear:- Higher memory address Last parameter Penultimate parameter .... Second

Calling printf in extended inline ASM

六月ゝ 毕业季﹏ 提交于 2019-11-26 07:47:32
问题 I\'m trying to output the same string twice in extended inline ASM in GCC , on 64-bit Linux. int main() { const char* test = \"test\\n\"; asm( \"movq %[test], %%rdi\\n\" // Debugger shows rdi = *address of string* \"movq $0, %%rax\\n\" \"push %%rbp\\n\" \"push %%rbx\\n\" \"call printf\\n\" \"pop %%rbx\\n\" \"pop %%rbp\\n\" \"movq %[test], %%rdi\\n\" // Debugger shows rdi = 0 \"movq $0, %%rax\\n\" \"push %%rbp\\n\" \"push %%rbx\\n\" \"call printf\\n\" \"pop %%rbx\\n\" \"pop %%rbp\\n\" : :

Why does the Mac ABI require 16-byte stack alignment for x86-32?

点点圈 提交于 2019-11-26 07:32:08
问题 I can understand this requirement for the old PPC RISC systems and even for x86-64, but for the old tried-and-true x86? In this case, the stack needs to be aligned on 4 byte boundaries only. Yes, some of the MMX/SSE instructions require 16byte alignments, but if that is a requirement of the callee, then it should ensure the alignments are correct. Why burden every caller with this extra requirement? This can actually cause some drops in performance because every call-site must manage this