Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: array of byte : how to compute its length

  1. #11

    Re: array of byte : how to compute its length

    Quote Originally Posted by Colin
    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
    I am not make clear what is the point!
    If it is a array, why not use the High(ByteArray) to get the length?
    and, as I know, the null char is #0 not 0, I think maybe, there is difference
    between #0 and 0., so, if you use Random(100), you may get 0, but you will not
    get #0
    if you just want to calc the file size when reading a file,
    TFileStream is a good choice, just create and read the property 'size'.
    If insisting calc the size by you self:
    [pascal]
    Var
    Buffer : array[0..10*1024-1] of char;
    i: integer;
    totalsize : int64;

    begin
    totalsize :=0;
    //create the TFileStream first....
    repeat
    i := theFileStream.Read(Buffer,10*1024-1);
    totalsize := totalsize + i;
    until i < 10*1024-1;
    end;
    [/pascal]


  2. #12

    Re: array of byte : how to compute its length

    thanks for the clarification

    i understand that i can not find the end of an arbitary chunk of memory even i've the adress of the first element .unless u know its size

    i thought that NIL is the default END of any new allocation in memory



  3. #13

    Re: array of byte : how to compute its length

    Quote Originally Posted by Chien
    and, as I know, the null char is #0 not 0, I think maybe, there is difference
    between #0 and 0., so, if you use Random(100), you may get 0, but you will not
    get #0
    #0 is pascal specific, it is just for the compiler. #0 meaning char(0) or byte(0) = as u shoud know, there is no such thing as #0 in machine code, it is 0.

    #0 = 0, and you can not use high on a byte array that would be dynamic.

    @virtual

    nil, null or nul are all just human readable code.

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

  4. #14

    Re: array of byte : how to compute its length

    Quote Originally Posted by Colin
    and you can not use high on a byte array that would be dynamic.
    I usually use following code to do something, it works ok:

    [pascal]
    procedure dynamicArray(inputParam : array of integer);
    Var
    i : integer;
    begin
    for i := 0 to High(inputParam) do
    ....
    end;
    [/pascal]

    Althrough it is dynamic, but the machine can tell you how long it is!


  5. #15

    Re: array of byte : how to compute its length

    [pascal]Var
    ptr , _start : PByte;
    i : integer;
    arr: array of integer;
    begin
    setlength(arr,100);
    ptr:=@arr;
    _start := ptr;
    for i := 1 to 100 do
    begin
    // fill our array with random numbers...
    // except that with this kind of dynamic array which isn't structured
    // same way this cannot work, unless ptr:=@arr[0];
    // which would be cheating, and if you pass @arr[0] to length function
    // you would not be able to use high() anymore
    ptr^ := Random(100);

    inc(ptr);
    end;
    ...
    end;[/pascal]

  6. #16

    Re: array of byte : how to compute its length

    Quote Originally Posted by Chien
    Quote Originally Posted by Colin
    and you can not use high on a byte array that would be dynamic.
    I usually use following code to do something, it works ok:

    [pascal]
    procedure dynamicArray(inputParam : array of integer);
    Var
    i : integer;
    begin
    for i := 0 to High(inputParam) do
    ....
    end;
    [/pascal]

    Althrough it is dynamic, but the machine can tell you how long it is!

    the var is not dynamic, you have by this time already set the length of the array, however, if trying to get the size of the file, without knowing the size of the file, you can not set the length of the array, therfore you would be calling high on an empty array, this is impossible. you need to re-read the previous posts.

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

Page 2 of 2 FirstFirst 12

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
  •