[newbie] intern ??

Discussion in 'C Programming' started by Walter Roberson, Aug 24, 2006.

  1. In article <44eda61f$0$346$>,
    Olivier <> wrote:

    > Is there a way to specify a identifier from a string?
    >String would be "aux1", identifier would be aux1?
    > I fear that no, but ...


    As you feared, the answer is NO.

    You would not want to do this without constraints anyhow, as
    the user could deliberately choose one of your variables
    that are important for controlling the program, or could choose
    one of the variables of the implementation. You would effectively
    need to check the string against all the variable names that
    you want to be allowed -- and if you are going through that trouble
    it is little more work to actually code the variable instead of
    hoping to be able to "interpret" it.

    if (strcmp(varname,"aux1") == 0) readout = aux1
    else if (strcmp(varname,"beta2") == 0) readout = beta2

    and so on
    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, Aug 24, 2006
    #1
    1. Advertising

  2. Re: intern ??

    Olivier wrote:
    > Dear all,
    >
    > Is there a way to specify a identifier from a string?
    > String would be "aux1", identifier would be aux1?
    > I fear that no, but ...
    > A.O.


    Not in standard C. But there are a few options outside the pale:

    (1) Generate a link map. Many compilers spit out the global variables
    as named relocatable symbols, often listed in the link map. From the
    map you may be able to figure out the absolute address of the variable.

    (2) Make your own name to address table, with name and address pairs.

    (3) Have the comp[iler generate "debug info". Then pore thru the
    STABS records lookin for the identifier. Doable but tricky.

    (4) Use RTTI if your compiler has that option.
     
    Ancient_Hacker, Aug 24, 2006
    #2
    1. Advertising

  3. Re: intern ??

    Olivier wrote:
    > Dear all,
    >
    > Is there a way to specify a identifier from a string?
    > String would be "aux1", identifier would be aux1?
    > I fear that no, but ...
    > A.O.


    I think the question is what you are really trying to do?

    This unchecked code fragment may do what you want...

    int *theVariable = NULL;
    int bar = 0;
    int bas = 0;

    if (strcmp(identifier, "bar") == 0) {
    theVariable = &bar;
    }
    else if (strcmp(identifier("bas") == 0) {
    theVariable = &bas;
    }
    else {
    /* deal with error */
    }

    Or maybe having some scheme where you register variables by name in
    some hash table and can look them up by name and get back a pointer to
    the desired variable will satisfy your requirements? Or are you
    looking for some built in language facility?

    -David
     
    David Resnick, Aug 24, 2006
    #3
  4. Walter Roberson

    Olivier Guest

    Dear all,

    Is there a way to specify a identifier from a string?
    String would be "aux1", identifier would be aux1?
    I fear that no, but ...
    A.O.
     
    Olivier, Aug 24, 2006
    #4
  5. Walter Roberson

    Olivier Guest

    Re: intern ??

    Ok, thanks to all---
    What I was trying to do was that:
    while developping a program, I get more and more
    variables, which I regularly want to check, in case
    anything gets broke. That's not really debugger work,
    just one step above. I can update my table of
    on-demand-printable variables as you suggested
    (with by the way a fairly easy to update script).
    My idea was to ask the special user which variable
    s/he wanted to see, recover the name as a string,
    say "ItsDog", from it reconstruct the real name
    which is Yuri->ItsDog and displayed it via
    Write_ItsDog, since all my variables have
    fixed routine-name for display.
    I'll update my table, that's not too hard!
    Thanks again! A.O.
     
    Olivier, Aug 24, 2006
    #5
  6. Re: intern ??

    On 24 Aug 2006 06:23:52 -0700, "Ancient_Hacker" <>
    wrote:

    >
    > Olivier wrote:
    > > Dear all,
    > >
    > > Is there a way to specify a identifier from a string?
    > > String would be "aux1", identifier would be aux1?
    > > I fear that no, but ...
    > > A.O.

    >
    > Not in standard C. But there are a few options outside the pale:
    >
    > (1) Generate a link map. Many compilers spit out the global variables
    > as named relocatable symbols, often listed in the link map. From the
    > map you may be able to figure out the absolute address of the variable.
    >

    For what I consider to be the usual meaning of 'global', which in C is
    more precisely '(symbol) with external linkage', I would say any
    implementation that does linking pretty much has to use relocatable
    linker symbols for these to obtain the required functionality, and I
    know of none that doesn't. (The standard doesn't actually require
    separate compilation and linking, or indeed even actual compilation.)

    Some, IME most, implementations also have some kind of linker symbols
    (often 'local' or 'hidden', else mangled) for C entities with internal
    linkage, i.e. declared with 'static'. But there is more variation over
    whether these appear in the link map. If any.

    > (2) Make your own name to address table, with name and address pairs.
    >

    This is of course the only completely portable method.

    > (3) Have the comp[iler generate "debug info". Then pore thru the
    > STABS records lookin for the identifier. Doable but tricky.
    >

    Or on many targets, pore through the linker symbols which are retained
    in the linked executable file. Same result as #1.

    > (4) Use RTTI if your compiler has that option.


    I know of no C compiler that has RTTI. C++ compilers do -- but they
    provide data including the name of the _type_ not of the variable(s).

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Sep 4, 2006
    #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. Recruit Interns

    Verification Intern Positions Available

    Recruit Interns, Aug 14, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,218
    Recruit Interns
    Aug 14, 2003
  2. Roedy Green

    Intern failure

    Roedy Green, Apr 6, 2004, in forum: Java
    Replies:
    2
    Views:
    374
    David Zimmerman
    Apr 7, 2004
  3. Robert Mischke
    Replies:
    3
    Views:
    1,539
    Tony Morris
    May 19, 2005
  4. Replies:
    5
    Views:
    1,100
    Dimitri Maziuk
    Mar 22, 2006
  5. Replies:
    9
    Views:
    487
    Owen Jacobson
    Oct 27, 2007
Loading...

Share This Page