Safer casts

Dee Girl deegirl at noreply.com
Sun May 11 15:59:39 PDT 2008


Yigal Chripun Wrote:

> your reply is correct but it is misleading.
> a different example:
> void Fn() {
>   int i;
> }
> 
> every time the function is called a new int i is generated on the stack
> and destroyed when the function is exited. so for a specific call to Fn
> there can be only one variable i, in the course of the program that same
> variable is created and destroyed many times ( for each call to the
> function).
> you are correct in saying that there is only one mysort function but
> every time you call it with a different comp delegate it needs to have a
> different sort instance. the difference is that the int i is generated
> on the stack but all those different sort instances are not. those are
> code which goes into the executable.
> so while in any specific time mysort uses only one sort instance, over
> the course of the entire program the sort template can get instantiated
> more than once.
> does it make more sense to you, now?

I am sorry Yigal. What you say makes no sense. Your previous reply is wrong and this reply is even more wrong.

It is simple logic. mysort has only one body. Inside that body is has a regular call to a function that implements the sorting. That call can only go to one function! It is very clear. D does not generate code at runtime.

It is a silly exercise but we can do this. Please compile this program.

=== test.d
#!/home/yasuko/bin/compile-launch -w
import std.algorithm;
import std.stdio;

void mysort(int[] array, bool delegate(int, int) comp)
{
    sort!(comp)(array);
}

void main()
{
    int[] array = [ 1, 2, 3, 4 ];
    bool comp1(int a, int b) { return a > b; }
    mysort(array, &comp1);
    //writeln(array);
    bool comp2(int a, int b) { return a < b; }
    mysort(array, &comp2);
    //sort!(comp2)(array);
    //writeln(array);
}
=== 

writeln is taken out so it does not get confusing. Please run

dmd -g test.d
objdump -d test.o|less

Search for word "section" in the disassembled file. There are 19. It seems each function has its section.

1. section .text is the all file

2. section .text._D4test6mysortFAiDFiiZbZv is the mysort function. I do not know the naming scheme of D but it is easy to guess which function is.

3. section .text._Dmain is the main funciton

4. section .text._D4test4mainFZv8compare1MFiiZb is compare1

5. section .text._D4test4mainFZv8compare2MFiiZb is compare2

6. section .text._D4test6mysortFAiDFiiZbZv108__T4sortS36_D4test6mysortFAiDFiiZbZv4compDFiiZbVE3std9algorithm12SwapStrategy0S233std9algorithm8iterSwapTAiZ4sortMFAiZv is the std.sort function

7. section .text._D3std8iterator12__T5beginTiZ5beginFAiZPi is begin funciton

8. section .text._D3std8iterator10__T3endTiZ3endFAiZPi is end function

9. section .text._D4test6mysortFAiDFiiZbZv112__T8sortImplS36_D4test6mysortFAiDFiiZbZv4compDFiiZbVE3std9algorithm12SwapStrategy0S233std9algorithm8iterSwapTAiZ8sortImplMFAiZv is the sortimpl function

10. section .text._D4test6mysortFAiDFiiZbZv112__T8sortImplS36_D4test6mysortFAiDFiiZbZv4compDFiiZbVE3std9algorithm12SwapStrategy0S233std9algorithm8iterSwapTAiZ8sortImplMFAiZv4predMFiZb is the pred function

11. section .text._D4test6mysortFAiDFiiZbZv55__T8getPivotS36_D4test6mysortFAiDFiiZbZv4compDFiiZbTPiZ8getPivotMFPiPiZPi is the getPivot function

12. section .text._D3std9algorithm16__T8iterSwapTPiZ8iterSwapFPiPiZv is the iterSwap function

13. section .text._D3std9contracts17__T8pointsToTiTiZ8pointsToFKiKiZb is the pointsTo function

14. section .text._D3std9algorithm11__T4swapTiZ4swapFKiKiZv is the swap function

15. section .text._D3std8iterator12__T5rangeTiZ5rangeFPiPiZAi is the range function

16. section .text._D4test6mysortFAiDFiiZbZv112__T8sortImplS36_D4test6mysortFAiDFiiZbZv4compDFiiZbVE3std9algorithm12SwapStrategy0S233std9algorithm8iterSwapTAiZ8sortImplMFAiZv243__T9partitionS165_D4test6mysortFAiDFiiZbZv112__T8sortImplS36_D4test6mysortFAiDFiiZbZv4compDFiiZbVE3std9algorithm12SwapStrategy0S233std9algorithm8iterSwapTAiZ8sortImplMFAiZv4predMFiZbVE3std9algorithm12SwapStrategy0S233std9algorithm8iterSwapTAiZ9partitionMFAiZPi is the partition function

17. section .text._D4test6mysortFAiDFiiZbZv55__T8isSortedS36_D4test6mysortFAiDFiiZbZv4compDFiiZbTAiZ8isSortedMFAiZb is the isSorted function

18. section .text._D4test6mysortFAiDFiiZbZv55__T8isSortedS36_D4test6mysortFAiDFiiZbZv4compDFiiZbTAiZ8isSortedMFAiZb4predMFiiZb is another isSorted function (I do not now why there are two)

19. section .text._D4test6mysortFAiDFiiZbZv55__T8isSortedS36_D4test6mysortFAiDFiiZbZv4compDFiiZbTAiZ8isSortedMFAiZb133__T12findAdjacentS108_D4test6mysortFAiDFiiZbZv55__T8isSortedS36_D4test6mysortFAiDFiiZbZv4compDFiiZbTAiZ8isSortedMFAiZb4predMFiiZbTAiZ12findAdjacentMFAiZPi is the isAdjacent function.

It is clear now that there is only one instantiation of std.sort. I hope now it is all clear. Thank you, Dee Girl



More information about the Digitalmars-d mailing list