I get a lot of shit for saying that Stack Overflow is the land of 12 year old boys, but it is the gospel truth. I don’t care how old they really are, they haven’t made it past the age of 12 mentally. Here is another shining example of why I make the statement. I stumbled across this while searching for all of the latest C++/C keywords so I could update a set of regular expressions in the Diamond text editor.
Like all meaningful questions on SO, this question was closed and will probably remain closed even after my edits. It should not shock you that the tiny handful of questions I’ve asked on SO have all been closed as well. If your question can’t be answered with a four second Google search, they will close it. After 3-4 meaningful questions you will be banned from asking anymore questions on SO.
Yeah, the children that run the place banned me from asking questions a while ago. One of the first times they shut me down was for this meaningful question. Want to know the hilarious part?
Yep, the question deemed unfit by the 12 year old boys and getting me shut down earned 1K views so far and is the result of some kind of badge. It still only got one up vote. Lots of people need to know how to do it. Most of them need to know many high minded obscure things so they never get to the 100 or whatever required for their votes to matter. Given the number of people writing code there is only a tiny tiny tiny fraction creating seed files for Ubuntu based distros.
So, since I’m sure this old meaningful question asked by someone else won’t be re-opened and my edit to the question won’t be allowed because it is over the heads of most SO regulars I will repost the information here.
This question should have never been closed. This is a standard interview question at legacy/heritage/very old shops. It isn’t Windows specific and it plagues the Unix/Linux world today. LLVM is in the process of adding support for the Pascal calling convention so it can make proper tails (among other system service) calls.
http://nondot.org/sabre/LLVMNotes/CustomCallingConventions.txt
No. The C calling convention isn’t the only calling convention in a Unix/Linux world. Lots of little systemy things got written in people’s favorite language and for many that wasn’t C.
C and Pascal evolved in their own little vacums early on. Dennis Richie was squirreled away at Bell Labs creating C and with it Unix on a DEC PDP computer. Niklaus Wirth was off in the realm of academia creating a language to teach kids about data structures.
Each language, for their own reasons, created the calling conventions they use. FORTRAN also has its own calling convention and memory organization for arrays.
Read this short article slow and several times:
This simple program is something you will see everywhere in shops with very old code bases. This is K&R era C code. Actually a touch later because with true K&R would look something like this:
int foo(s, f, b)
char* s;
float f;
struct Baz * b;
{
return 5;
}
This question is a standard because it is designed to identify a veteran developer and separate them from someone who only knows the latest standard.
C is a stack based language that doesn’t allow a developer to manipulate the program stack. As such you need to know that it relies on PostFix (also called Reverse Polish) notation. The classic example is the parsing of
3 + 4
being pushed onto the stack as
3
4
+
(using a stack that grows down visually. Yes, I know most grow up.)
You do this so you can pop them off in the order needed. First param, second param, operation.
It has been roughly 30 years since I dabbled in Pascal. Really haven’t touched it since the days of DOS and PCs that only had 2 floppy drives.
Pascal has a quasi-weird base. Many implementations are for a virtual stack or operate in pure Heap base/mode.
Heap based languages tend to use PreFix or Polish notation. (Two names, same thing) The classic examples of this use math expressions and seriously confuse the reality.
In terms of heap based languages or those following this convention parameters are populated back to front.
There are many complaints in the comments about “undefined behavior.” For someone who knows only the latest most modern standard, this would be a true statement. Someone who worked with the early compilers and took assembly programming would know that many of the better compilers of the day always passed at least one parameter to every function call. No matter what calling convention was being used, this was always the first value popped off the (virtual or physical) stack. Parameter count.
You never got “undefined behavior” with miss-matched parameter counts, you just got uninitialized values.
Why do you see zeros in the output? This was compiled in DEBUG mode.
When you compiled with DEBUG the vast majority of compilers on the majority of platforms, (MVS, VMS, Wang, DOS, Tandem, OS/2, etc.) would initialize all variables. Many would add boundary checking code for arrays and dynamically allocated chunks of memory. This lead to the lovely situation of programs working perfectly when compiled with DEBUG and failing spectacularly when compiled without DEBUG.
The intent of the question is to identify an extremely seasoned developer. Someone familiar with late 1980s to mid 1990s C and the compilers that went with it. They have an extremely large code base written during this time frame. Moving to a new standard would definitely be prohibitively expensive, if it was even possible. Some things got dropped along the way. Some “tricks” like swapping two variables without using a temporary may have seemed cute at the time, but won’t port to newer optimizing compilers because they were never a good idea. Anyone who knows the differences in said calling conventions and why they exist will be familiar with all of the problems and shortcuts that were taken during that era.
If you want a shining example of something that mostly got dropped along the way you need only look at how a single value was passed to a function requiring two and things all worked out.
The Pascal tag should also not have been removed from this. The question is a very good example of calling a Pascal library function from C on any platform and the issues that arise from it.