Board index » delphi » Assigning Variable to Pointer

Assigning Variable to Pointer

Hello...
I've read some Data using BlockRead from a binary file. Since I have to
calculate the memory needed to creatr a buffer on heap, I reserve it
dynamicly using GetMem. I know the Data struture exactly, there is always
1 Byte (needed as flag)
100 Byte (array of byte);
100 ...
100...
100...
100...
This structure is N times in the buffer, when one ended up, the next one
starts immediatly after this. Now I need to "jump" in the buffer and get the
Value I need.
Something like
Byte_Var := [some Pointer]

I read it...

BlockRead(FileName, FileHeader^, SizeOf(FileHeader^));
{Read File Header only, to see how many times the strucutre above
follows...}
Size_Word := (FileHeader^.Index * SizeOf(The_showed_Structure_above));
GetMem(DataPtr, Size_Word);
BlockRead(FileName, DataPtr^, SizeOf(DataPtr^));
{now how to access this buffer's variables ?}

 

Re:Assigning Variable to Pointer


omsec schrieb:

Quote

> Hello...
> I've read some Data using BlockRead from a binary file. Since I have to
> calculate the memory needed to creatr a buffer on heap, I reserve it
> dynamicly using GetMem. I know the Data struture exactly, there is always
> 1 Byte (needed as flag)
> 100 Byte (array of byte);
> 100 ...
> 100...
> 100...
> 100...
> This structure is N times in the buffer, when one ended up, the next one
> starts immediatly after this. Now I need to "jump" in the buffer and get the
> Value I need.
> Something like
> Byte_Var := [some Pointer]

> I read it...

> BlockRead(FileName, FileHeader^, SizeOf(FileHeader^));
> {Read File Header only, to see how many times the strucutre above
> follows...}
> Size_Word := (FileHeader^.Index * SizeOf(The_showed_Structure_above));

Here is your problem. It is preferred to create a TYPE of
your structure, some array[...] and Record. The final TYPE
also has a size which is simple used with Sizeof()

Quote
> GetMem(DataPtr, Size_Word);
> BlockRead(FileName, DataPtr^, SizeOf(DataPtr^));

The DataPtr has no size which is known by the compiler at
sizeof. And - if you have a TYPE you do not need the Getmem,
you can use the simple and standardize New procedure.

Quote
> {now how to access this buffer's variables ?}

Look for "Dynamic arrays" and "Turbo Pascal memory,
Pointer Primer" in the FAQ chapter of the TP-links site
for more pointer related basics.

Do not try to circumvent the Pascal type checking unless
it is really necessary!

Franz Glaser

Other Threads