Jump to content
milurt

memory paging or segmentation

Recommended Posts

1 hour ago, David Heffernan said:

There's really no significant difference here between C and Pascal. 

May be yes, there are some significant differences .... first of all the use of pointers directly ...... these means that the errors are no so clear.

 

If you use the normal array definition you get "Range check Error" instead of "Segmentation error"... isn't this enough?

 

And in general, unless there are speed or interfacing problems with particular APIs, why use direct memory management in PASCAL?

In all my life as a programmer I have no memory of having directly used GetMem in PASCAL, I have never felt the need (but I have programmed parts of code in assembler).
If I have to interface with "something" that can't be handled in Pascal, I create a DLL in "C" and work them with the "C" style.

But of course everyone is free to act as they wish, fortunately.
Mine was advice.

 

Bye

 

 

Share this post


Link to post
3 hours ago, DelphiUdIT said:

May be yes, there are some significant differences .... first of all the use of pointers directly ...... these means that the errors are no so clear.

 

If you use the normal array definition you get "Range check Error" instead of "Segmentation error"... isn't this enough?

 

And in general, unless there are speed or interfacing problems with particular APIs, why use direct memory management in PASCAL?

In all my life as a programmer I have no memory of having directly used GetMem in PASCAL, I have never felt the need (but I have programmed parts of code in assembler).
If I have to interface with "something" that can't be handled in Pascal, I create a DLL in "C" and work them with the "C" style.

But of course everyone is free to act as they wish, fortunately.
Mine was advice.

 

Bye

 

 

The pascal code in the question has pointer access and arithmetic 

Share this post


Link to post
21 hours ago, milurt said:

now compile it:

 

program Memorial;

  {$APPTYPE GUI}

  var ballpointer,step:pchar;
    a:NativeInt;

Begin
  step:=ptr(1);
  ballpointer:=GetMemory(3000000);
  for a:=1 to 3000000 do
  begin
    ballpointer^:=#45;
    ballpointer:=ballpointer+1;
  end;
end.

 

You are assuming Sizeof(Char) = 1, which is no longer true since Delphi 2007. Use

ballpointer:=GetMemory(3000000*Sizeof(char));

Incrementing a pointer to Char (PChar) will correctly take the size of Char into account, so your statement

 ballpointer:=ballpointer+1;

will increment the address held in ballpointer by 2. Since GetMemory takes the size in bytes your code allocates only half the memory needed for 3,000,000 UTF16 characters, so the loop runs over the end of the buffer when it is halfway through.

Share this post


Link to post

i see nowhere sizeof(char) in my program.

why is a +1 a +2?

for does not change it and getmemory is done for 1x3000000,

which was not so important, because i get the

runtime error at each segm:0000 and segm:0001 where

i write to not at 3000000+1

Share this post


Link to post
28 minutes ago, milurt said:

see nowhere sizeof(char) in my program.

ballpointer:=ballpointer+1; // +1 here is SizeOf

Most probably you're using a Unicoder version of Delphi where the compiler is aware that PChar is 2 bytes.

Share this post


Link to post
2 hours ago, milurt said:

i see nowhere sizeof(char) in my program.

why is a +1 a +2?

Because you have this declaration:

ballpointer: PChar;

PChar is an alias for PWideChar. WideChar is 2 bytes long. When pointer arithmetics is used, all operations take into account the real size of the data type the pointer points to. Thus, this statement:

ballpointer := ballpointer+1;

is in fact compiled and executed as

ballpointer := ballpointer + 1*SizeOf(WideChar);

and you have your pointer correctly pointing to the next WideChar in memory.

 

This is really easy to check with a simplest program:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  P: PChar;
begin
  GetMem(P, 20);
  WriteLn(Format('P = %8x', [NativeUInt(P)]));
  P := P + 1;
  WriteLn(Format('P = %8x', [NativeUInt(P)]));
  ReadLn;
end.

You'll see that second value printed is greater than the first by 2 in any modern Delphi version.

(Yes, I know there is a memory leak but this is just a demonstration).

Share this post


Link to post

 

On 3/11/2023 at 3:51 PM, David Heffernan said:

What is SizeOf(Char) on your system. If it is 2 then that would explain everything. 

 

Write a program that does 

 

Writeln(SizeOf(Char));

Share this post


Link to post
7 hours ago, milurt said:

no, suddenly,

but the problem was a half year long.

 

It's almost impossible to help you 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×