Testing if char *string was declared

Discussion in 'C Programming' started by Andrej Prsa, Jul 8, 2003.

  1. Andrej Prsa

    Andrej Prsa Guest

    Hi!

    Is there a way to reliably test whether a string was ever declared? E.g.

    char *str;

    test (str); /* -> no */
    str = strdup ("contents");
    test (str); /* -> yes */

    I managed to do it if I set char *str = NULL and then check whether str ==
    NULL in a test function, but I feel this is a workaround.

    Thanks,

    Andrej
    Andrej Prsa, Jul 8, 2003
    #1
    1. Advertising

  2. Andrej Prsa

    pete Guest

    Andrej Prsa wrote:
    >
    > Hi!
    >
    > Is there a way to reliably test whether
    > a string was ever declared? E.g.
    >
    > char *str;
    >
    > test (str); /* -> no */
    > str = strdup ("contents");
    > test (str); /* -> yes */
    >
    > I managed to do it if I set char
    > *str = NULL and then check whether str ==
    > NULL in a test function, but I feel this is a workaround.


    I don't understand why you would test str,
    immediatley after declaration.

    --
    pete
    pete, Jul 8, 2003
    #2
    1. Advertising

  3. Andrej Prsa

    Andrej Prsa Guest

    My apologies again, the example I wrote doesn't make sense; I just want to
    prevent successive strdup()s on a same char *str to create memory leaks.

    Best regards,

    Andrej
    Andrej Prsa, Jul 8, 2003
    #3
  4. Andrej Prsa

    pete Guest

    Andrej Prsa wrote:

    > My apologies again, the example I wrote doesn't make sense;
    > I just want to prevent successive strdup()s
    > on a same char *str to create memory leaks.


    After you're done using the value returned by strdup, free it.
    That's all you have to do.

    I don't think that strdup (from K&R2 chapter 6)
    is all that great of a function.

    char *strdup(char *s)
    {
    char *p;

    p = (char *) malloc(strlen(s)+1);
    if (p != NULL)
    strcpy(p, s);
    return p;
    }


    If it's not exactly what you want,
    then it might be simpler just to use strcpy instead,
    while managing your own destination allocation.

    --
    pete
    pete, Jul 8, 2003
    #4
  5. In 'comp.lang.c', Andrej Prsa <> wrote:

    > Is there a way to reliably test whether a string was ever declared? E.g.
    >
    > char *str;
    >
    > test (str); /* -> no */
    > str = strdup ("contents");
    > test (str); /* -> yes */
    >
    > I managed to do it if I set char *str = NULL and then check whether str ==
    > NULL in a test function, but I feel this is a workaround.


    AFAIK, there is no alternative. A local variable by itself is not
    initialized, and there is no way to know it.

    {
    char *str = NULL;

    test (str); /* -> no */
    str = strdup ("contents");
    test (str); /* -> yes */
    }

    I personally only do that when I can't do otherwise. I prefer to define and
    initialize when I can:

    {
    char *str = strdup ("contents");
    }

    but I can't each time.

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jul 8, 2003
    #5
  6. In 'comp.lang.c', Andrej Prsa <> wrote:

    > Sorry, I really didn't explain it well; I meant initialized, not declared.
    > I want to automate the strdup() function to something as:
    >
    > int my_strdup (char *in)
    > {
    > /* Check if char *in was ever malloc-ed (e.g. by strdup): */
    > int switch = my_check_function_which_i_am_not_sure_how_to_write (in);


    Note that the 'switch' keyword is part of the C-language.

    > if (switch == 0) in = strdup ("never initialized");
    > if (switch == 1)
    > {
    > free (in);
    > in = strdup ("newly initialized");
    > }
    > }


    I see. The idiomatic way is to test the pointer against NULL. You are not
    supposed to have uninitialized pointers in a program, because you would have
    no way to discriminate between a valid or an invalid value. (and magically,
    this is the problem you are facing to)

    This is why the pointers have a special value called NULL in C (or NIL or
    whatever in other languages) that clearly means

    "This pointer is invalid. Don't attempt to dereference it at all or you will
    burn in Hell."

    There are complicated alternatives with a structure, a flag or whatever, but
    the simple way is NULL.

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jul 8, 2003
    #6
  7. Andrej Prsa wrote:

    > Hi!
    >
    > Is there a way to reliably test whether a string was ever declared? E.g.
    >
    > char *str;
    >
    > test (str); /* -> no */


    Why not? str is declared (two lines above).

    > str = strdup ("contents");
    > test (str); /* -> yes */


    Clearly, you don't mean "whether a string was ever declared" but "whether a
    pointer was ever pointed to a valid string".

    The answer is "no", unless...

    >
    > I managed to do it if I set char *str = NULL and then check whether str ==
    > NULL in a test function, but I feel this is a workaround.


    It's not a workaround. I strive very hard to ensure that all my pointer
    values are only in an indeterminate state for the minimum possible length
    of time. If I don't have a good value to give them right away, I give them
    NULL to be going on with.

    I can therefore be confident that a pointer will have either the value NULL
    or a valid value. This is not a workaround, but a sensible modus operandi.

    --
    Richard Heathfield :
    CLINT v1.0b is available from http://www.rjgh.co.uk
    (Stage 1 of) Stretchy Strings!!!
    Richard Heathfield, Jul 8, 2003
    #7
    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. JohnZing

    declared or not declared ?

    JohnZing, Feb 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,571
    Jon Paal
    Feb 5, 2006
  2. wwj
    Replies:
    7
    Views:
    542
  3. wwj
    Replies:
    24
    Views:
    2,484
    Mike Wahler
    Nov 7, 2003
  4. Ben Pfaff
    Replies:
    5
    Views:
    463
    Tristan Miller
    Jan 17, 2004
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
Loading...

Share This Page