Recent

Author Topic: Problem with pointers  (Read 16561 times)

_piotr

  • Newbie
  • Posts: 2
Problem with pointers
« on: October 21, 2009, 10:23:59 pm »
Hi!
 I am rewriting C++ program to Lazarus. Hovewer I have, a problem with the following statement:
   
    float a;
    long *p;
    p = (long *) &a;

How it would look in Lazarus/free Pascal?

   a: real;
   p: ^longint;

   p:= ????????????????

Thanks for any help!
-------------------------------------------------------
Sorry for any errors, I am not native english speaker.
 

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
Re: Problem with pointers
« Reply #1 on: October 21, 2009, 10:59:09 pm »
I understand little of C, but I think it translates to this (prob. dangerous code):

Code: [Select]
var a: single;
p: plongint;
begin
  p:=plongint(@a);
end;

leocda

  • Jr. Member
  • **
  • Posts: 60
  • Computer Graphics researcher
    • Picture to People
Re: Problem with pointers
« Reply #2 on: October 22, 2009, 02:52:31 am »
First of all, don't use "real". It is a platform dependent type.

You MUST use "single" to be sure you have 4 bytes of data, since you have a pointer to 4 bytes.

I imagine what a low level task could need to put a data type in a pointer for another type. Maybe you wanna make binary shift with you floating point number, what is not a ordinary behavior anyway.

_piotr

  • Newbie
  • Posts: 2
Re: Problem with pointers
« Reply #3 on: October 22, 2009, 12:52:07 pm »
Thank you very much!!! This really helped:
Quote
var a: single;
p: plongint;
begin
  p:=plongint(@a);
end;

It was one of steps to get a IEEE 754 representation of a floating-point value.
Code: [Select]
var
  ErrorMsg: String;
  var a: single;
  p: plongint;
  l: longint;
  tbl: array [1..32] of boolean;
  i: integer;
begin
  read(a);
  p:=plongint(@a);
  for i:=32 downto 1 do
      begin
           l:=p^;
           l:=l and longint(1);
           if (l=1) then
              tbl[i]:=true
           else
              tbl[i]:=false;
           p^:=p^ shr 1;
      end;
  for i:=1 to 32 do
      begin
           if((i=2) or (i=10)) then write('|');
           if (tbl[i]) then
              write('1')
           else
              write('0');
      end;
  read(a);
  Terminate;
end;   

leocda

  • Jr. Member
  • **
  • Posts: 60
  • Computer Graphics researcher
    • Picture to People
Re: Problem with pointers
« Reply #4 on: October 22, 2009, 04:36:52 pm »
It works, but it's really very slow.

Here a better way to make a "binary string" from a Single. You can adapt it to your needs.
It works fine for the Intel endian style (little endian).

Code: [Select]
function SingleToBinary(Val: Single): String;
var aux: LongInt;
    i, j: Byte;
begin
    // Hopely aux will be put in a register.
    aux := LongInt(@Val);

    // Just one memory allocation for the whole string.
    result := '00000000000000000000000000000000';

    j := 1;
    for i := 0 to 31 do begin
        // Binary shift has the same cost, no matter
        // how many bit you will cut off.
        if ((aux shl i) and $100000000) > 0 then
           // Just setting. No memory reallocation.
           result[j] := '1';
        j := j + 1;
    end;
end;

 

TinyPortal © 2005-2018