Pointer clarification needed

T

Tricky

Guest
the following code works, and Im just making sure that it is garanteed
to work and Im not just hitting some luck that the pointer address has
remained constant this time only. Look at the following code:

process
type a_p_t is access integer;

variable ap1 : a_p_t;
variable ap2 : a_p_t;
begin
ap1 := new integer;
ap1.all := 1;

ap2 := ap1;

DEALLOCATE(ap1);

ap1 := new integer;
ap1.all := 5;

echo(integer'image(ap2.all) & LF);
wait;
end process;
......

I thought that ap2 would hold be a copy of the origional ap1,
therefore when new memory was alocated you couldnt garantee that ap2
would now point to the correct place, and in this case when I run it
Im just lucky ap1 doesnt change when a new integer is created?. or Is
it actually the case that the address will hold valid for ever, and
ap2 will now always point to the value inside ap1.

am I correct in thinking, that if I assign ap1 to something else, ap2
then wont work?
 
Tricky wrote:
the following code works, and Im just making sure that it is garanteed
to work and Im not just hitting some luck that the pointer address has
remained constant this time only. Look at the following code:

process
type a_p_t is access integer;

variable ap1 : a_p_t;
variable ap2 : a_p_t;
begin
ap1 := new integer;
ap1.all := 1;

ap2 := ap1;

DEALLOCATE(ap1);

ap1 := new integer;
ap1.all := 5;

echo(integer'image(ap2.all) & LF);
wait;
end process;
......

I thought that ap2 would hold be a copy of the origional ap1,
therefore when new memory was alocated you couldnt garantee that ap2
would now point to the correct place, and in this case when I run it
Im just lucky ap1 doesnt change when a new integer is created?. or Is
it actually the case that the address will hold valid for ever, and
ap2 will now always point to the value inside ap1.
Hi Tricky,
from the VHDL 2002 LRM p47

"NOTE—If an access value is copied to a second variable and is then
deallocated, the second variable is not set to null
and thus references invalid storage."

So there is no guarantee that ap2.all will work, as far as I can see.
I expect you have just been lucky.

am I correct in thinking, that if I assign ap1 to something else, ap2
then wont work?
No I don't think so. I would guess that in your case with your
particular simulator it may well carry on working - but that is just
luck. You could try it and see!

regards
Alan



--
Alan Fitch
Senior Consultant

Doulos – Developing Design Know-how
VHDL * Verilog * SystemVerilog * SystemC * PSL * Perl * Tcl/Tk * Project
Services

Doulos Ltd. Church Hatch, 22 Marketing Place, Ringwood, Hampshire, BH24
1AW, UK
Tel: + 44 (0)1425 471223 Email: alan.fitch@doulos.com
Fax: +44 (0)1425 471573 http://www.doulos.com

------------------------------------------------------------------------

This message may contain personal views which are not the views of
Doulos, unless specifically stated.
 
Tricky wrote:
On 9 Sep, 09:57, Tricky <trickyh...@gmail.com> wrote:
On 9 Sep, 09:47, Alan Fitch <alan.fi...@spamtrap.com> wrote:

snip
On further messing around, I have seen it break. I have also seen it
run fine once, and after a restart and run again, the simulator hangs
(I think because its trying to allocated already leaked memory, and it
doesnt like it).

This was all in modelsim. Looks like Im going to have to move into
pointers to pointers for what I need to do.

On this note, am I right in thinking you can only point to new memory,
not existing memory?
Therefore, pointers to pointers are impossible?
You cannot point to an object that hasn't been dynamically allocated.
There's no "address-of" operator like there is in C.

However you can create a dynamically allocated object that points to
another object that is dynamically allocated.

For instance

type ptrInt is access integer;

type ptrptrInt is access ptrInt;

Of course you must make sure you create everything in the correct order...

regards
Alan

--
Alan Fitch
Senior Consultant

Doulos – Developing Design Know-how
VHDL * Verilog * SystemVerilog * SystemC * PSL * Perl * Tcl/Tk * Project
Services

Doulos Ltd. Church Hatch, 22 Marketing Place, Ringwood, Hampshire, BH24
1AW, UK
Tel: + 44 (0)1425 471223 Email: alan.fitch@doulos.com
Fax: +44 (0)1425 471573 http://www.doulos.com

------------------------------------------------------------------------

This message may contain personal views which are not the views of
Doulos, unless specifically stated.
 
On 9 Sep, 09:31, Tricky <trickyh...@gmail.com> wrote:
the following code works, and Im just making sure that it is garanteed
to work and Im not just hitting some luck that the pointer address has
remained constant this time only. Look at the following code:

  process
     type a_p_t is access integer;

     variable ap1 : a_p_t;
     variable ap2 : a_p_t;
  begin
    ap1 := new integer;
    ap1.all := 1;

    ap2 := ap1;

    DEALLOCATE(ap1);

    ap1 := new integer;
    ap1.all := 5;

    echo(integer'image(ap2.all) & LF);
    wait;
  end process;
.....

I thought that ap2 would hold be a copy of the origional ap1,
therefore when new memory was alocated you couldnt garantee that ap2
would now point to the correct place, and in this case when I run it
Im just lucky ap1 doesnt change when a new integer is created?. or Is
it actually the case that the address will hold valid for ever, and
ap2 will now always point to the value inside ap1.

am I correct in thinking, that if I assign ap1 to something else, ap2
then wont work?
I think I may have just answered my own question - slightly modified
code outputs the value 10, and doesnt give a fatal error:

ap1 := new integer;
ap1.all := 1;

ap2 := ap1;

DEALLOCATE(ap1);

ap3 := new integer;
ap3.all := 10;

ap1 := ap3;
--ap1.all := 5;

echo(integer'image(ap2.all) & LF);
 
On 9 Sep, 09:47, Alan Fitch <alan.fi...@spamtrap.com> wrote:
Tricky wrote:
the following code works, and Im just making sure that it is garanteed
to work and Im not just hitting some luck that the pointer address has
remained constant this time only. Look at the following code:

  process
     type a_p_t is access integer;

     variable ap1 : a_p_t;
     variable ap2 : a_p_t;
  begin
    ap1 := new integer;
    ap1.all := 1;

    ap2 := ap1;

    DEALLOCATE(ap1);

    ap1 := new integer;
    ap1.all := 5;

    echo(integer'image(ap2.all) & LF);
    wait;
  end process;
......

I thought that ap2 would hold be a copy of the origional ap1,
therefore when new memory was alocated you couldnt garantee that ap2
would now point to the correct place, and in this case when I run it
Im just lucky ap1 doesnt change when a new integer is created?. or Is
it actually the case that the address will hold valid for ever, and
ap2 will now always point to the value inside ap1.

Hi Tricky,
   from the VHDL 2002 LRM p47

"NOTE—If an access value is copied to a second variable and is then
deallocated, the second variable is not set to null
and thus references invalid storage."

So there is no guarantee that ap2.all will work, as far as I can see.
I expect you have just been lucky.

am I correct in thinking, that if I assign ap1 to something else, ap2
then wont work?

No I don't think so. I would guess that in your case with your
particular simulator it may well carry on working - but that is just
luck. You could try it and see!

regards
Alan

--
Alan Fitch
Senior Consultant

Doulos – Developing Design Know-how
VHDL * Verilog * SystemVerilog * SystemC * PSL * Perl * Tcl/Tk * Project
Services

Doulos Ltd. Church Hatch, 22 Marketing Place, Ringwood, Hampshire, BH24
1AW, UK
Tel:  + 44 (0)1425 471223               Email: alan.fi...@doulos.com
Fax:  +44 (0)1425 471573                http://www.doulos.com

------------------------------------------------------------------------

This message may contain personal views which are not the views of
Doulos, unless specifically stated.
Thanks Alan.

On further messing around, I have seen it break. I have also seen it
run fine once, and after a restart and run again, the simulator hangs
(I think because its trying to allocated already leaked memory, and it
doesnt like it).

This was all in modelsim. Looks like Im going to have to move into
pointers to pointers for what I need to do.
 
On 9 Sep, 09:57, Tricky <trickyh...@gmail.com> wrote:
On 9 Sep, 09:47, Alan Fitch <alan.fi...@spamtrap.com> wrote:



Tricky wrote:
the following code works, and Im just making sure that it is garanteed
to work and Im not just hitting some luck that the pointer address has
remained constant this time only. Look at the following code:

  process
     type a_p_t is access integer;

     variable ap1 : a_p_t;
     variable ap2 : a_p_t;
  begin
    ap1 := new integer;
    ap1.all := 1;

    ap2 := ap1;

    DEALLOCATE(ap1);

    ap1 := new integer;
    ap1.all := 5;

    echo(integer'image(ap2.all) & LF);
    wait;
  end process;
......

I thought that ap2 would hold be a copy of the origional ap1,
therefore when new memory was alocated you couldnt garantee that ap2
would now point to the correct place, and in this case when I run it
Im just lucky ap1 doesnt change when a new integer is created?. or Is
it actually the case that the address will hold valid for ever, and
ap2 will now always point to the value inside ap1.

Hi Tricky,
   from the VHDL 2002 LRM p47

"NOTE—If an access value is copied to a second variable and is then
deallocated, the second variable is not set to null
and thus references invalid storage."

So there is no guarantee that ap2.all will work, as far as I can see.
I expect you have just been lucky.

am I correct in thinking, that if I assign ap1 to something else, ap2
then wont work?

No I don't think so. I would guess that in your case with your
particular simulator it may well carry on working - but that is just
luck. You could try it and see!

regards
Alan

--
Alan Fitch
Senior Consultant

Doulos – Developing Design Know-how
VHDL * Verilog * SystemVerilog * SystemC * PSL * Perl * Tcl/Tk * Project
Services

Doulos Ltd. Church Hatch, 22 Marketing Place, Ringwood, Hampshire, BH24
1AW, UK
Tel:  + 44 (0)1425 471223               Email: alan.fi....@doulos.com
Fax:  +44 (0)1425 471573                http://www.doulos.com

------------------------------------------------------------------------

This message may contain personal views which are not the views of
Doulos, unless specifically stated.

Thanks Alan.

On further messing around, I have seen it break. I have also seen it
run fine once, and after a restart and run again, the simulator hangs
(I think because its trying to allocated already leaked memory, and it
doesnt like it).

This was all in modelsim. Looks like Im going to have to move into
pointers to pointers for what I need to do.
On this note, am I right in thinking you can only point to new memory,
not existing memory?
Therefore, pointers to pointers are impossible?
 

Welcome to EDABoard.com

Sponsor

Back
Top