473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Testing if char *string was declared

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
Nov 13 '05 #1
6 4648
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
Nov 13 '05 #2
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
Nov 13 '05 #3
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
Nov 13 '05 #4
In 'comp.lang.c', Andrej Prsa <an*********@guest.arnes.si> 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- em**********@noos.fr [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/
Nov 13 '05 #5
In 'comp.lang.c', Andrej Prsa <an*********@guest.arnes.si> 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- em**********@noos.fr [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/
Nov 13 '05 #6
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 : bi****@eton.powernet.co.uk
CLINT v1.0b is available from http://www.rjgh.co.uk
(Stage 1 of) Stretchy Strings!!!

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: TechNovice | last post by:
Hi: I'm trying to find a way to test input values. To test an integer I tried this code: ******Code****** int input_number; cin>>input_number; while(!input_number) cout<<"invalid...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
10
by: yogeshmk | last post by:
I need to break a longer string (with strtok()) and store the smaller strings in an array. since the number of small strings is not fixed/known, I cannot use a declaration like char *format, so I...
6
by: cman | last post by:
If you have the following string variable declared: char *current; What does the following test in a while-loop amount to: While (!*current) Does this check if *current is "not empty" i.e...
18
by: william | last post by:
below is a short piece of code I wrote to testify my understanding of char *, and array. #include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str);
10
by: Neil | last post by:
I am working on a code that is supposed to return a string to my main function from the test function. The cout in test returns "My Name", but the cout in main returns some junk characters. Can...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.