• C

Parameter Passing Order

Can anybody explain why pascal order of pushing parameters is not suitable for variable
number of arguments.Why can't compiler generate more information for such cases.Please
explain the root problem in this convention.

Regards,
Chai-yo
chai_yo123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_lychee_Commented:
in pascal arguments pushed from left to right, so the rightmost argument is the shallowest element on the stack...

y this is bad for variable arguments: the info on no. of args is normally the 1st argument (y not the last? i guess that makes type checking much more difficult?)... so when the routine is entered, the code can't determine how long the variable list is....

i guess it's possible for the compiler to push an extra word of information on how many params were passed, but y use that when u can just use the C convention?
0
LucHoltkampCommented:
Another problem with the PASCAL calling convention is that the called function is responsible for cleaning up the stack. Whereas in the C calling convention the caller cleans up the stack after the call. The problem is that the called function does not know how many parameters are on the stack (it must be known during compile-time, but it can vary...), so it cannot clean up the stack.

>> I guess it's possible for the compiler to push an extra word
>> of information on how many params were passed
I don't thinks so... and not only the number is neccesairy but also the size, otherwise the called function still can't clean up the stack. This would also mean a special calling convention for var-args...

>> the info on no. of args is normally the 1st argument (y not
>> the last? i guess that makes type checking much more
>> difficult?)...
Even impossible, for the adress of that 1st argument can not be retrieved without the information in that same first argument...

Luc
0
_lychee_Commented:
actually the called function can (with quite a lot of hassle) clean up the stack....

it pops the cs/ip, pops everything else, pushes back the cs/ip and then returns... that's on the intel archi at least...

but again, y go thru the hassle when u have the (wonderful?) c calling convention?
0
KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

wyllikerCommented:
LucHoltkamp is correct with the reason why PASCAL calling conventions cannot take variable parameters - the number of arguments to remove from the stack would be unknown in that you would only know the minimum number of arguments and that the number of arguments could be different on a per caller basis.

The language Pascal does runtime parameter checking so it requires that all of the parameters' types are known when it generates code.  With variable argument C functions - it is not possible for the compiler to know the true type of any one variable argument. It would be up to the called function to have a way of discerning the type of argument - printf uses the format masks to coerce values to match and assumes the type of the corresponding argument but you could just as easily pass garbage to printf and it wouldn't know until it blew up.


0
isasoriCommented:
Fixed amount of arguments - concept of language Pascal as strict programming language of a high level.
By the way, it is one from the reasons, why the Pascal compiler works much faster C compiler.
If to add in Ïàñêàëü a variable amount of arguments, arithmetic of the pointers and still some things, we shall receive C with the changed syntax.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.