Recent

Author Topic: Pointers  (Read 5150 times)

macrillo

  • New Member
  • *
  • Posts: 18
Pointers
« on: August 16, 2014, 11:43:25 pm »
I know I've haven't read all of the Posts in this Forum to see if my
problem was already curred...
I just upgraded from fpc.2.4.1 to 2.6.4 ( the latest available for downloading ).
2.4.1 has worked for me just fine but I thought why not, i'ts been  like 4 years!! since I upgraded

Now my code doesn't compile, who knew. ...
First off Pointer action
Why am I not allowed to do this

chunk := (stsc.Buffer+i)^.first_chunk;
Where stsc.Buffer is a pointer to start of memory where (stsc_record) begin ?
It is allowed and working in 2.4.1



Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Pointers
« Reply #1 on: August 17, 2014, 12:11:38 am »
Hi and welcome to forum.

Please tell us:
what error messages you got,
how the variables (chunk, i, stsc, stsc.Buffer etc.) are declared,
what compiler mode you use (objfpc. delphi).

Some piece of (compilable) code is welcome, of course.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

macrillo

  • New Member
  • *
  • Posts: 18
Re: Pointers
« Reply #2 on: August 20, 2014, 09:30:55 pm »
Ok, of course you need more info but I thought this was some 'common' problem.

I use Delphi compatible setting target win32 for i386, pentium 4, mostly standard settings.
I didn't have to change much from default. I will give some declarations and compiler messages
( Can you copy them somehow and paste ? ) I use the FPC IDE.

Type
        stsc_entry = Record
         first_chunk                      : Dword;
         samples_per_chunk          : Dword;
         sample_description_index : Dword;
        end;

Var

{$A4}
    stsc : Record
     entry_count  : Dword;
     buffer       : ^stsc_entry;    // Pointer to some preallocated memory
     buffer_Size  : Dword;
     end;

Function Get_Sample_From_File(dest : pointer; MaxSize,Sample_Index : Dword) : integer;
Var Err : Integer = 0;
    Size,Offset,indx,BytesRead,Res : Dword;
    i,idx,chunk : Integer;
begin
 // Get Sample using stco and stsz information

 // Check Sample_Index for validity
 If Sample_Index = 0 then Exit(-1); // exit with error


 // Get chunk from stsc
  if stsc.entry_count < 1 then Error(' STSC Entry_count should not be zero ....');
  i := 0; chunk := (stsc.Buffer+i)^.first_chunk; idx := 0;                                  <----- ERROR line
  while idx < Sample_index do
   begin
     indx := idx;
    inc(idx,((stsc.Buffer + i)^.samples_per_chunk));
     if idx >= Sample_index then break;
      inc(chunk);

     if i < stsc.entry_count then
      begin // are there more entries ? if so
       if (stsc.Buffer+i+1)^.first_chunk = chunk then inc(i);
      end;
   end;

  ................................................

To big of a program to get you something you can compile and try but this is where the first(  and
all listed errors are the same) occurs.

Operation "+" not supported for types "^.stsc_entry" and LongInt

Tried to change it to Dword but with same result. It feels like they have changed how you are
allowed to addres indexes in dynamic arrays...


// hope this clear things up, otherwise I can try to reproduce this error with some small piece of code so you can try and compile it. I continue to work with 4.2.1 until I know how much I have to change in my code.

 

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Pointers
« Reply #3 on: August 20, 2014, 09:36:58 pm »
I use Delphi compatible setting target win32 for i386, pentium 4, mostly standard settings.

Actually, Delphi settings are not standard settings.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Pointers
« Reply #4 on: August 20, 2014, 09:40:50 pm »
I did a quick test.
Only {$mode delphi} gives that error message while in {$mode objfpc} code compiles well.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

macrillo

  • New Member
  • *
  • Posts: 18
Re: Pointers
« Reply #5 on: August 20, 2014, 10:18:44 pm »
Marcov , I Started to list the changes and there was not that many. I don't know why I use delphi compatible but there was some DirectX library code that would'nt compile without it a couple of years ago so its just been there. I started with BP 7 so it's syntax seemed ok. And you can look at the win SDK and More or less just translate the C-calls straight off.

Anyway, I tried Switching to ObjFpc but I get 'Unexpected end of file'
Both in 2.4.1 and 2.6.4 .... Do I have a serious underlying problem or do you find anything
wrong  in using pointers as i do ? ( exept using delphi compatible mode, or is that a 2.4.1 issue
that allowed me to do so, should I use objFpc?).

Thanks for your patience, usually I try until something compiles and stick with that way ....  :D

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Pointers
« Reply #6 on: August 21, 2014, 03:36:06 am »
Try this:
Code: [Select]
chunk := pointer(ptruint(stsc.Buffer)+i)^.first_chunk;

Do you have reference of the original C code we could look at? It just felt like parts of the code don't make sense, or difficult to understand.
« Last Edit: August 21, 2014, 03:49:33 am by User137 »

macrillo

  • New Member
  • *
  • Posts: 18
Re: Pointers
« Reply #7 on: August 21, 2014, 10:42:27 pm »
Hi all again, It feels like we are getting off topic a bit ....

The code snippet I supplied may seem strange but it is taken out of context.
It shows where I got an error and some surrounding code and definitions as requested.

I don't have any C-code that I've 'translated', that was just a reason I mentioned wy I choosed
Delphi compatible so many years ago. ( I Think it was some DirectX headers I downloaded  that would't compile without it and my own code didn't seem to care so I went along with it.)
What's best or most practical is not the issue here,though any thougths is appreciated for future use  :)

This code Do compile and work under 2.4.1 but Not in 2.6.4 !

My original question was wy I'm not allowed to do this pointer arithmetics anymore,
I't might be strange but I find it useful when you know what you're doing ( You know what
Type/Record/ etc. resides ( or should reside  :) ) at that memory location. There might be a
better way of doing this but I find this way easy enough. I'ts the best way I've come up with
interpreting C-calls with olny a pointer as input, there you can typecast in the function declaration
and later access Arrays/Records as you please( It seems, Pascal is much moore strict but this is how I have dealt with it).


I will Supply some broken out code ,fpc configfile used and a dump of compiler messages using
the same cfg file only alternating the Mode.

Code: [Select]

Type
        stsc_entry = Record
         first_chunk              : Dword;
         samples_per_chunk        : Dword;
         sample_description_index : Dword;
        end;




Var

{$A4}

    stsc : Record
     entry_count  : Dword;
     buffer       : ^stsc_entry;
     buffer_Size  : Dword;
     end;

     SomeMemory : Array[0..1023] of byte;


Function Get_Sample_From_File(dest : pointer; MaxSize,Sample_Index : Dword) : integer;
Var
    Size,Offset,indx,BytesRead,Res : Dword;
    i,idx,chunk : integer;
begin
 // Get Sample using stco and stsz information

 // Get chunk from stsc

  i := 0; idx := 0;
  chunk := (stsc.Buffer+i)^.first_chunk;

  while idx < Sample_index do
   begin
     indx := idx;
    inc(idx,((stsc.Buffer + i)^.samples_per_chunk));
     if idx >= Sample_index then break;
      inc(chunk);

     if i < stsc.entry_count then
      begin // are there more entries ? if so
       if (stsc.Buffer+i+1)^.first_chunk = chunk then inc(i);
      end;
   end;
end;



Begin
 // initialize
  stsc.entry_count := 1;
  stsc.Buffer      := @SomeMemory[20];
  stsc.Buffer_Size := 1023;

 Get_Sample_From_File(@SomeMemory[0],1023,5);
end.

Code: [Select]

// -----------------------------------------------------------------------------------------------------------
// New Compiler

e:\FPC\2.6.4\bin\i386-win32>fpc -iD -iW
2014/03/06 2.6.4

e:\FPC\2.6.4\bin\i386-win32>fpc e:\fpc\chrille\test\compilertest.pas
Target OS: Win32 for i386
Compiling e:\fpc\chrille\test\compilertest.pas
compilertest.pas(26,10) Warning: Function result does not seem to be set
compilertest.pas(28,5) Note: Local variable "Size" not used
compilertest.pas(28,10) Note: Local variable "Offset" not used
compilertest.pas(28,17) Note: Local variable "indx" is assigned but never used
compilertest.pas(28,22) Note: Local variable "BytesRead" not used
compilertest.pas(28,32) Note: Local variable "Res" not used
Linking E:\FPC\EXE\compilertest.exe
61 lines compiled, 0.9 sec , 35392 bytes code, 1836 bytes data
1 warning(s) issued
5 note(s) issued

e:\FPC\2.6.4\bin\i386-win32>fpc e:\fpc\chrille\test\compilertest.pas
Target OS: Win32 for i386
Compiling e:\fpc\chrille\test\compilertest.pas
compilertest.pas(36,24) Error: Operation "+" not supported for types ".^stsc_ent
ry" and "LongInt"
compilertest.pas(41,27) Error: Operation "+" not supported for types ".^stsc_ent
ry" and "LongInt"
compilertest.pas(41,5) Error: Incompatible types: got "untyped" expected "LongIn
t"
compilertest.pas(47,23) Error: Operation "+" not supported for types ".^stsc_ent
ry" and "LongInt"
compilertest.pas(62) Fatal: There were 4 errors compiling module, stopping
Fatal: Compilation aborted
Error: e:\FPC\2.6.4\bin\i386-win32\ppc386.exe returned an error exitcode (normal
 if you did not specify a source file to be compiled)


// -----------------------------------------------------------------------------------------------------------
// Old Compiler


e:\FPC\2.4.1\bin\i386-win32>fpc -iD -iW
2010/01/21 2.4.1-r1:14761

e:\FPC\2.4.1\bin\i386-win32>fpc e:\fpc\chrille\test\compilertest.pas
Target OS: Win32 for i386
Compiling e:\fpc\chrille\test\compilertest.pas
compilertest.pas(26,10) Warning: Function result does not seem to be set
compilertest.pas(28,5) Note: Local variable "Size" not used
compilertest.pas(28,10) Note: Local variable "Offset" not used
compilertest.pas(28,17) Note: Local variable "indx" is assigned but never used
compilertest.pas(28,22) Note: Local variable "BytesRead" not used
compilertest.pas(28,32) Note: Local variable "Res" not used
Linking E:\FPC\EXE\compilertest.exe
61 lines compiled, 0.10 sec , 35872 bytes code, 1744 bytes data
1 warning(s) issued
5 note(s) issued

e:\FPC\2.4.1\bin\i386-win32>fpc e:\fpc\chrille\test\compilertest.pas
Target OS: Win32 for i386
Compiling e:\fpc\chrille\test\compilertest.pas
compilertest.pas(26,10) Warning: Function result does not seem to be set
compilertest.pas(28,5) Note: Local variable "Size" not used
compilertest.pas(28,10) Note: Local variable "Offset" not used
compilertest.pas(28,17) Note: Local variable "indx" is assigned but never used
compilertest.pas(28,22) Note: Local variable "BytesRead" not used
compilertest.pas(28,32) Note: Local variable "Res" not used
Linking E:\FPC\EXE\compilertest.exe
61 lines compiled, 0.10 sec , 35872 bytes code, 1744 bytes data
1 warning(s) issued
5 note(s) issued


// -------------------------------------------------------------------------------------------------------------
// Config file used in all compilations (fpc.cfg) with -M switch altered

 -TWin32
# -Mobjfpc
 -Mdelphi
 -vw
 -vi
 -vn
 -Sg
 -Si
 -Sc
 -Ct
 -Ci
 -Co
# -Cg
 -CX
 -Ooregvar
 -O1
 -O2
 -CpPENTIUM4
 -OpPENTIUM4
 -Rintel
 -FEE:\FPC\EXE
 -FUE:\FPC\PPU
 -gl
 -p-
 -b-
Hope for suggestions/explanations

User137 : It did pass the first test when I tried typecasting to ptruint but still complains
abaout illegal qualifier .... :(

macrillo

  • New Member
  • *
  • Posts: 18
Re: Pointers
« Reply #8 on: August 21, 2014, 11:06:49 pm »
Forgot  :D
As the testcode compiles in objfpc mode, I HAVE tried to compile the original code
in that mode also .... 

I got unexpected end of file error ... Haven't  even started to investigate, suppose you
are not allowed to use {$define xx}{$ifdef xx}{$endif} or {$MACRO on} in objfpc mode,
otherwise I don't see how I can get a unexpected end of file error if there is no code alternating switches ...

macrillo

  • New Member
  • *
  • Posts: 18
Re: Pointers
« Reply #9 on: August 22, 2014, 12:55:35 am »
If anyone is interested in my 'weird' or 'difficult to understand' code
this is my interpretation of section 8.18.2 of 'stsc' in
ISO/IEC 14496-12:2005(E) (ISO base media file format)
Not using ObjectOriented programming which sertanly would have helped ....

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Pointers
« Reply #10 on: August 22, 2014, 01:52:10 am »
Sorry, it apparently helps by testing and not guessing  :P  This compiles in delphi mode

Code: [Select]
type
  stsc_entry = Record
    first_chunk                      : Dword;
    samples_per_chunk          : Dword;
    sample_description_index : Dword;
  end;
  Pstsc_entry = ^stsc_entry;       

  chunk := Pstsc_entry(ptruint(stsc.Buffer)+i)^.first_chunk;
Because pointer type doesn't recognize member  .first_chunk, you need to do real typecast. Then again adding value to pointer is done as int value, not as pointer. You can convert them to eachother.

macrillo

  • New Member
  • *
  • Posts: 18
Re: Pointers
« Reply #11 on: August 22, 2014, 02:44:23 am »
Thank you User 137.

I tried your approach on my example file and it compiled ...  :)
The only downsize is that this means even more typecasting then what was needed
before  I found out that my earlier method worked ....  :(

I haven't yet looked into why my code ( whitch the snippet comes from ) doesn't compile
using Objfpc whitch seems to allow how I access Pointer memory.

This must be a straitening of the rules by the compiler over the years and I shouldn't
have been able to use Pointers this way Before ( To be complient with DELPHI ).
As Iv'e said Before I choosed Delphi complience to make some code compile several years ago
and have not seen any reason to change compiler mode since.

The way I used ( and apparently is OK using Objfpc mode ) is the most 'natural' and 'obvious/easy to read/understand)' way of using a (pointer(base) + index) into an Array of some type or an arbitrary memory location containing a Record of some type ( some type beeing specified by the pointer(base) in this case), at least to me.

Again, thank you  User 137,
but i Think I will try to make my code compile using Objfpc instead of inserting all of the typecasts involved and see where that leads me...

 

TinyPortal © 2005-2018