3 ms·
> Another sticking point in this interpretation is Traister’s incomprehensible approach to writing functions that take a variable number of arguments. He does t
by shakna 6y ago
> Another sticking point in this interpretation is Traister’s incomprehensible approach to writing functions that take a variable number of arguments. He does this by passing an arbitrary number of arguments to a function (the first being the number of arguments) and accessing them using offsets from the address of the first argument.
This is one of those things that works by accident for some compilers for some platforms, but because it works for the developer, they think it's a brilliant idea.
From what I remember, and I'm taxing my memory here, some of the early Turbo compilers did lay out arguments in this fashion, for DOS, when all optimisations were disabled. Most of the time.
I do recall seeing similar layouts in some of the programs I played around with at the time, and thinking it was a genius idea. For context, I was about seven or eight years old.
- userbinator 6y agoBy "some" you mean "every stack-based right-to-left calling convention", which happens to be the norm for 16 and 32-bit x86. In contrast, varargs on 64-bit x86 is a mess: https://blog.nelhage.com/2010/10/amd64-and-va_arg/ https://blog.nelhage.com/2010/10/amd64-and-va_arg/
- saagarjha 6y agoA mess, but also fast.
- notacoward 6y agoThat convention is nowhere near as common as you present it to be. I was working on systems 30 years ago where the first few arguments were passed in registers. Many such systems. "Everything on the stack" was the rule for register-poor x86, which is why people weaned on DOS/Windows remain attached to it, but even back then it was far from a universal standard.
- dn3500 6y agoVarargs was not part of the language originally. If you look at the source code for printf() from Unix v7, I think you'll find it accesses its arguments by indexing off the address of the first one. I don't remember when varargs came along, but it was some time in the 1980s, and this book was written in 1990. Which is not to excuse the author. His description of how this works and why you would want to do it is just wrong.
- _kst_ 6y agoA particular implementation of printf used that technique. There probably weren't a whole lot of different implementations at the time, but it's not part of the C specification. <varargs.h> was a pre-Standard header that provided a consistent interface for accessing variable arguments. It was superseded by <stdarg.h> in the 1989 ANSI C standard. gcc in particular dropped support for <varargs.h> a number of years ago.