POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days : Re: Days 5- Server Time
29 Jul 2024 14:14:48 EDT (-0400)
  Re: Days 5-  
From: Darren New
Date: 25 Apr 2012 01:22:54
Message: <4f978a2e@news.povray.org>
On 4/23/2012 1:31, Invisible wrote:
> Damn... I'm trying to think of something you can code without actually using
> recursion. Hmm. Well, I guess there must be /something/...

Almost all business logic. I don't remember seeing any recursion in any 
operating system kernels I've read. I can't think of any recursive SQL code 
I've written at all.

> So if you want to count how many nodes there are in a binary tree, how do
> you do that non-recursively?

You use a queue. Each node you visit, you tack on to the end of the queue to 
be counted, and you go until the queue is empty.

Alternately, manually maintain the stack. Which is not "recursion" in the C 
sense, because you can actually tell without just blasting memory in unknown 
ways when you run out of room for the stack.

> If a programming language supports calling /any/ foreign language, that
> language will always be C. It will know about standard C types, it will
> usually know about C structs, and it will let you call arbitrary C functions.

OK. Yes, as long as you link to the proper C calling convention.

> And yet, the way that you call a C function is, apparently, undefined.

It's implementation-defined. You have to read the compiler manual (or source 
code) to know what the sequence is. And different functions in the same code 
can have different calling conventions.

Unless you have a CPU that actually enforces calling conventions.

> It also makes me wonder how the heck it's possible to take object code from
> multiple C compilers and link it all together, if each of them is
> potentially using a completely different calling convention. It sounds like
> it shouldn't work.

And indeed it doesn't. Try compiling one library with gcc and another with 
visual C and see if they link cleanly. Take a library off the mac and link 
it to a Windows-compiled main().

For that matter, try compiling one program for x86 and the other for x64.

-- 
Darren New, San Diego CA, USA (PST)
   "Oh no! We're out of code juice!"
   "Don't panic. There's beans and filters
    in the cabinet."


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.