POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days Server Time
29 Jul 2024 10:31:09 EDT (-0400)
  Teach yourself C++ in 21 days (Message 149 to 158 of 168)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Francois Labreque
Subject: Re: Days 5-
Date: 25 Apr 2012 08:25:24
Message: <4f97ed34$1@news.povray.org>
Le 2012-04-25 04:22, Invisible a écrit :
> On 25/04/2012 06:22 AM, Darren New wrote:
>> 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 suppose I should consider myself lucky that I'm never going to be
> writing anything like that.

Why not?  Didn't you rewrite the code to compile your danse tournament 
rankings?  I know you only did it for yourself, but that was business 
logic, even if you weren't using WebSphere, J2EE, Weblogic or some big 
platform like that.

>
>>> 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.
>
> So... how is it ever possible to statically link software?
>

You have to take extra steps to make sure it works.  For example, back 
in the days of Windows 3.1, you had to use the Pascal calling convention 
for your functions in order to make sure that they could interact with 
other modules.  (Maybe it's still that way, I haven't touched a C 
program in 15 years.)

-- 
/*Francois Labreque*/#local a=x+y;#local b=x+a;#local c=a+b;#macro P(F//
/*    flabreque    */L)polygon{5,F,F+z,L+z,L,F pigment{rgb 9}}#end union
/*        @        */{P(0,a)P(a,b)P(b,c)P(2*a,2*b)P(2*b,b+c)P(b+c,<2,3>)
/*   gmail.com     */}camera{orthographic location<6,1.25,-6>look_at a }


Post a reply to this message

From: Invisible
Subject: Re: Day 6
Date: 25 Apr 2012 09:08:24
Message: <4f97f748$1@news.povray.org>
On 25/04/2012 12:59 PM, Warp wrote:
> Invisible<voi### [at] devnull>  wrote:
>> You know what? I'm just going to /buy/ the book.
>
>    I'd recommend getting both of them. (Of course you could buy the
> "Effective C++" first, and if you like it, buy "More Effective C++"
> later.)

I think I can actually buy the pair for a lower price... (Amazon likes 
to do that.)

Gotta love how Amazon screwed me over, and I was all pissed about it, 
and now here I am giving them my money again.


Post a reply to this message

From: Invisible
Subject: Re: Days 5-
Date: 25 Apr 2012 09:12:30
Message: <4f97f83e$1@news.povray.org>
>>> Almost all business logic.
>>
>> I suppose I should consider myself lucky that I'm never going to be
>> writing anything like that.
>
> Why not? Didn't you rewrite the code to compile your danse tournament
> rankings?

I did not "rewrite" anything. I merely wrote a program that downloads 
some web pages and attempts to extract the raw data from them. And that 
is all.

(The organiser's website is obnoxiously difficult to navigate. 
Fortunately I was able to reverse-engineer how the URLs are generated...)

> I know you only did it for yourself, but that was business
> logic, even if you weren't using WebSphere, J2EE, Weblogic or some big
> platform like that.

OK.

>> So... how is it ever possible to statically link software?
>
> You have to take extra steps to make sure it works.

Really?

> For example, back in
> the days of Windows 3.1, you had to use the Pascal calling convention
> for your functions in order to make sure that they could interact with
> other modules. (Maybe it's still that way, I haven't touched a C program
> in 15 years.)

So what you're saying is "the C calling convention is undefined, but the 
OS calling convention is not"?


Post a reply to this message

From: Francois Labreque
Subject: Re: Days 5-
Date: 25 Apr 2012 10:25:33
Message: <4f98095d$1@news.povray.org>
Le 2012-04-25 09:12, Invisible a écrit :
>>> So... how is it ever possible to statically link software?
>>
>> You have to take extra steps to make sure it works.
>
> Really?
>

Yes.  For example: Miocrsoft dictating that all Windows applications and 
DLLs must use the Pascal calling convention.

(I know DLLs are not statically linked... but you get my drift.  If you 
want to use library XYZ, you need to use the same calling convention as 
library X uses.

>> For example, back in
>> the days of Windows 3.1, you had to use the Pascal calling convention
>> for your functions in order to make sure that they could interact with
>> other modules. (Maybe it's still that way, I haven't touched a C program
>> in 15 years.)
>
> So what you're saying is "the C calling convention is undefined, but the
> OS calling convention is not"?

No.  I'm saying - as Darren pointed out - that the C calling convention 
is not undefined, but implementation-defined.  There's a difference.

Division by zero = undefined.
Number of bits in an integer = implementation-defined.

-- 
/*Francois Labreque*/#local a=x+y;#local b=x+a;#local c=a+b;#macro P(F//
/*    flabreque    */L)polygon{5,F,F+z,L+z,L,F pigment{rgb 9}}#end union
/*        @        */{P(0,a)P(a,b)P(b,c)P(2*a,2*b)P(2*b,b+c)P(b+c,<2,3>)
/*   gmail.com     */}camera{orthographic location<6,1.25,-6>look_at a }


Post a reply to this message

From: Darren New
Subject: Re: Days 5-
Date: 25 Apr 2012 23:33:45
Message: <4f98c219$1@news.povray.org>
On 4/25/2012 1:22, Invisible wrote:
> Really? That's surprising. No recursive sorting algorithms for balancing the
> B*-trees that the directory nodes use, or for sorting the ready thread list
> by priority?

If you can limit the recursion a priori, then sure. If you know you'll only 
have at most 4 levels of tree, or only 128 entries in the list to sort, or 
something like that, recursion is fine. I wouldn't write production code to 
manipulate an arbitrary data structure with actual machine-stack-based 
recursion in C.

> SQL is a rather special-purpose language. I'm not sure it even /supports/
> recursion...

That's kind of my point.

> In other words, you manually implement the stack.

Yes.

> Tell me, in what way does this prevent you running out of stack?

It doesn't. It just keeps you from running out of stack without knowing it.

If your language runtime already supplies that service (e.g., C# throwing an 
exception when it runs out of stack or something) then recursive algorithms 
are fine.

>> Unless you have a CPU that actually enforces calling conventions.
> So... how is it ever possible to statically link software?

You appear to be having a hard time distinguishing "it's 
implemention-defined and not specified in the standard' from "we don't know 
what it is, ever."

> I mean, if every compiler uses its own random calling convention,

It doesn't.

> then how
> /the hell/ is it possible to write some C, link it against GMP, wxWidgets
> and the OS header files, and get a usable binary? You know, given that you
> have no idea which compiler these libraries are compiled with...

Because sometimes the OS defines the calling convention to be used.

> I would, however, expect that on any given architecture, the C calling
> convention is always the same.

In spite of the page I pointed you to showing all the different calling 
conventions used by the x86 line, right?

-- 
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

From: Darren New
Subject: Re: Teach yourself C++ in 21 strange malfunctions
Date: 25 Apr 2012 23:36:36
Message: <4f98c2c4$1@news.povray.org>
On 4/25/2012 1:12, Invisible wrote:
> You do all of that stuff from /outside/ your transactions, that's all.

Except that .NET is about components, so that doesn't work out too well.

And if you're talking about putting Haskell on .NET, then your entire 
program becomes one "transaction". (Note we've moved to talking about STM 
from talking about Haskell, which was not my intention. It was an analogy.)

> Remember, transactions are only for thread communication. You start a
> transaction, fetch the next work item from the shared work queue, and the
> transaction. Then you process the work item, calling whatever libraries you
> need. Simples.

And if the work item is in a class written that does I/O to obtain it, or to 
log a message, or etc etc etc?

-- 
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

From: Invisible
Subject: Re: Teach yourself C++ in 21 strange malfunctions
Date: 26 Apr 2012 04:13:42
Message: <4f9903b6$1@news.povray.org>
On 26/04/2012 04:36 AM, Darren New wrote:
> On 4/25/2012 1:12, Invisible wrote:
>> You do all of that stuff from /outside/ your transactions, that's all.
>
> Except that .NET is about components, so that doesn't work out too well.
>
> And if you're talking about putting Haskell on .NET, then your entire
> program becomes one "transaction".

How so?

> (Note we've moved to talking about
> STM from talking about Haskell, which was not my intention. It was an
> analogy.)

OK. And I was just saying that Haskell /already/ lets you use libraries 
written in C, which aren't functional at all. I don't see why using the 
.NET libraries would be any harder. [Assuming you solve the problem of 
how to make your program run on the CLR in the first place, obviously.]

>> Remember, transactions are only for thread communication. You start a
>> transaction, fetch the next work item from the shared work queue, and the
>> transaction. Then you process the work item, calling whatever
>> libraries you need.
>
> And if the work item is in a class written that does I/O to obtain it,
> or to log a message, or etc etc etc?

I am really bad at typing, aren't I? :-S

What I /meant/ to write was that you start a transaction, fetch the next 
work item, and then end the transaction. Once the transaction is over, 
you can do as much I/O as you like.

The problem (I assume) that you're talking about is that all the 
existing .NET stuff is structured under the assumption that you can do 
I/O whenever you feel like it. I can see a few ways to approach that. 
(Perhaps the simplest possibility being "only Haskell is allows to use 
STM". :-P )


Post a reply to this message

From: Warp
Subject: Re: Days 5-
Date: 26 Apr 2012 08:09:56
Message: <4f993b14@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> If you can limit the recursion a priori, then sure. If you know you'll only 
> have at most 4 levels of tree, or only 128 entries in the list to sort, or 
> something like that, recursion is fine. I wouldn't write production code to 
> manipulate an arbitrary data structure with actual machine-stack-based 
> recursion in C.

  Very usually the recursion depth of a typical recursive algorithm is
O(log n). This means that the computer wouldn't be able to hold as much
data as to make you run out of stack space when running the recursive
algorithm on it. (For example, if the recursion depth is 50, that means
that there's about 10^15 elements of data. Even if each element took just
one single byte, that would be like 1024 terabytes of RAM.)

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Days 5-
Date: 27 Apr 2012 20:38:17
Message: <4f9b3bf9$1@news.povray.org>
On 4/26/2012 5:09, Warp wrote:
>    Very usually the recursion depth of a typical recursive algorithm is
> O(log n). This means that the computer wouldn't be able to hold as much
> data as to make you run out of stack space when running the recursive
> algorithm on it.

Assuming that you're not writing a library that might invoke you 40 levels down.

That said, I've found I rarely need it, and that the undefined behavior 
isn't worth the risks. YMMV. If you can handle the program crashing when you 
give it too much data in a way that generates bogus results, then sure, feel 
free. :-)  (I'm not being sarcastic - there are lots of programs like that.)

-- 
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

From: Darren New
Subject: Re: Teach yourself C++ in 21 strange malfunctions
Date: 27 Apr 2012 20:40:58
Message: <4f9b3c9a@news.povray.org>
On 4/26/2012 1:13, Invisible wrote:
> OK. And I was just saying that Haskell /already/ lets you use libraries
> written in C, which aren't functional at all.

And can the C call back into the Haskell? And if so, are all the Haskell 
invariants and such maintained? And can you do that without telling the 
caller whether they're calling C or compiled Haskell?

> What I /meant/ to write was that you start a transaction, fetch the next
> work item, and then end the transaction. Once the transaction is over, you
> can do as much I/O as you like.

Right. But if "fetch the transaction" means "obtain it from the server over 
there using TCP/IP", then you're kind of screwed, aren't you?

Heck, even a regular expression match isn't functional in .NET.

> The problem (I assume) that you're talking about is that all the existing
> .NET stuff is structured under the assumption that you can do I/O whenever
> you feel like it. I can see a few ways to approach that. (Perhaps the
> simplest possibility being "only Haskell is allows to use STM". :-P )

And that's the kind of incompatibility I'm saying makes .NET much less useful.

-- 
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

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

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