Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-04

Getmem -> Move -> Freemem

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Guillermo Martínez Jiménez

Hello.

I'm using
I need to reserve dynamic memory to store raw binary data. I tried the next code:

  fSize := aObject^.size;
  GetMem (fData, fSize);
  Move (aObject^.dat, fData, fSize);

Both "fData" and "dat" are untyped POINTER but the program returns an "Segment violation" at the Move procedure. I've checked the pointers and both are valid (I mean not NIL) and size is always more than zero.

I've read some documentation about the heap memory but I can't understand why my code doesn't works.

Guillermo Martínez Jiménez
to Guillermo Martínez Jiménez

Hello.

I'm using FPC 2.2.2 on Xubuntu 8.10.

I need to reserve dynamic memory to store raw binary data. I tried the next code:

  fSize := aObject^.size;
  GetMem (fData, fSize);
  Move (aObject^.dat, fData, fSize);

Both "fData" and "dat" are untyped POINTER but the program returns an "Segment violation" at the Move procedure. I've checked the pointers and both are valid (I mean not NIL) and size is always more than zero.

I've read some documentation about the heap memory but I can't understand why my code doesn't works.

Any idea?

Guillermo "Ñuño" Martínez

Marco van de Voort
to Guillermo Martínez Jiménez: Hello. I'm using I need to reserve dynamic memory to store raw binary data. I tried the…

In our previous episode, Guillermo Mart�nez Jim�nez said:

Show quoted text 7 lines
fSize := aObject^.size; GetMem (fData, fSize); Move (aObject^.dat, fData, fSize); Both "fData" and "dat" are untyped POINTER but the program returns an "Segment violation" at the Move procedure. I've checked the pointers and both are valid (I mean not NIL) and size is always more than zero.

Both are .dat and fdata are points. IOW you are moving "fsize" bytes from the place where the 4 byte of the pointer dat is stored to the place where the 4 bytes of the pointer fdata is stored. Since you probably move more than 4 bytes you totally corrupt what is behind "fdata" in memory.

Try .dat^ and fdata^ in the move line.

Guillermo Martínez Jiménez
Show quoted text 6 lines
Both are .dat and fdata are points. IOW you are moving "fsize" bytes from the place where the 4 byte of the pointer dat is stored to the place where the 4 bytes of the pointer fdata is stored. Since you probably move more than 4 bytes you totally corrupt what is behind "fdata" in memory. Try .dat^ and fdata^ in the move line.

Thanks. That fixes it partially.

Now it fails trying to copy 998846 bytes. If I put an "IF fSize < 900000 THEN..." to prevent move more than 900000 bytes it does work and I'm sure it moves up to 206737 bytes. But "size" is LONGINT so IIRC it should be able to copy ~2Gib of data.

Does it have a size limit? If so, is there a way to copy all that data without doing it byte-by-byte?

Guillermo "Ñuño" Martínez

Jonas Maebe
to Guillermo Martínez Jiménez
Guillermo Martínez Jiménez wrote 6 lines
Now it fails trying to copy 998846 bytes. If I put an "IF fSize < 900000 THEN..." to prevent move more than 900000 bytes it does work and I'm sure it moves up to 206737 bytes. But "size" is LONGINT so IIRC it should be able to copy ~2Gib of data. Does it have a size limit?

No. This probably means that one of your pointers does not point to a block with the size you expect.

Jonas