Is this leagal?

O

Owen Zhang

Somebody told me this morning that the following is leagal.

char *a = "It's a test";

The memory is automatically allocated. Is this correct?
 
J

jacob navia

Owen said:
Somebody told me this morning that the following is leagal.

char *a = "It's a test";

The memory is automatically allocated. Is this correct?

Yes. The pointer will point to the start of a
zero terminated character string.

The compiler generates and allocates storage for that string.
 
W

Walter Roberson

Somebody told me this morning that the following is leagal.
char *a = "It's a test";
The memory is automatically allocated. Is this correct?

It is correct that the storage for the literal will be allocated
by the compiler, without you needing you malloc() the string
or use something like

char X[12] = "It's a test";

But to clarify a way that your question could be read:
The memory that is allocated for the string literal in that code,
char *X = "It's a test";
will be "permanent" memory, in the sense that it will last for
the lifetime of the program, just as if you had used a "static"
object. The memory that will be used for the string literal
will *not* have "automatic storage" duration. So for example,

char *teststring(void) { char *X = "It's a test"; return X; }

This is allowed, and you can access the returned string after
the function returned. This constrasts with

char *teststring(void) { char X[12] = "It's a test"; return X; /* WRONG */ }

because the X in that routine would have automatic storage duration
and would become inaccessible once the routine returned.


Another important thing to watch out for is that when you do
use the string literal form,

char *X = "It's a test";

then the string pointed to is not necessarily writable; if you
were to command, for example, X[7] = 'T'; then the C standard
says the result is up to the implementation: the implementation
is allowed to store string literals in read-only memory.

Furthermore, if you had

char *teststring2(void) { char *Y = "a test"; return Y; }

then the implementation would be allowed to return Y as pointing
into the same memory as being used to hold "It's a test" .

So... *only* use that kind of assignment when you want "read-only"
access; if you need to write to the string, then use either

char X[12] = "It's a test"; OR
char X[] = "It's a test";
The second of these does the same as the first excepts saves you
from having to count the number of characters. (Remember to
include the hidden trailing 0 that every string has.)
 
K

Keith Thompson

Owen Zhang said:
Somebody told me this morning that the following is leagal.

char *a = "It's a test";

The memory is automatically allocated. Is this correct?

Yes, but be careful. Tedious details follow.

This will cause 'a' to point to a statically allocated character array
containing the string "It's a test" terminated by a null character.
The phrase "statically allocated" means that this array will exist
throughout the lifetime of your program.

The phrase "automatically allocated" actually has a specific technical
meaning in C. Object (variables) with "automatic storage duration"
are local to a function or block, and cease to exist on exit from the
function or block. These are also referred to as "locally allocated"
or, not entirely correctly, as "stack allocated".

So yes, the memory is "automatically" allocated in the sense that the
allocation is handled for you by the compiler, but not in the
specialized sense of the word used by the C standard.

Another thing to watch out for is that you should never attempt to
modify a string literal. For example, you might think that this:

a[0] = 'i';

would change the string to "it's a test", but in fact it's an error.
Worse, it's a kind of error called "undefined behavior", which means
that the compiler isn't required to warn you about it; your program
might crash, or it might even do just what you expect. To avoid this
error, you should declare it as:

const char *a = "It's a test";

so any attempt to modify the string will (probably) be caught by the
compiler.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Sections 4
(Pointers), 6 (Arrays and Pointers) and 8 (Characters and Strings)
are relevant to your question.

I'll bet you weren't expecting such a complicated answer to such a
simple question!
 
K

Keith Thompson

Chris Thomasson said:
[...]
To avoid this
error, you should declare it as:

const char *a = "It's a test";
[...]

or:

char const a[] = "It's a test";

If you make it an array, you don't need to make it const (though of
course you can).
 

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,777
Messages
2,569,604
Members
45,235
Latest member
Top Crypto Podcasts_

Latest Threads

Top