Testing if char *string was declared

A

Andrej Prsa

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
 
P

pete

Andrej said:
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.
 
A

Andrej Prsa

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
 
P

pete

Andrej said:
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.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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.
 
R

Richard Heathfield

Andrej said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top