Results 1 to 7 of 7

Thread: Accessing Byte values from a block of allocated memory...

  1. #1
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Accessing Byte values from a block of allocated memory...

    This should be a simple one.

    ok lets say I've allocated some memory to store audio data in one of my objects using:

    lets assume
    [pascal]var SamplePtr: Pointer;
    SampleSize: Word;[/pascal]
    and then
    [pascal]GetMem(SamplePtr, SampleSize);[/pascal]
    then
    [pascal]ReadBlock(FileStream, SamplePtr^, SampleSize);[/pascal]

    now...

    How do I access this 8-bit audio data as if it were simply an array of Byte values? Or in some other similar way. Some code would be great.


    [size=7px]I can't believe I've never done this in Win32 before.[/size]
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #2

    Accessing Byte values from a block of allocated memory...

    You could do this:
    [pascal]type
    TByteArray = array[0..MaxInt] of Byte;

    ...

    x := TByteArray(SamplePtr)[1];[/pascal]
    I'm not too sure about the syntax there, but I know that this is possible.
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  3. #3

    Accessing Byte values from a block of allocated memory...

    Useless Hacker is close with his suggestion, but you need another type:

    [pascal]type
    TByteArray = array[0..MaxInt - 1] of Byte;
    PByteArray = ^TByteArray; // and this[/pascal]
    Use the above for your typecast in the code (x := PByteArray(SamplePtr)[some_index]). Note that the size of the initial array type is only really 'important' in two ways:

    The compiler will complain if you access an element using a hard-coded number that's not in the range (e.g. TByteArray = array[0..1] of Byte, then you access MyArray[23] in code). That's a compile-time error.

    Range checking (which I never remember to enable anyway) may trigger problems here, thinking you've overran the array boundaries (it doesn't know any better!).

    The above two are the reasons why you usually use as big a declared array size as possible before using a pointer to it (you'll sometimes see "array[0..0]" types, then a pointer to them, in some code). Also, note that PByteArray is declared in SysUtils, so you don't necessarily have to redeclare the type unless you want to.
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  4. #4

    Accessing Byte values from a block of allocated memory...

    Oh, and of course your code will be clearer if you use a variable to get rid of all the typecasts later, perhaps like this:

    [pascal]var
    Blah: Pointer;
    Another: PByteArray;
    begin
    Blah := DoSomethingToGetPointer;

    Another := PByteArray(Blah); // here, the important bit

    // now, it's clear all the way -- just like an array...
    Another[0] := 1;
    Another[1] := 23;
    Another[2] := $FF;
    end;[/pascal]

    EDIT: And another thought. Are you sure you can't just use a dynamic array? It would make your life easier, you know! If you desperately need a pointer then it's not difficult to get one (just the address of element 0 after all).
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  5. #5

    Accessing Byte values from a block of allocated memory...

    (offtopic..)
    Isn't newer Delphi supporting Pointer arithmetics ?? I know there are several tutorial for accessing a pixel in allocated mem for delphi3/4. They all use complicated pointer<->int conversion.

    Code:
    // freepascal code
    VAR p     &#58; pointer;
           size &#58; longint;
           pb   &#58; ^BYTE;
    
    Getmem&#40; p, size &#41;;
    pb &#58;= p;
    &#40;pb + number&#41;^&#58;=255;

  6. #6
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Accessing Byte values from a block of allocated memory...

    All: Thanks for your responses. I managed to get what I needed.

    Alimonster: I am afraid I do. Dynamic arrays in class objects don't seem to keep well. I always run into problems. Also I will be feeding this data to OpenAL & DirectSound for playback so I'll need to keep it in a very compatable formatwith these.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  7. #7

    Accessing Byte values from a block of allocated memory...

    Quote Originally Posted by holybyte
    (offtopic..)
    Isn't newer Delphi supporting Pointer arithmetics ?? I know there are several tutorial for accessing a pixel in allocated mem for delphi3/4. They all use complicated pointer<->int conversion.

    Code:
    // freepascal code
    VAR p     &#58; pointer;
           size &#58; longint;
           pb   &#58; ^BYTE;
    
    Getmem&#40; p, size &#41;;
    pb &#58;= p;
    &#40;pb + number&#41;^&#58;=255;
    use:
    Code:
    VAR size &#58; longint;
           p   &#58; PByteArray;
    
    Getmem&#40; p, size &#41;;
    p&#91;number&#93; &#58;=255;
    Much nicer code.

    As far as I know even Turbo Pascal supported pointer arithmetic.


    This code will walk over a raw byte a raw byte array backwards using pointer arithmetic. Inc() & dec() increase & decrease the pointer by the sizeof what the pointer points to(in this case a byte)
    Code:
    VAR 
      size &#58; longint;
      p &#58; pointer;
      pb   &#58; ^Byte;
    
    Getmem&#40; p, size &#41;;
    pb &#58;= p;
    inc&#40; pb, size div sizeof&#40;pb^&#41; &#41;;
    while pb <> p do
      begin
      dec&#40;pb&#41;;
      ...
      end;

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
  •