JCLL
JCLL

Reputation: 5555

Pointer dereference in VHDL

I have not been able to understand how to dereference a pointer in VHDL.

What I have in mind is a C code like :

  int a;
  int* ptr_a;

  a = 42;
  ptr_a=&a;
  *ptr_a=451;// how can I do this ?

I tried to mimick this code in VHDL :

ptr_test : process
    type     ptr_integer is access integer;
    variable a     : integer;
    variable ptr_a : ptr_integer;
  begin
    a         := 42;
    ptr_a     := new integer'(a);
    report "ptr now points to a : ptr=" & str(ptr_a.all);
    ptr_a.all := 451;
    report "ptr modified        : ptr=" & str(ptr_a.all);
    report "a   is NOT modified : a  =" & str(a);
    wait;
  end process;

So how can I correctly modify a value through a pointer ?

Upvotes: 4

Views: 6213

Answers (2)

Martin Thompson
Martin Thompson

Reputation: 16822

You can't directly. Access types are not "just like pointers" - they are to at least some extent distinct types of data storage.

This line does not create a pointer to a:

ptr_a     := new integer'(a);

It creates a data object with the same value as a and sets up ptr_a to reference it.

If you were to create another access type variable :

variable ptr_b : ptr_integer;

and set it to to point to ptr_a:

ptr_b := ptr_a;

then changes to ptr_b.all will reflect in ptr_a.all.

Upvotes: 7

user1818839
user1818839

Reputation:

"new" is the equivalent of the (C++ rather than C) "new" operation; invoking a constructor allocating an integer on the heap and initialising it to "a". (Naturally, you can "deallocate" it when done)

What you are looking for is ptr_a := a'access; which is the Ada way of accessing a global or local (stack) variable via a pointer : this is only legal if said variable has been declared "aliased" alerting the compiler to the fact there may be more than one view of it (and thus, preventing some nice optimisations). In C, everything is "aliased" whether you want it or not.

This is one of the aspects of Ada that didn't make it through the simplification process into VHDL : and it's difficult to see a good use for it. So there isn't an exact equivalent in VHDL.

Martin's answer just popped up : as he says, you CAN have 2 or more pointers to the same heap object.

Alternatively, explain what you are trying to achieve this way; there may be a VHDL alternative way of doing it.

Upvotes: 3

Related Questions