Announcement

Collapse
No announcement yet.

Memory Management WITHOUT the WinAPI

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Memory Management WITHOUT the WinAPI

    Hi Folks,

    Just started exploring some things and figured I'd ask -

    Does anyone know how to dynamically (as in at runtime) create new variables (from bytes to udts) at runtime without:
    1) Using the WinAPI
    2) Using the Array functions (redim, etc)

    In C this would be calloc and malloc, or in C++ this would be new()

    I've seen some GREAT examples of linked lists, etc. and all use the API. Which is fine. And I obviously could use a lower level language.

    I'm just wondering if I'm missing something in the manual, or if this is more than PB is designed to do without the API.

    JS
    John,
    --------------------------------
    John Strasser
    Phone: 480 - 273 - 8798

  • #2
    Originally posted by John Strasser View Post

    Does anyone know how to dynamically (as in at runtime) create new variables (from bytes to udts) at runtime without:
    1) Using the WinAPI
    2) Using the Array functions (redim, etc)
    Code:
      local s as string
    big enough for you?

    Comment


    • #3
      At the most fundamental level the operating system has to keep track of memory allocations, and especially in Windows there is no way to bypass that fact. In the examples you gave of C's ,malloc() and calloc() C runtime functions, they have to ultimately call a Windows Api function to obtain memory if they are running in a Windows environment. If they are running in Linux/Unix then the appropriate operating system specific functions need to be called there.

      You can cause the PowerBASIC compiler to make memory allocation calls by dimensioning an Asciiz string of some specific length, or a dynamic string, but then you will have to deal with nasty casting/conversion problems if you get especially creative in what you store there. So I really don't see the point.
      Fred
      "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

      Comment


      • #4
        <snicker>

        OK, that will create it in a sub or function. But I want it to be unique and persistant. And while PBMain *is* a function I can't do this:

        Code:
         
        pseudo:
        for x = 1 to 10
        local s as long
        next x
        what I want is something along the lines of a linked list where every udt contains a pointer to another udt of the same type

        so it would look like:

        Code:
         
        type myudt
           li as long
           p as myudt ptr
        end type
         
        dim myvar as myudt
        myvar.p= new (myudt)   'new returns a ptr to myudt
        @p.p= new (myudt)   'new returns a ptr to myudt
         
        and so on. . .
        or it could even be:
        Code:
        local p as long ptr
         
        p=new(long)
        JS
        John,
        --------------------------------
        John Strasser
        Phone: 480 - 273 - 8798

        Comment


        • #5
          Presumably in order to create a dynamic sring PB is interacting with the API anyway, so I don't know what you are gaining. But to pursue the line taken - in case it goes anywhere - take the string eg. It is dynamic, so it can be extended. the "address spae" inside the string is limited to the length of the string, but no problem - you can grow the string to add a member to your linked list, or whatever. And it can be LOCAL, GLOBAL or STATIC. For example, it could be static in a Wndproc and you could pass its address around using setprop/getprop. So you could do everything you have asked for with it, though there are easier ways.

          Comment


          • #6
            I know there *are* easier ways - it's just to see if it could be done. And I know that malloc, etc. all call the OS anyway.
            This was (again) just to see if there was a self-contained way to do this.

            Sorta like: I can shell to the OS to rename a file, or I could just use the "name" command.

            JS
            John,
            --------------------------------
            John Strasser
            Phone: 480 - 273 - 8798

            Comment


            • #7
              >And I know that malloc, etc. all call the OS anyway

              What do you think happens when the compiler executes 'REDIM arrayname' or 'S="Hello"'

              ????

              (Hint: Two words; one of the words is "operating").

              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                what I want is something along the lines of a linked list where every udt contains a pointer to another udt of the same type
                It's Your Lucky Day...

                My December 1998 article "Make Haste, Not Waste" in BASICally Speaking magazine (no longer published) describes EXACTLY this; not only that, it keeps the linked list of UDTs sorted by three separate keys at one time. In April 1999 the PB/CC (Windows) version of this software was published.

                The corrected PUBLIC DOMAIN code for the PB/DOS version of the code was published at the same time. Both Windows and DOS code may be downloaded from:

                Dynamic Allocation and Linked Lists for PB/CC and PB/DOS

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Thanks Michael,

                  I found another reference to that code on another thread (searched the forums for linked lists). . . I'm still going through the code. That and Don wrote another one.

                  I just figured I'd ask - I might have missed something in the manual. Especially since this is my first foray back into programming in about 8 years (personal projects rather than professional development)

                  JS
                  John,
                  --------------------------------
                  John Strasser
                  Phone: 480 - 273 - 8798

                  Comment


                  • #10
                    Unique and persistent, no api (directly), dynamic memory alloc. can be achieved two ways (at least).

                    Code:
                    GLOBAL dynamStr AS STRING
                    'or within a function
                    STATIC dynamicStr AS STRING
                    
                    x = 600000   'this is # of bytes to be allocated and can be an expression with various results
                    dynamStr = SPACE$(x)
                    
                    'now you have 600K to use [I]so long as you only operate directly on the string memory[/I]. 
                    'You can't use eg.: 
                    dynamStr = $TRIM(dynamStr)  'which reallocates the memory.
                    MID$(dynamStr, y, z) = "asdfg" 'is OK as is:
                    ASC(dynamStr, y) = 4           'and many others.

                    Comment


                    • #11
                      ISTR with GLOBAL & STATIC you can go up to 16Mb.

                      Comment

                      Working...
                      X