1. Pointer work around

--=====================_915745647==_

Hello all,

I still kinda interested in porting Doom to Euphoria, and overwhelming
response was that pointers would pose a problem. Well I've enclosed a short
C++ program that uses pointers and a ported Euphoria version. I was
wondering if some of you who know C or C++ and Euphoria would mind taking a
look at it, and determine if this would work in a larger port like Doom.
The version of Doom I would like to port in a Linux version that seems to
be written almost entirely in C, so I don't think I have to worry about
pointers and dynamic memory.

Thanks
Paul Martin
--=====================_915745647==_
 x-mac-type="705A4950"; x-mac-creator="705A4950"

new topic     » topic index » view message » categorize

2. Re: Pointer work around

The version of Doom I would like to port in a Linux version that seems to
be written almost entirely in C, so I don't think I have to worry about
pointers and dynamic memory.

You still might, C has pointers not just C++.

-----Original Message-----
From: Euphoria Programming for MS-DOS
[mailto:EUPHORIA at LISTSERV.MUOHIO.EDU]On Behalf Of Paul Martin
Sent: Thursday, January 07, 1999 10:47 AM
To: EUPHORIA at LISTSERV.MUOHIO.EDU
Subject: Pointer work around


Hello all,

I still kinda interested in porting Doom to Euphoria, and overwhelming
response was that pointers would pose a problem. Well I've enclosed a short
C++ program that uses pointers and a ported Euphoria version. I was
wondering if some of you who know C or C++ and Euphoria would mind taking a
look at it, and determine if this would work in a larger port like Doom.
The version of Doom I would like to port in a Linux version that seems to
be written almost entirely in C, so I don't think I have to worry about
pointers and dynamic memory.

Thanks
Paul Martin

new topic     » goto parent     » topic index » view message » categorize

3. Re: Pointer work around

On Thu, 7 Jan 1999 09:47:27 -0600, Paul Martin <twilight at WCC.NET> wrote:

>Hello all,
>
>I still kinda interested in porting Doom to Euphoria, and overwhelming
>response was that pointers would pose a problem. Well I've enclosed a short
>C++ program that uses pointers and a ported Euphoria version.

Looks good to me. I would make a function to get the input values,
as below, to save space.

include get.e -- to handle value()

-- Code to setup structure for point
sequence point, pt1, pt2, median
constant point_x = 1, point_y = 2
point = {0,0}
pt1 = point
pt2 = point

function getMedian( sequence p1, sequence p2)
    -- set-up new structure for result
    sequence result
    result = point
    -- do the actual business
    result[point_x] = (p1[point_x]+p2[point_x])/2
    result[point_y] = (p1[point_y]+p2[point_y])/2
    return result
end function

function getXY (atom pt)
sequence xy
   xy = point
   printf(1,"Enter the X and Y coordinates for point # %d : ",pt)
   xy[point_x] = gets(0)
   xy[point_x] = value (xy[point_x])
   xy[point_x] = xy[point_x][2]
   printf(1,"%d\t",xy[point_x])

   xy[point_y] = gets(0)
   xy[point_y] = value (xy[point_y])
   xy[point_y] = xy[point_y][2]
   printf(1,"%d\n",xy[point_y])
return xy
end function

pt1 = getXY(1)
pt2 = getXY(2)
median = getMedian(pt1,pt2)
printf (1,"Mid point in (%g, %g)\n", {median[point_x],median[point_y]})

new topic     » goto parent     » topic index » view message » categorize

4. Re: Pointer work around

Your program in C or Eu doesn't even use pointers!

new topic     » goto parent     » topic index » view message » categorize

5. Re: Pointer work around

Hello,

I'm afraid that there is some sort of misunderstanding here. I copied this
program directly from a book, and the section I copied it from was entitled
"Passing Structures by Pointers" which incidentally is in the remarks in
the program. If this program is not using pointers; then what is it using?
If you have a short C program that uses pointers; I would like to see it.
I'm not saying you are wrong, because I still have a lot to learn about C
and C++. And yes, my Euphoria program does not contain pointers because,
Euphoria as far as I know does not support them, at least the way C or C++
does.

What I was trying to achieve was to rewrite the source code for the Linux
version of Doom into Euphoria. Since Euphoria does not handle pointers and
structures like C does I was trying to find a simple solution that can be
inserted, so that I would not have completely restructure the program and
its data.

Thanks

Paul Martin
At 03:58 AM 1/8/99 EST, you wrote:
>Your program in C or Eu doesn't even use pointers!
>
>

new topic     » goto parent     » topic index » view message » categorize

6. Re: Pointer work around

Paul Martin wrote:

> Since Euphoria does not handle pointers and structures like C
> does I was trying to find a simple solution that can be inserted,
> so that I would not have completely restructure the program and
> its data.

You probably want to map C structures into Euphoria sequences. Instead of
pointers, store the 'structures' in a global sequence, and pass the index
instead of a pointer. I typically name my global structure 'the'.

It help if you have some support functions to allocate/deallocate indexes in
the sequence. You can do this by setting up a parallel sequence which
contains a flag indicating if the sequence is free (untested code follows):

    sequence the, isFree
        the = {}
        isFree = {}

    function storeVal( object val )

        -- store a value into sequence 'the'
        -- return the index

        integer at

        -- look for a free slot
        at = find( -1, isFree )
        if at then
            -- store
            the[at]= val
            -- flag as taken
            isFree[at] = 0
            -- return index
            return at
        end if

        -- add an index
        the = append( the, value )
        -- add a flag
        isFree = append( isFree, 0 )
        -- return index
        return length( the )

    end function


    procedure freeVal( integer index )

        -- free value at index

        -- free the memory
        the[index] = 0
        -- flag the index as free
        isFree[index] = -1

    end procedure

The functions storeVal/freeVal roughly map into malloc/free.

The 'swap' function is typical example using pointers. In Euphoria (and C),
since parameters are treated as local, the following will *NOT* work:

    procedure swap( integer a, integer b )
        -- example of swap routine that *won't* work
        integer tmp
        tmp = a
        a = b
        b = tmp
    end procedure

Here is a version which uses indexes to the global sequence instead:

    procedure swap( integer first, integer second )
        -- swap two values in a global sequence
        integer tmp
        tmp = the[first]
        the[first] = the[second]
        the[second] = tmp
    end procedure


You could then do a swap like this:

    integer ptrA, ptrB

    -- allocate and store values in "addresses"
    ptrA = storeVal( 123 )
    ptrB = storeVal( 456 )

    -- swap values in "addresses"
    swap( ptrA, ptrB )

    -- display values in "addresses"
    ? the[ptrA]
    ? the[ptrB]

    -- free "addresses"
    freeVal( ptrA )
    freeVal( ptrB )


I find that 'the' as the name of the global sequence makes my code more
readable:

    the[creature][image] = bitmap12


Hope this helps.

-- David Cuny

new topic     » goto parent     » topic index » view message » categorize

7. Re: Pointer work around

Your program just uses 2 variables that have two other variables that are
"twins" of them.  I'll come up with a simple C pointer program later and send
it to you.

new topic     » goto parent     » topic index » view message » categorize

8. Re: Pointer work around

--part0_915900581_boundary
Content-ID: <0_915900581 at inet_out.mail.aol.com.1>
Content-type: text/plain; charset=US-ASCII

Here is a simple C++ prog that uses pointers, i put in comments but if there
are any questions feel free to ask.

--part0_915900581_boundary
Content-ID: <0_915900581 at inet_out.mail.aol.com.2>
Content-type: text/plain;
        name="POINTER.CPP"
Content-transfer-encoding: quoted-printable
Content-disposition: inline

// Pointer example.
#include <iostream.h>
#include <math.h>
#include <complex.h>

typedef struct
=09{
=09=09int X, Y;
=09} Point;

double Slope(Point First, Point Second);

void main()
{
=09Point P[2] //Declares an array of two sets of points.
=09Point *PP; //Declares a pointer to a point.

=09PP =3D new Point; //Initiallizes pointer.
=09PP =3D &P[0]; //Sets pointer to equal the first point in P.
=09cout << "X1: ";
=09cin >> PP->X; //Gets the value of X1 with the pointer.
=09cout << "Y1 :";
=09cin >> PP->Y; //Gets the value of Y1 with the pointer.
=09PP++;  //Sets the pointer to equal the second point in P.
=09cout << "X2 :";
=09cin >> PP->X; //Gets the value of X2 with the pointer.
=09cout << "Y2 :";
=09cin >> PP->Y; //Gets the value of Y2 with the pointer.
=09cout << "The slope of the line between (X1, Y1) and (X2, Y2) is "
=09cout << Slope(P[0], P[1]) << ".";  //Finds the slope with the actual a=
rray.

}

double Slope(Point One, Point Two)
{
=09return ((Two.Y - One.Y)/(Two.X - One.X));
}


--part0_915900581_boundary--

new topic     » goto parent     » topic index » view message » categorize

9. Re: Pointer work around

I've ported some polygon-drawing routines written i C to Euphoria, and I
had to solve the problem with pointers. This is how I did it:
I changed anything looking like this:

struct {
int X
int Y
}Array

char Array *pt1
int Crap

Crap=pt1->X


to this:

sequence Array
integer Crap
constant X=1
constant Y=2
Array=repeat(0,2)

Crap=Array[X]



...And anything looking like this:

int Array[200]

int Crap

Crap=*(Array+100)


was changed to this:

sequence Array
integer Crap
Array=repeat(0,200)

Crap=Array[100]


I hope this makes sense. It worked anyway...

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

10. Re: Pointer work around

it's still not using anything pointer oriented in Euphoria but its a step in
the right direction.

new topic     » goto parent     » topic index » view message » categorize

11. Re: Pointer work around

On Mon, 11 Jan 1999 03:14:18 EST, Adam Weeden <SkaMan325 at AOL.COM> wrote:

>it's still not using anything pointer oriented in Euphoria but its a step in
>the right direction.

It's a workaround for the lack of proper structures in Euphoria.
A pointer is a value corresponding to the address of an object
in memory. There is no need for a pointer in Euphoria, since
there is nothing you can do with the address of the variable
that you can't do by referencing the object directly by name,
or name[index].
Nothing, that is, except crash the program by overwriting something
you shouldn't be messing with. I believe that Euphoria moves stuff
around in memory as it wants, which means unless you specifically
allocate a block of memory for a variable, it won't necessarily
be in the same place next time you look for it.

Irv

new topic     » goto parent     » topic index » view message » categorize

12. Re: Pointer work around

At 04:14 a.m. 11-01-99 , you wrote:
>it's still not using anything pointer oriented in Euphoria but its a step in
>the right direction.
>

The main problem with pointers in Euphoria is that we can't pass arguments
by reference. The only work around is to make variables globals (ugghh!) or
make it a function.

Example in unchecked C (I'm no C expert):

void square(long* a)
{
        *a *= *a;
}

int main(....)
{
        long x;
        ...
        square(&x);
}

[probably this C code is wrong, but some C guy may fix it, it only shows
the idea]

In Euphoria you have two choices:

A)
        global integer x

        procedure square()
                x = x*x
        end procedure
        ...
        square()

B)
        function square(integer a)
                return a*a
        end function

        integer x
        ...
        x = square(x)

Alternative A is quite un practicable. Alternative B is what we usually do.
The problems get's worst when you're working with sequences, you have to
use (most of the times) a temporary sequence.

What I would like to see in Euphoria:
1) Passing variables by reference
2) Sequence templates

Sequence templates would be like records, and would define the general
"morphology" of a sequence.

Example:

template MySeq
        MySeq = {{integer, sequence},{atom}}
end template

sequence x of MySeq

x = {{0,"Hello"},{1000}}        -- OK
x = {{"Hello",0},{1000}} -- Wrong! Compiler error expected



Regards,
        Daniel   Berstein
        daber at pair.com

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu