Pointer

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Pointer

Luis Fernando Del Aguila Mejía
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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Pointer

Felipe Monteiro de Carvalho
On 11/15/05, Luis Del Aguila <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Pointer

Jonas Maebe-2
In reply to this post by Luis Fernando Del Aguila Mejía

On 15 nov 2005, at 17:11, Luis Del Aguila wrote:

> 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;"

>   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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal