|
|
|
|
|
by jonathrg
917 days ago
|
|
This is because of the calling convention / ABI If you write void free_with_extra_args(int *a, int *b) {
free(a);
}
then *a is in already in the correct slot (the RDI register) for the first argument when free_with_extra_args is being called. Whatever is put into *b is never touched. If you compile this with gcc -O2 you get free_with_extra_args:
jmp free
If you make the function call free(b) instead, you'll have to move b into the right place before calling free: free_with_extra_args:
mov rdi, rsi
jmp free
This is on x86-64 as summarized here https://en.wikipedia.org/wiki/X86_calling_conventions#System...Wikipedia also has a nice summary of calling conventions on other platforms like ARM. All modern calling conventions are similar: pass the first args in registers and then use the stack as needed https://en.wikipedia.org/wiki/Calling_convention |
|