[5.10] Native types can't be used with pointers

Just starting out? Need help? Post your questions and find answers here.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [5.10] Native types can't be used with pointers

Post by Fred »

No, when constructing a structure you need to know the whole size of it when EndStructure is called. It's the same in C/C++ for example and changing that will be very wierd and will have serious side effects (like SizeOf() being broken, OffsetOf() as well etc).
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [5.10] Native types can't be used with pointers

Post by Psychophanta »

Of course.
But please take in account if you forbid all types for pointers inside structures, then will be problems for some programs. This would suppose a regression!
Last edited by Psychophanta on Thu Jan 17, 2013 2:00 pm, edited 1 time in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [5.10] Native types can't be used with pointers

Post by Fred »

Only native type are forbidden, please read my posts more carefully, thanks.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [5.10] Native types can't be used with pointers

Post by Psychophanta »

Oh, not enougth descriptive to me some of your posts, what a scare!.
Agree now! and tx
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: [5.10] Native types can't be used with pointers

Post by PMV »

Taken to long to find the old discussion about the topic with
not defined structures used with pointers in structures,
but finally i have found it ... even so this topic is now finished
here it is:
http://www.purebasic.fr/english/viewtop ... 7&p=400211

MFG PMV
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: [5.10] Native types can't be used with pointers

Post by BorisTheOld »

Sigh!!!!! This whole discussion is a waste of bandwidth.

A pointer is just a pointer to a location in memory, and there's no universal law that says it has to be defined in a particular way. Every language I've ever worked with (and that's a lot) has had its own rules regarding pointers. Some are strictly typed, others are loosely typed, and others are not typed at all.

If PB forbids the use of native types with pointers, then get over it. That's just the way the language works. And if one feels that this feature is needed for documentation purposes, then use comments, that's what they're for.

Personally, I like PB pointers just the way they are.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [5.10] Native types can't be used with pointers

Post by Psychophanta »

BorisTheOld wrote:For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string
I like it.
It goes now to my collection. :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [5.10] Native types can't be used with pointers

Post by luis »

BorisTheOld wrote: Personally, I like PB pointers just the way they are.
I would like a little more, but we can go by with what we have.

The biggest shortcoming I often encounter is this: you cannot increment a pointer value in an expression and use the resulting new address. You need to alter the value of the pointer variable permanently and then assign the value.

Code: Select all

*x.Integer

(*x + 2)\i  = 10  ;  we don't have this (add 2 to the pointer and store 10 at the resulting address)

It would be nice. You can do it in other ways (the quickest making a copy of the pointer an alter that one) but it's something I miss.

There are other things but those would require a lot more work to be implemented, probably not worth it.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: [5.10] Native types can't be used with pointers

Post by Danilo »

luis wrote:The biggest shortcoming I often encounter is this: you cannot increment a pointer value in an expression and use the resulting new address. You need to alter the value of the pointer variable permanently and then assign the value.

Code: Select all

*x.Integer

(*x + 2)\i  = 10  ;  we don't have this (add 2 to the pointer and store 10 at the resulting address)

It would be nice. You can do it in other ways (the quickest making a copy of the pointer an alter that one) but it's something I miss.
Using pointers automatically as arrays (like C) would be cool:

Code: Select all

*x.Integer = AllocateMemory( 10 * Sizeof(Integer) )
If *x
    *x[0]\i = 10
    *x[5]\i = 12
EndIf
PB emulation:

Code: Select all

Macro DefPointerArray( __array_name__, __array_type__, __member_name__=mem )
    Structure __array_name__
        __member_name__.__array_type__#[0]
    EndStructure
EndMacro

DefPointerArray(ByteArray     , b, b)
DefPointerArray(AsciiArray    , a, a)
DefPointerArray(CharacterArray, c, c)
DefPointerArray(WordArray     , w, w)
DefPointerArray(UnicodeArray  , u, u)
DefPointerArray(LongArray     , l, l)
DefPointerArray(IntegerArray  , i, i)
DefPointerArray(FloatArray    , f, f)
DefPointerArray(DoubleArray   , d, d)
DefPointerArray(QuadArray     , q, q)
DefPointerArray(StringArray   , s, s)


Debug "--------------------------------------------------------"

*p1.LongArray = AllocateMemory(10 * SizeOf(Long))
If *p1

    For i = 0 To 9 : *p1\l[i] = i   : Next

    For i = 0 To 9 : Debug *p1\l[i] : Next

EndIf


Debug "--------------------------------------------------------"

*p2.ByteArray = *p1
If *p2

    For i = 0 To 39 : a$ + Chr( *p2\b[i] + '0' ) + "," : Next

    Debug a$

EndIf


Debug "--------------------------------------------------------"

Structure myPoint
    x.l
    y.l
EndStructure

DefPointerArray(myPointArray,myPoint)

*pt.myPointArray = AllocateMemory( 3 * SizeOf(myPoint) )
If *pt

    *pt\mem[0]\x = 10
    *pt\mem[0]\y = 20
    *pt\mem[1]\x = 60
    *pt\mem[1]\y = 70
    *pt\mem[2]\x = 110
    *pt\mem[2]\y = 120

    *ptLong.LongArray = *pt
    
    For i = 0 To 5 : Debug *ptLong\l[i] : Next

EndIf


Debug "--------------------------------------------------------"

DefPointerArray(Str3Arr,s{3})

*s.Str3Arr = AllocateMemory( 3 * 3 * SizeOf(Character) )
If *s
    *s\mem[0] = "abc"
    *s\mem[1] = "def"
    *s\mem[2] = "ghi"

    *c.CharacterArray = *s
    
    For i = 0 To 8 : Debug Chr( *c\c[i] ) : Next

EndIf
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [5.10] Native types can't be used with pointers

Post by luis »

Danilo wrote: Using pointers automatically as arrays (like C) would be cool:
Yes, this was one the other thing I was thinking about but didn't mention because I believe there can be hope for my first wish but this second one is something I don't see coming :)

The idea of using a structure to "jump" the right amount for the offset like C does natively based on the pointed data type is certainly interesting, problem is you still have to specify the underlying field

*p1\l = i

instead of

*p1 = i

and this kills the magic for me.

But thanks for the nice example :wink:
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: [5.10] Native types can't be used with pointers

Post by Danilo »

luis wrote:The idea of using a structure to "jump" the right amount for the offset like C does natively based on the pointed data type is certainly interesting, problem is you still have to specify the underlying field

*p1\l = i

instead of

*p1 = i

and this kills the magic for me.

The reason for that is the topic name: "[5.10] Native types can't be used with pointers". :)

You can only use structures with PB-Pointers, so even if they would add the auto-array-dereferencing for pointers,
you would need to write something like:

Code: Select all

*p1[i]\l = i
You would always need to use a structure member anyway, because pointers are only supported with structures, not simple types.

Code: Select all

*p1[i]\l = i ; PB way if they would enable it
*p1\l[i] = i ; using DefPointerArray
Native PB support would allow to write:

Code: Select all

*pt.myPoint = AllocateMemory( 3 * SizeOf(myPoint) )
If *pt

    *pt[0]\x = 10
    *pt[0]\y = 20
    *pt[1]\x = 60
    *pt[1]\y = 70
    *pt[2]\x = 110
    *pt[2]\y = 120
directly for complex structures, instead ugly:

Code: Select all

*pt.myPointArray = AllocateMemory( 3 * SizeOf(myPoint) )
If *pt

    *pt\mem[0]\x = 10
    *pt\mem[0]\y = 20
    *pt\mem[1]\x = 60
    *pt\mem[1]\y = 70
    *pt\mem[2]\x = 110
    *pt\mem[2]\y = 120
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: [5.10] Native types can't be used with pointers

Post by fsw »

Psychophanta wrote:
BorisTheOld wrote:For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string
I like it.
It goes now to my collection. :wink:

... it doesn't make any sense to me :shock:
Caesar was stabbed not hanged...

Maybe I just don't understand it...

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [5.10] Native types can't be used with pointers

Post by skywalk »

Yes, can someone please explain what the heck this means :?:
For ten years Caesar ruled with an iron hand. Then with a wooden foot, and finally with a piece of string.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: [5.10] Native types can't be used with pointers

Post by LuCiFeR[SD] »

You have to be a fan of "The goons" to understand :) Spike Milligan's sense of humour was unique to say the least :)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [5.10] Native types can't be used with pointers

Post by Psychophanta »

You can also do this slow and ugly way:
Wrote nothing , grrr, it does not work
Last edited by Psychophanta on Fri Jan 18, 2013 8:47 pm, edited 1 time in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply