Lazarus

Programming => General => Topic started by: xinyiman on May 18, 2017, 03:21:53 pm

Title: Error with integer pointer?!
Post by: xinyiman on May 18, 2017, 03:21:53 pm
Code: Pascal  [Select][+][-]
  1.  
  2.            For i:= (lHt)  downto 0 do
  3.                Move(Pointer(lPixmapInt+lScanLineSz8*(i)), Pointer(lBuffInt+(lHt-i)*lScanLineSz),lScanLineSz);
  4.  
  5.  

All variables is longint, when i build lazarus return

.pas(1520,75) Error: Variable identifier expected

Why?
Title: Re: Error with integer pointer?!
Post by: Martin_fr on May 18, 2017, 03:39:52 pm
de-reference your pointers
Code: Pascal  [Select][+][-]
  1.     For i:= (lHt)  downto 0 do
  2.                Move(PByte(lPixmapInt+lScanLineSz8*(i))^,
  3.                        PByte(lBuffInt+(lHt-i)*lScanLineSz)^,
  4.                        lScanLineSz);
  5.  
  6.  
Title: Re: Error with integer pointer?!
Post by: xinyiman on May 18, 2017, 03:41:11 pm
But in this mode build

Code: Pascal  [Select][+][-]
  1.  
  2.            For i:= (lHt)  downto 0 do
  3.            begin
  4.               a:=lPixmapInt+lScanLineSz8*(i);
  5.               b:=lBuffInt+(lHt-i)*lScanLineSz;
  6.                Move(Pointer(a), Pointer(b),lScanLineSz);
  7.            end;  
  8.  
  9.  
Title: Re: Error with integer pointer?!
Post by: Martin_fr on May 18, 2017, 03:46:55 pm
But in this mode build

Code: Pascal  [Select][+][-]
  1.  
  2.            For i:= (lHt)  downto 0 do
  3.            begin
  4.               a:=lPixmapInt+lScanLineSz8*(i);
  5.               b:=lBuffInt+(lHt-i)*lScanLineSz;
  6.                Move(Pointer(a), Pointer(b),lScanLineSz);
  7.            end;  
  8.  
  9.  

Move(a,b,s);
The compiler will take the address of a and b (as if it was Move(@a,@b,s)).

In your code a and b are variables. So the address of a and b (not the address that they point to) is taken.

Your code above will move the content of a (and any random memory after the var a), to the place where the content of var b (and any random memory after) is.
That will likely corrupt your memory.

Code: Pascal  [Select][+][-]
  1.                Move(PByte(a)^, PByte(b)^ ,lScanLineSz);
  2.  
Title: Re: Error with integer pointer?!
Post by: xinyiman on May 19, 2017, 11:35:51 am
Code: Pascal  [Select][+][-]
  1.  
  2.                 a:=lPixmapInt+lScanLineSz8*(i);
  3.                 b:=lBuffInt+(lHt-i)*lScanLineSz;
  4.  
  5.                Move(PByte(a)^, PByte(b)^,lScanLineSz);    
  6.  
  7.  

Well as you say you compile. But when I run the code goes wrong. Variable data is:

a = 65472
b = 184998450
lScanLineSz = 370

Do you know how to tell me what's wrong?

Error type:

External: SIGSEGV
Title: Re: Error with integer pointer?!
Post by: Martin_fr on May 19, 2017, 12:16:38 pm
Quote
a = 65472

This looks (most likely) wrong. (I know no OS where your apps would be at such a low position in memory)

You need to check how you calculate the values. Or show us, where you get the original values (source values) for your calculation.

But before all:
If your variables are declared as "integer" then this will only work on 32 bit cpu.
"integer" only has 32 bit, and anything above will be cut off.
You can use PtrUInt instead (or Int64 / QWord, or Pointer or PByte)

Also I recommend (If you do not already do) for testing and debugging compile with the following flags
-Criot -gh -gt
Title: Re: Error with integer pointer?!
Post by: xinyiman on May 19, 2017, 12:24:44 pm
I'm trying to compile with a lazarus a program written in delphi. I did the conversion, I put some banging, but then I can not get it to work. You find the source here:


https://sourceforge.net/projects/dicomwidow/files/dicomwidow/1.0.1.1/dicom_widow_1.0.1.1_sources_20091029.zip/download

Function SetDimension in dicomImageExt unit
Title: Re: Error with integer pointer?!
Post by: bytebites on May 19, 2017, 07:20:42 pm
CopyMemory(Destination: Pointer; Source: Pointer; Length: DWORD);
note that then order of dest and source is changed.
Move(Source^, Destination^, Length);
Title: Re: Error with integer pointer?!
Post by: Martin_fr on May 19, 2017, 07:28:39 pm
CopyMemory may be easier to read in this case.

But it doesn't change that the value for "a" seems incorrect.

I haven't worked with scanline. I know there were difference between Lazarus' scanline and Delphi's. Not sure if there still are.

As I indicated there will be an issue, if he uses "integer" on 64 bit cpu.
TinyPortal © 2005-2018