Author Topic: GLBasic pointer question  (Read 1700 times)

Offline nabz32

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 307
    • View Profile
GLBasic pointer question
« on: 2017-Jul-23 »
Hello again,

only a small question, is it possible to build a linked list
in glbasic, something like:

Code: GLBasic [Select]
TYPE element

*element // store the next element in the list..

ENDTYPE
 

And then check if this Pointer is NULL?

Or is ist possible to access a c class inside the GLBasic code, without using EXTERN "C" all the time?
Having the abillity to write a linked list in GLBasic would be awesome.


Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5028
  • O Rly?
    • View Profile
Re: GLBasic pointer question
« Reply #1 on: 2017-Jul-24 »
You can use INLINE and put a List into a DGArray, return the Index of it and u can use it in types without having problems.

Fun-Fact:
I've managed to put a wrapper of a c++ Class into a GLBasic Type... it was a try and it works perfectly :)
You can try to wrapp the Linked List into a GLB-TYpe.

I can try to send you an example later today.
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline nabz32

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 307
    • View Profile
Re: GLBasic pointer question
« Reply #2 on: 2017-Jul-24 »
thanks Schranz0r,

pretty good news for me  :good:

A small example would be greatly appreciated

Offline nabz32

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 307
    • View Profile
Re: GLBasic pointer question
« Reply #3 on: 2017-Jul-26 »
I tried to create a class in inline, which compiles without errors,

I can create an object of it in INLINE,
and also create a DGArray of the class type.

But when I try to use DIMPUSH() on the DGArray in INLINE, I get countless errors suddenly.

Here is the Global INLINE Code:
Code: GLBasic [Select]
INLINE
        class SEdgeC{
                private:
                        DGInt x;
                        DGInt y;
                        SEdgeC* nextEdge;
                public:
                        SEdgeC( DGInt xN , DGInt yN );
                        SEdgeC* getNextEdge();
                        void addNewEdge(DGInt x , DGInt y );
                        void deleteAll();
        };
        SEdgeC::SEdgeC( DGInt xN , DGInt yN ){
                x = xN;
                y = yN;
                nextEdge = NULL;               
        }
        SEdgeC* SEdgeC::getNextEdge(){
                return nextEdge;
        }
        void SEdgeC::deleteAll(){
                if( nextEdge != NULL ){
                        nextEdge->deleteAll(); 
                }
                delete this;           
        }
        void SEdgeC::addNewEdge( DGInt xN , DGInt yN ){
                if( nextEdge != NULL ){
                        nextEdge->addNewEdge( xN , yN ); // add nextEdge to my nextEdge, if I allready have one
                } else nextEdge = new SEdgeC( xN , yN );        // if not then finally create the new edge
        }
        DGArray<SEdgeC> Sectors;
ENDINLINE
 

and the local INLINE code I used in my main function:
Code: GLBasic [Select]
INLINE
SEdgeC start( 0 , 0 );
start.addNewEdge( 0 , 0 );
ENDINLINE
 

this is all working, but when I call
Code: GLBasic [Select]
INLINE
DIMPUSH(Sectors,start);
ENDINLINE

I get tons of weird errors, telling me that every statement in the INLINE class is faulty.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10723
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: GLBasic pointer question
« Reply #4 on: 2017-Aug-04 »
Oh dear. You need a default constructor (no args).
You need a copy operator and a copy constructor. Take care what you do with the pointer so you don't have dangling pointers and multiple delete calls in the end.
The array is not going to be debugable (V15) this way - that's a bit more complicated.

What are you trying to achieve with this? You really think that a a linked list is faster than an array of types with line-array? It might be, but most times it's not, because the most time consuming usually is allocating memory. And an array has less calls to malloc/new than a linked list.
For very big data you are right, though, since you don't need the copies when you have to re-allocate more memory.