Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: array of byte : how to compute its length

  1. #1

    array of byte : how to compute its length


    Code:
    function getPtrLength(_ptr : PBYTE) : integer;
    begin
    Result := 0;
      while ( _ptr <> NIL ) do
      begin
         inc(Result);
         inc(_ptr);
      end;
    end;
    why this function cause a loop for ever when passed an array of byte

    when use it like this
    Code:
    var 
      ptr , _start : PByte;
    begin
      getmem(ptr,100);
      _start := ptr;
      for i := 1 to 100 do
      begin
         ptr^ := Random(100); // fill our array with random numbers
         inc(ptr);
      end;
    //use our function here
    ShowMessage(inttostr(getPtrLength(_start))); // expected result is 100 !! but its not
    end.
    whats wrong here , thanks

  2. #2

    Re: array of byte : how to compute its length

    You are looking for a null char, but there is no null char in the array. (You fill all 100 elements with random)
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  3. #3

    Re: array of byte : how to compute its length

    You can't compute the length of an arbitary chunk of memory unless it's explicitly given be the nature of the datastructure or the os/memory manager has that functionality

    If it's a nullterminated array of byte, then you simply forget to dereference before checking if it's nil(0). eg:

    [pascal]
    function getPtrLength(_ptr : PBYTE) : integer;
    begin
    Result := 0;
    while ( _ptr^ <> 0 ) do
    begin
    inc(Result);
    inc(_ptr);
    end;
    end;
    [/pascal].

    And as Andreaz is saying, you don't have any data structure in your array in your example
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  4. #4

    Re: array of byte : how to compute its length

    [pascal]
    function getPtrLength(_ptr : PBYTE) : integer;
    begin
    Result := 0;
    repeat
    inc(Result);
    inc(_ptr);
    until _ptr^ = 0;

    //i believe repeat will be better as the cmp instruction should be placed at the end of the loop rather than to waste 1 extra jmp instruction.
    end;


    var
    ptr , _start : PByte;
    begin
    getmem(ptr,101); //1 end byte
    _start := ptr;
    for i := 1 to 100 do
    begin
    ptr^ := Random(100); // fill our array with random numbers
    inc(ptr);
    end;
    ptr^ := 0;
    //use our function here
    ShowMessage(IntToStr(getPtrLength(_start))); // expected result is 100 !! but its is
    end.
    [/pascal]
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  5. #5

    Re: array of byte : how to compute its length

    [pascal]
    // expected result is 100 !! but its is
    [/pascal]
    However ptr^ := Random(100); can also place 0 within the array which would terminate length function early. Unless you manage data so there is no null inside it is more "proper" to store the length within a separate variable or possibly at the beginning of array.

  6. #6

    Re: array of byte : how to compute its length

    thanks , all i want to do is calculat the size of a given file manually

    so after reading the file

    BlockRead(File,_Ptr,size//supose that we know the size already);
    // use of the function
    filesize := GetPtrLength(_ptr);

    you may say why you calculate the size and u know it already

    i just want to know how to get the size of a memory file



  7. #7
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    Re: array of byte : how to compute its length

    You could always use streams...

    TMemoryStream

    Then you'll immediately have the 'size' property and you'll still be able read and write in much the same manner.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  8. #8

    Re: array of byte : how to compute its length

    Quote Originally Posted by User137
    [pascal]
    // expected result is 100 !! but its is
    [/pascal]
    However ptr^ := Random(100); can also place 0 within the array which would terminate length function early. Unless you manage data so there is no null inside it is more "proper" to store the length within a separate variable or possibly at the beginning of array.
    good point i didnt look at that but either way, you can just add a 1 to the random(100), and when you grab the value from the ptr you just -1, however, if the length is going to be below 255 bytes, just use the first byte to record the length of the data when it is written.

    obviously you can also do larger by allocating upto 4 bytes... or even 8 bytes to record datalen

    also TMemoryStream is a class, which will most likely be slower, classes are created on the heap (which is slower btw). also he asked how to check manually, using a memory stream would be cheating.

    anyways the problem you will have (if you were do check without knowing the size from a header or via api), many files can contain terminators... lol (null chars), exe files for example.

    -Colin
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  9. #9

    Re: array of byte : how to compute its length

    I think the original question was already answered, if you have a pointer to array or any data at all, it is not possible to find out its length unless it is terminated and very often it is not.

    If you need to know size of a file it is asked from operating system using function provided by API, and same program may not work in Linux or other OS. But i don't honestly know specifics on how files are terminated if they are, and what differences there are in NTFS and FAT32 etc...

  10. #10

    Re: array of byte : how to compute its length

    Blockread(file pointer, buffer, max size, var BytesRead)

    There is no need for calculating the size, you got them from paramter BytesRead.

    Regards
    Thomas

Page 1 of 2 12 LastLast

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
  •