How to initialize a char*?

P

Peter

Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

------------
char* ptr;

func(..., ptr);
----------

How to properly initialize a char*? I used to use

char* ptr = "";

but was told this is not good.

Can anybody explain to me why, and what's a good way to initilize it?

Thanks,
Peter
 
C

Christopher Benson-Manica

Peter said:
char* ptr;
func(..., ptr);

This is bad - ptr is uninitialized (not even NULL), so passing it to a
function is either pointless or an invitation to disaster, a mistake
in any case.
How to properly initialize a char*? I used to use
char* ptr = "";

Depends on what you're doing with it. You probably were told that
because it should be

const char *ptr="";

Why? Because "" is a constant empty string; trying to modify it
invites disaster. Therefore, pointers to such string literals (so
they're called) should be declared const; that way if you try to
modify what it points to the compiler can inform you of your mistake.
Can anybody explain to me why, and what's a good way to initilize it?

Done. Unless you have a specific reason not to, though, you should be
using std::strings so you won't have to worry about the preceding
stuff.
 
J

Julie

Peter said:
Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

------------
char* ptr;

func(..., ptr);
----------

How to properly initialize a char*? I used to use

char* ptr = "";

but was told this is not good.

Can anybody explain to me why, and what's a good way to initilize it?

I'd suspect that it isn't recommended because it isn't really clear what ptr is
used for: it has a value pointing to a constant string that shouldn't be
modified, but can be.

When and where a pointer is necessary, you can initialize it as:

char* ptr = NULL;

Then, it is more obvious that the pointer has been initialized and points to
nothing, but that when it does point to something, the destination will be
modifiable.
 
M

Mike Wahler

Peter said:
Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

------------
char* ptr;

func(..., ptr);
----------

How to properly initialize a char*? I used to use

char* ptr = "";

but was told this is not good.

const char *ptr = "";

But this initializes 'ptr' with the address of
the first character of the string literal "".
The target of this pointer cannot be modified.

If you want to create an initialize a pointer whose
value you want to change later, just initialize it
to the address of some character, or to 0 (NULL).

char *ptr = 0;
Can anybody explain to me why, and what's a good way to initilize it?

It really depends upon what you're doing. As a general
rule, I try not to create an object until I have a meaningful
value available with which to initialize it. If no such
value is available, I'll 'default' initialize it, i.e. for a
pointer, with NULL.

Are you sure you wouldn't be better off using a std::string
object instead of a char pointer? std::string objects are
automatically initialized when created.

std::string s; /* initialized with empty string */

-Mike
 
B

Bill Seurer

Peter said:
Can anybody explain to me why, and what's a good way to initilize it?

In general the best way to initialize pointers is with null (that is, 0).
 
D

David Harmon

On Tue, 13 Apr 2004 11:19:38 -0700 in comp.lang.c++, "Peter"
So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

Uninitialized variables are evil, whether pointers or otherwise,
although uninitialized pointers are the worst. Never define a variable
without initializing it, without the most extreme reason to do so.

You should get rid of all your (char *) pointers and replace them by
using std::string instead. std::string initializes itself by default to
an empty string, and everybody is happy.
 
K

Kevin Goodsell

Peter said:
Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

There's absolutely no reason to do this (unless possibly if the function
expects a non-const reference). The fact that you think you want to
suggests that you don't understand pointers yet, and/or this does not do
what you expect.

Never use any variable before you've given it a meaningful value. Doing
so gives undefined behavior, and could crash your program or worse.
----------

How to properly initialize a char*? I used to use

char* ptr = "";

This uses a dangerous and deprecated language feature -- the implicit
conversion of a string literal to a (non-const) char*. I'd suggest
making your compiler warn about this if it provides such a warning.
but was told this is not good.

Can anybody explain to me why, and what's a good way to initilize it?

Initialize it with whatever you need. You're the only one who knows what
purpose it serves, so we can't suggest what to use. But leaving it
uninitialized is an error, and doesn't make sense.

Also, use std::string instead of a char* if you need a string.

-Kevin
 
J

Julie

Christopher said:
This is bad - ptr is uninitialized (not even NULL), so passing it to a
function is either pointless or an invitation to disaster, a mistake
in any case.

Not true.

Presume that func is declared as:

void func(int input, char * & dest);

Your statement may only be correct if func is declared as:

void func(int input, char * dest);

Since the OP didn't include the prototype, no assumption can be made about the
validity of passing in ptr.
 
C

Christopher Benson-Manica

Julie said:
Since the OP didn't include the prototype, no assumption can be made about the
validity of passing in ptr.

Well, okay - rats. Good call.
 
J

Julie

David said:
On Tue, 13 Apr 2004 11:19:38 -0700 in comp.lang.c++, "Peter"


Uninitialized variables are evil, whether pointers or otherwise,
although uninitialized pointers are the worst. Never define a variable
without initializing it, without the most extreme reason to do so.

Not true.

The (unintentional) misuse of uninitialized variables may lead to undefined
behavior.
You should get rid of all your (char *) pointers and replace them by
using std::string instead. std::string initializes itself by default to
an empty string, and everybody is happy.

Not true.

char *, std::string, or any other character container should be used
judiciously and appropriately, depending on the specific implementation needs.

std::string is generally the recommended choice for general-purpose character
string storage and handling.

Further, the OP didn't specify as to what the char * is used for -- either
pointing to an array of characters or a pointer to a single character. A char
* should be the type used when pointing to a single character (use of
std::string would not be appropriate).
 
D

David Harmon

On Tue, 13 Apr 2004 13:34:44 -0700 in comp.lang.c++, Julie
Presume that func is declared as:

void func(int input, char * & dest);

If so, it is an evil func. You can tell me which fatal error does it
have, buffer overflow, or thread-hostile static buffer, or dynamically
allocating memory that the caller will forget to free? It needs to be
rewritten to use std::string.
 
J

Julie

David said:
On Tue, 13 Apr 2004 13:34:44 -0700 in comp.lang.c++, Julie


If so, it is an evil func. You can tell me which fatal error does it
have, buffer overflow, or thread-hostile static buffer, or dynamically
allocating memory that the caller will forget to free? It needs to be
rewritten to use std::string.

You shouldn't be making blanket statement like this w/o knowing the OP's
intention.

Here is another example that is perfectly legitimate:

void func(char * const input, int word_number, char * & ptr_to_word);
// find word in input and store address in ptr

No allocations, no (thread-hostile???) static buffers, no overflow.
It needs to be rewritten to use std::string.

As I previously replied, char * is a perfectly legitimate and usable type.
Blanket statements such as "use std::string" serve no purpose.

The answer to the OP's question is simple and concise:

Initialize a char * w/ NULL.

All other sub-threads relating to the intention of the OP are conjecture w/o
further clarification and input from the OP.
 
M

Matt Wharton

Uninitialized variables are evil, whether pointers or otherwise,
Not true.

The (unintentional) misuse of uninitialized variables may lead to undefined
behavior.

This is true, but I think it would be fair to argue that it is always good
practice to initialize variables to something (most likely NULL or 0 in this
case). No harm comes from doing this (always initializing), whereas there
are many situations where NOT initializing will shoot you in the foot.

-Matt
 
J

Julie

Matt said:
This is true, but I think it would be fair to argue that it is always good
practice to initialize variables to something (most likely NULL or 0 in this
case). No harm comes from doing this (always initializing), whereas there
are many situations where NOT initializing will shoot you in the foot.

-Matt

Yes, my statement was directed at the 'evil' part -- there is nothing 'evil'
about it, it just has the potential to lead to undefined behavior.

Absolutely, variables, as a general rule, should be initialized to a
standardized default value.
 
I

Ioannis Vranos

Peter said:
Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

------------
char* ptr;

func(..., ptr);
----------

How to properly initialize a char*? I used to use

char* ptr = "";

but was told this is not good.

Can anybody explain to me why, and what's a good way to initilize it?


Usually people who initialize, initialize it to 0.

Initialization to some initial value is a "religious" matter, that is some
strongly like to do it while others don't. The truth is that in reality
initialization doesn't protect you.






Ioannis Vranos
 
I

Ioannis Vranos

Christopher Benson-Manica said:
This is bad - ptr is uninitialized (not even NULL), so passing it to a
function is either pointless or an invitation to disaster, a mistake
in any case.

Why?




Depends on what you're doing with it. You probably were told that
because it should be

const char *ptr="";


And what would be the use of that?


Why? Because "" is a constant empty string; trying to modify it
invites disaster. Therefore, pointers to such string literals (so
they're called) should be declared const; that way if you try to
modify what it points to the compiler can inform you of your mistake.


The above is entirely, completely useless.






Ioannis Vranos
 
I

Ioannis Vranos

Julie said:
Presume that func is declared as:

void func(int input, char * & dest);


You can't do that!


Your statement may only be correct if func is declared as:

void func(int input, char * dest);


I do not think his statement is correct.






Ioannis Vranos
 
I

Ioannis Vranos

Julie said:
Here is another example that is perfectly legitimate:

void func(char * const input, int word_number, char * & ptr_to_word);
// find word in input and store address in ptr


I do not think it is. You can take references from pointers only in
templates. Even in this case passing an unitialised char * would not have
any bad effect by its own.


No allocations, no (thread-hostile???) static buffers, no overflow.


As I previously replied, char * is a perfectly legitimate and usable type.
Blanket statements such as "use std::string" serve no purpose.

The answer to the OP's question is simple and concise:

Initialize a char * w/ NULL.


Nope. If he chooses to make the unneeded initialisation he should use 0, NOT
NULL.






Ioannis Vranos
 
D

David Harmon

On Tue, 13 Apr 2004 15:47:58 -0700 in comp.lang.c++, Julie
Yes, my statement was directed at the 'evil' part -- there is nothing 'evil'
about it, it just has the potential to lead to undefined behavior.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[6.14] What does the FAQ mean by "such-in-such is evil"?".
http://www.parashift.com/c++-faq-lite/
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top