Sign in Join

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

Mailing lists / fpc-pascal / 2005-11

Pointer

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Luis Del Aguila

Somebody can help me.
I do not understand, that I am making bad.
The program is:

Program ProbandoMemoria;
{$R+}
Var
  PLPunteros: ^Pointer;
  NuevoTamanio : integer;
  elementos : longint;
  x,b : ^integer;
Begin
  elementos := 3;
  NuevoTamanio:=Sizeof(Pointer)*elementos;
  ReallocMem(PlPunteros,NuevoTamanio);
  Writeln('Direccion : ',longint(Plpunteros)  );
  new(x);
  x^:=7;
  Plpunteros^:=x;
  b:=Plpunteros^;
  Writeln('Direccion : ',longint(b),' valor : ',b^);
  new(x);
  x^:=15;
  (Plpunteros+4)^:=x;
  b:=(Plpunteros+4)^;
  Writeln('Direccion : ',longint(b),' valor : ',b^);
  new(x);
  x^:=25;
  (Plpunteros+8)^:=x;
  b:=(Plpunteros+8)^;
  Writeln('Direccion : ',longint(b),' valor : ',b^);
//This I do not understand.
//Why ' b^ ' does not have the 7?
  b:=nil;
  b:=PlPunteros^;
  Writeln('Direccion : ',longint(b),' valor : ',b^);
  Readln;
End.

The result is:

Direccion : 604444
Direccion : 604460 valor : 7
Direccion : 604476 valor : 15
Direccion : 604492 valor : 25
Direccion : 604460 valor : 604476

Thanks.

Att.
Luis Del Aguila

Reply by email

Felipe Monteiro de Carvalho
to Luis Del Aguila
Luis Del Aguila wrote
Somebody can help me. I do not understand, that I am making bad. The program is:

What were you trying to do? and what was the result you expected?

What does that program do?

By the way your program is leaking memory. You call ReallocMem and New, but does not dispose of the allocated memory.

Felipe Monteiro de Carvalho

Reply by email

Jonas Maebe
to Luis Del Aguila: Somebody can help me. I do not understand, that I am making bad. The program is: Program…
Luis Del Aguila wrote 24 lines
Somebody can help me. I do not understand, that I am making bad. The program is: Program ProbandoMemoria; {$R+} Var PLPunteros: ^Pointer; NuevoTamanio : integer; elementos : longint; x,b : ^integer; Begin elementos := 3; NuevoTamanio:=Sizeof(Pointer)*elementos; ReallocMem(PlPunteros,NuevoTamanio); Writeln('Direccion : ',longint(Plpunteros) ); new(x); x^:=7; Plpunteros^:=x; b:=Plpunteros^; Writeln('Direccion : ',longint(b),' valor : ',b^); new(x); x^:=15; (Plpunteros+4)^:=x;

In this statement (and several next ones) you are writing past the memory allocated for Plpunteros. Adding a value to a pointer, increases the value of said pointer by this value times the size of the elements the pointer points to. Since Plpunteros = ^Pointer, the above writes X to the address "pointer(plpunteros)+4*sizeof(pointer)".

What you want is "(Plpunteros+1)^:=x;". Or, if you are in fpc or
objfpc mode, you can also use "plpunteros[1]:=x;"
Show quoted text 10 lines
b:=(Plpunteros+4)^; Writeln('Direccion : ',longint(b),' valor : ',b^); new(x); x^:=25; (Plpunteros+8)^:=x; b:=(Plpunteros+8)^; Writeln('Direccion : ',longint(b),' valor : ',b^); //This I do not understand. //Why ' b^ ' does not have the 7?

Probably because one of the previous out-of-bounds writes overwrote the value.

Jonas

Reply by email