Do Function Parameters and Arguments in C Use the Same Memory Unit? Verification Method Included

Do Function Parameters and Arguments in C Use the Same Memory Unit? Verification Method IncludedRegarding the function parameters and arguments in C language, there has been a detailed introduction in previous articles (C language function parameters and arguments), so I will not elaborate on that here. Instead, we will explore whether the function parameters and arguments use the same memory unit.What is a memory unit?This is merely a paraphrase of the information I have read (interpreted and organized by myself—there may be deviations): a memory unit is a collection of multiple (generally 8) storage elements in computer memory (storage), each memory unit has an address, and a storage element is a physical structure in a computer that can independently store 1 bit of binary information. For example, a variable int a = 1 may occupy 4 bytes (32 bits, which may vary across different platforms), occupying 4 memory units, each memory unit has a unique memory address, and they are usually contiguous.Next, let’s take a look at the function parameters and arguments.Are the memory units of value-passing parameters and arguments the same?First, let’s look at an example:

#include <stdio.h>// Define a value-passing functionvoid func(int a){ // Define parameter a    printf("Parameter a's memory address = %p\n", (void *)&a);}int main(){    int a = 1; // Define argument a    // Pass the value of argument a to parameter a    func(a);    printf("Argument a's memory address = %p\n", (void *)&a);    return 0;}

After compiling and running the code, the output is:

Parameter a's memory address = 000000000061FDF0Argument a's memory address = 000000000061FE1C

From the above example, we can see that in the value-passing method, the memory addresses of parameters and arguments a are different. According to the related materials I have read, each memory unit has an address, thus verifying that in a value-passing function, the parameter and argument do not use the same memory unit.Are the memory units of reference-passing parameters and arguments the same?First, we need to distinguish that in a reference-passing function, the memory addresses of the parameters and arguments themselves are different from the memory addresses they point to, otherwise, if we directly output the memory address pointed to by the parameter and the memory address of the argument, we would get the same value, as shown in the following code:

#include <stdio.h>// Define a reference-passing functionvoid func(int *b){ // Define parameter b    printf("Memory address pointed to by parameter b = %p\n", (void *)b);    printf("Memory address of parameter b itself = %p\n",(void *)&b);}int main(){    int b = 1;     func(&b);    printf("Argument b's memory address = %p\n", (void *)&b);    return 0;}

After compiling and running the code, the output is:

Memory address pointed to by parameter b = 000000000061FE1CMemory address of parameter b itself = 000000000061FDF0Argument b's memory address = 000000000061FE1C 

From the first and third lines of the output, we can see that if we directly output the memory address of the argument pointed to by the parameter, the outputs would be the same. However, if we output the memory address of the parameter itself, we would get different values, thus verifying that in a reference-passing function, the parameter and argument also use different memory units.Disclaimer: The content is for reference only and does not guarantee correctness!

Leave a Comment