reading strings with different lengths

Discussion in 'VHDL' started by ALuPin@web.de, Sep 25, 2008.

  1. Guest

    Hi,

    I have the following problem:

    In a text file "vectors.txt" the following string is declared:
    "640"

    When reading the text file with a process I have to declare a string
    as follows:


    p_ReadFile : process
    file F : text open read_mode is "vectors.txt";
    variable L : line;
    variable resu : string (1 to 5);
    begin
    readline(F, L);
    read(L, resu);
    end process p_ReadFile;

    Now I want the variable "resu" to store different strings, for example
    "640", "1600", "10000".
    How do I constraint the string array type so that I can read different
    string lengths ?

    Thank you for your opinion.

    Rgds,
    Andre
    , Sep 25, 2008
    #1
    1. Advertising

  2. Guest

    Hi Alan,

    thank you for your solution !

    Rgds
    , Sep 25, 2008
    #2
    1. Advertising

  3. Guest

    On Sep 25, 1:50 pm, Alan Fitch <> wrote:

    > impure function getString(FILE F : text) return string
    > variable L : LINE;
    > begin
    > readline(F, L);
    > return L.all;
    > end;


    A question for Alan (I normally sit opposite him in the office,
    but today we're a few hundred miles apart!) and anyone else:
    Does this function represent a memory leak? You've populated
    variable L with some text, causing it to be allocated, but then
    you've returned from the function without deallocating L.
    A sufficiently smart compiler could detect that the contents
    of L are sure to be unreachable after the function returns,
    and then could deallocate it automatically - but is that
    mandated by the LRM? I fear not, but I'd be delighted
    if someone could reassure me that it's OK.

    Explicitly deallocating L in such a function is somewhat
    tricky, and is <ahem> left as an exercise for the reader :)
    --
    Jonathan Bromley
    , Sep 25, 2008
    #3
  4. a écrit :
    > On Sep 25, 1:50 pm, Alan Fitch <> wrote:
    >
    >> impure function getString(FILE F : text) return string
    >> variable L : LINE;
    >> begin
    >> readline(F, L);
    >> return L.all;
    >> end;

    >

    [...]
    > Does this function represent a memory leak? You've populated
    > variable L with some text, causing it to be allocated, but then
    > you've returned from the function without deallocating L.


    I think you're right there.


    > A sufficiently smart compiler could detect that the contents
    > of L are sure to be unreachable after the function returns,
    > and then could deallocate it automatically - but is that
    > mandated by the LRM? I fear not, but I'd be delighted
    > if someone could reassure me that it's OK.


    I wouldn't trust the smartness of the compiler. And I don't know the LRM
    well enough (must have it somewhere...)


    > Explicitly deallocating L in such a function is somewhat
    > tricky, and is <ahem> left as an exercise for the reader :)


    Certainly tricky. I wouldn't try this at home...
    As a workaround (or would you call this cheating ?), instead of creating
    a function I'd just use "L.all" instead of "read(L, constrainedstring)"

    Nicolas
    Nicolas Matringe, Sep 25, 2008
    #4
  5. Hi Jonathan

    writes:

    > A sufficiently smart compiler could detect that the contents
    > of L are sure to be unreachable after the function returns,
    > and then could deallocate it automatically - but is that
    > mandated by the LRM?


    Doesn't seem to be mandated but it surely is allowed as far as I can
    see. IEEE 1076-2002, 7.3.6 says:

    "In the absence of explicit deallocation, an implementation must
    guarantee that any object created by the evaluation of an allocator
    remains allocated for as long as this object or one of its subelements
    is accessible directly or indirectly; that is, as long as it can be
    denoted by some name."

    Regards
    Marcus

    --
    note that "property" can also be used as syntaxtic sugar to reference
    a property, breaking the clean design of verilog; [...]

    (seen on http://www.veripool.com/verilog-mode_news.html)
    Marcus Harnisch, Sep 26, 2008
    #5
  6. Guest

    On Sep 26, 11:44 am, Marcus Harnisch <> wrote:

    > writes:
    > > A sufficiently smart compiler could detect that the contents
    > > of L are sure to be unreachable after the function returns,
    > > and then could deallocate it automatically - but is that
    > > mandated by the LRM?

    >
    > Doesn't seem to be mandated but it surely is allowed as far as I can
    > see. IEEE 1076-2002, 7.3.6 says:
    >
    > "In the absence of explicit deallocation, an implementation must
    > guarantee that any object created by the evaluation of an allocator
    > remains allocated for as long as this object or one of its subelements
    > is accessible directly or indirectly; that is, as long as it can be
    > denoted by some name."


    Thanks for doing my LRM dirty work for me :)

    The snag is that it's non-trivial for a compiler to determine whether
    it's safe to deallocate after an access variable has gone out of
    scope.
    In Alan's example it is definitely OK, but even then you must do
    fairly sophisticated lifetime analysis to prove that not only does
    the variable go out of scope, but also there are definitely no
    other references to the allocated string. So we have no written
    guarantee that the memory occupied by L.all will be reclaimed.

    For a string of a few dozen characters, allocated a few hundred
    times during a simulation, this obviously doesn't much matter.
    But I also like to use access types to give me other kinds of
    variable-sized array; suppose I create some megapixel video
    fields, and fail to deallocate them? This is an unpleasant
    hole in the LRM; I would very much want to do the deallocation
    by hand, to be safe.

    Thanks
    --
    Jonathan
    , Sep 27, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Taras_96

    Variable 'variable lengths'

    Taras_96, May 8, 2005, in forum: VHDL
    Replies:
    2
    Views:
    489
    Taras_96
    May 9, 2005
  2. Replies:
    7
    Views:
    514
    Malcolm
    May 15, 2005
  3. jt
    Replies:
    3
    Views:
    910
    Keith Thompson
    May 23, 2005
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    732
    Malcolm
    Jun 24, 2006
  5. Replies:
    4
    Views:
    352
    hdante
    Jun 22, 2006
Loading...

Share This Page