C language with array and struct body to __c language

Source: Internet
Author: User
Tags array example

C-language function calls when the reference operation on the 32-bit x86 machine relies on the stack. But in the x86_64 machine uses some registers as auxiliary, but if the parameter is too many, the register is not enough to use, at this time also must use the stack operation to implement the parameter. Although the C language does not explicitly limit the number of function pass arguments ( Depending on the compiler implementation: Http://stackoverflow.com/questions/9034787/function-parameters-max-number), but too much parameter passing is bound to affect code execution efficiency.

Usually the C language function is very explicit, as in the following function:

int test (int a,float b,int *pointer);
Note: The following examples are described using the 32-bit X86,GCC compiler.

But what if the array or struct body is passed as a parameter? How many parameters are passed, how much stack space is occupied?

typedef struct list{
int A;
int b;
int C;
int D;
int e;
} list;
int test1 (int array[5]);
int test2 (list listtest);

First look at the array example:
/*passing arguments test:array*/
#include <stdio.h>
void Array (int tmp[5]) {
printf ("%d\n", tmp[2]) ;
}
int main (void) {
int test[]={1,2,3,4,5};
Array (test);
To compile the assembly code, first intercept the main function pass-parameter section:
        MOVL    $ -32 (%EBP)
        movl    $ -28 (%EBP)
        movl    $ -24 (%EBP)
        movl    $ -20 (%EBP)
        movl    $ -16 (%EBP)
        Leal    -32 (%EBP),%eax    pushl%eax Call Array
You can see that in the main function the array element is first written to the array space, then the array address (that is, the address of the element A[0] is saved in the eax, then the EAX is pressed and the array function is finally called. And look at the array function:

Array:
. LFB0:
        pushl   %ebp
        movl    %esp,%EBP
        subl    $,%esp
        movl    8 (%EBP),%eax
        addl    $ 8,%eax
        movl    (%eax),%eax
        subl    $,%esp pushl%eax   $. LC0
        call    printf
        addl    $16,%esp
        nop
        leave
        ret

The first is the old-fashioned operation: Save Ebp, Sync ebp, and esp,esp Move down to create a new function stack environment. Then take 8 (%EBP). What is it? In fact, the function execution went through 3 times the press stack: PUSHL%eax,call% Ebp. And each time the stack is 32 bits, that is, 4 bytes. So 0 (%EBP) is the value of PUSHL%EBP, 4 (%EBP) is the function return address, and 8 (%EBP) is the PUSHL%eax value, that is, the array address. Of course Addl $8,%eax is tmp[2. Address, no longer repeat. So much, in a word, array (test) just passed an array address, and did not pass the entire array element as a parameter to the child function.

Then look at the structure of the example:

#include <stdio.h>
typedef struct list{
        int A;
        int b;
        int C;
        int D;
} list;
void Test (list tmp) {
        printf ("%d\n", tmp.b);
}
int main (void) {
        list tmp={.a=10,.b=20,.c=30,.d=40};
        Test (TMP);
}

Also intercepts the main function parameter pass fragment:
        MOVL    $ -24 (%EBP)
        movl    $, -20 (%EBP) movl $,    -16 (%EBP)
        MOVL $    -12 (%EBP)
        PUSHL   -12 (%EBP)
        pushl   -16 (%EBP)
        pushl   -20 (%EBP)
        pushl   -24 (%EBP)
        Call    Test

As you can see, this is actually putting the members of the structure in full pressure stack. Therefore, in open source project code, you often don't see the structure as a parameter, but instead pass a pointer to the structure body. So no matter how many members of the structure, the pressure stack is only pressed into a value. As the following code:
 #include <stdio.h> typedef struct list{int A;
        int b;
        int C;
int D;
}list;
        void Test (list *tmp) {printf ("%d\n", tmp->b);} int main (void) {list tmp={.a=10,.b=20,.c=30,.d=40};
Test (&TMP); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.