Results 1 to 6 of 6

Thread: Pointers in Delphi?

  1. #1
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798

    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?

  2. #2
    Newbie
    Join Date
    Mar 2006
    Posts
    38
    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.

  3. #3
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    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.

  4. #4
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,053
    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

  5. #5
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    I knew somebody would ask that.
    Because I'm good at it.

  6. #6
    Newbie bluescreen25's Avatar
    Join Date
    Apr 2007
    Posts
    43
    Great develop language... i develop "cAVe"- frontend in delphi.... must not ever be VB....

    regards , bluescreen25

Similar Threads

  1. Dual mice and Dual Pointers
    By ThunderStick in forum Input Devices
    Replies: 8
    Last Post: 06-17-2007, 06:42 PM
  2. Destinator - Delphi
    By deanwarez in forum Software & Software Development
    Replies: 5
    Last Post: 03-27-2007, 12:34 PM
  3. Delphi programming of XM Direct - Help
    By walttoinfiniti in forum Software & Software Development
    Replies: 4
    Last Post: 01-18-2007, 09:55 PM
  4. Delphi SDK examples?
    By Nisos in forum FreeDrive
    Replies: 11
    Last Post: 04-23-2005, 05:22 PM
  5. Delphi +Dlls
    By 0l33l in forum Software & Software Development
    Replies: 35
    Last Post: 02-06-2005, 03:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •