Announcement

Collapse
No announcement yet.

Pointers in Delphi?

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

  • Pointers in Delphi?

    I know this is a long-shot since hardly anybody uses Delphi nowadays, but in a sad effort to make an efficient program, Im begging.

    Heres my structures:

    Code:
      SongList: Array of TSong;
      Albums: Array of TAlbum;
      Artists: Array of TArtist;
    
    TSong = record
        ID: Integer;               // ID for loading from cache and flash
        Title: String;
        Artist: String;
        Album: String;
        Filename: String
      end;
    
      // Album has a reference to the Song stored in the master song list
      TAlbum = record
        ID: Integer;               // ID for loading from cache and flash
        Title: String;
        Artist: String;
        Picture: String;
        Path: String;
        Songs: Array of ^TSong;
      end;
    
      // Artist contains a reference to the Album stored in the master album list
      TArtist = record
        ID: Integer;
        Title: String;
        Path: String;
        Albums: Array of ^TAlbum;
      end;
    There is a main list of Songs, Albums and Artists.
    In the artists list, there is an "Albums" element which is an array of POINTERS to objects in the main albums list.
    It sounds confusing but the concept is simple.

    Anyway, I parse all my music fine. When I try to access it though, I get messed up errors:

    Code:
    Faber Drive
     - 1 Albums
        - 33
    Gorillaz
     - 4 Albums
        - 34
        - 35
        - 36
        - 37
    Guns N Roses
     - 6 Albums
        - 1934974067
        - 543908705
        - 2
        - 1667855221
        - 1226863221
        - 1886203507
    Iron Maiden
     - 23 Albums
        - 1432107587
        - 541991027
        - 1936617321
        - 0
        - 1819231068
        - 1934974067
        - 543449445
        - 2
        .
        . ETC
        .
        - 1432107587
    Jack Johnson
     - 5 Albums
        - 541991027
        - 1936617321
        - 33685504
        - 1819231068
        - 1934974067
    Jay Z & Linkin Park
     - 1 Albums
        - 1701212773
    Linkin Park
     - 3 Albums
        - 2
        - 1667855221
        - 1226863221
    Meatloaf
     - 6 Albums
        - 1886203502
        - 1432107587
        - 541991027
        - 1936617321
        - 33832632
        - 1819231068
    Rage Against The Machine
     - 4 Albums
        - 82
        - 83
        - 84
        - 85
    ** Those are accessed by Artists[I].Albums[J].ID so it should be listing the album's ID number. It works perfectly fine until a random point at which the numbers get all screwed up!

    Any ideas?
    2001 Mustang Convertible Worklog
    Indigo Custom Frontend (Flash/Delphi)
    Blog

    Qube v1.3 Now Available at the mp3Car Store!!!!!!
    The simplest IO controller you'll ever use!

  • #2
    I'm guessing you are allocating memory when constructing the arrays? Its also looks like you are not de-referncing the pointer arrays when accessing them...

    My advice is to use the following:

    TSongArray : Array of TSong;
    TAlbumArray : Array of TAlbum;
    TArtistArray : Array of TArtist;


    TSong = record
    ID: Integer; // ID for loading from cache and flash
    Title: String;
    Artist: String;
    Album: String;
    Filename: String
    end;

    // Album has a reference to the Song stored in the master song list
    TAlbum = record
    ID: Integer; // ID for loading from cache and flash
    Title: String;
    Artist: String;
    Picture: String;
    Path: String;
    Songs: TSongArray;
    end;

    // Artist contains a reference to the Album stored in the master album list
    TArtist = record
    ID: Integer;
    Title: String;
    Path: String;
    Albums: TAlbumArray;
    end;

    Not a pointer in site. Use SetLength(open array, Length(openarray)+1) to extend open array by one item, and then openarray[High(openarray)] to set its contents.

    The alternative (and object oriented approach) would be to use TObjectList and create each "record" as an object with associated attributes / methods.

    Comment


    • #3
      I figured this was dead so I didnt bother posting the solution.

      The problem was this:
      - Every time you resize an array, you create a NEW one, and copy all the information to it.
      - I had pointers to locations in the first array.
      - When I resized the arrays, my pointers were pointing to locations that no longer held the data I needed -- RANDOM NUMBERS

      Thanks though.
      2001 Mustang Convertible Worklog
      Indigo Custom Frontend (Flash/Delphi)
      Blog

      Qube v1.3 Now Available at the mp3Car Store!!!!!!
      The simplest IO controller you'll ever use!

      Comment


      • #4
        I guess the question becomes... why are you still using Delphi?!
        Fusion Brain Version 6 Released!
        1.9in x 2.9in -- 47mm x 73mm
        30 Digital Outputs -- Directly drive a relay
        15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
        Buy now in the MP3Car.com Store

        Comment


        • #5
          I knew somebody would ask that.
          Because I'm good at it.
          2001 Mustang Convertible Worklog
          Indigo Custom Frontend (Flash/Delphi)
          Blog

          Qube v1.3 Now Available at the mp3Car Store!!!!!!
          The simplest IO controller you'll ever use!

          Comment


          • #6
            Great develop language... i develop "cAVe"- frontend in delphi.... must not ever be VB....

            regards , bluescreen25

            Comment

            Working...
            X