What happens to this memory

J

Joakim Hove

Hello,

I am considering to write a function like this:


enum shape_enum {circle , rectangle};

const char * shape_name(enum shape_enum shape_id) {
if (shape_id == circle)
return "Circle";
else if (shape_id == reactangle)
return "Rectangle";
end

printf("The name of the shape is: %s \n",shape_name(shape_id));

But I am wondering what happens to the memory allocated(??) for the
strings "Rectangle" and "Circle". Can I exhaust my available memory by
calling shape_name() sufficiently many times?

Regards

Joakim Hove
 
W

Walter Roberson

Joakim Hove said:
I am considering to write a function like this:
enum shape_enum {circle , rectangle};
const char * shape_name(enum shape_enum shape_id) {
if (shape_id == circle)
return "Circle";
else if (shape_id == reactangle)
return "Rectangle";
end
printf("The name of the shape is: %s \n",shape_name(shape_id));
But I am wondering what happens to the memory allocated(??) for the
strings "Rectangle" and "Circle". Can I exhaust my available memory by
calling shape_name() sufficiently many times?

That's a good question to be asking, and the answer is NO, you
cannot exhaust memory that way. Constant string literals such as
"Circle" are guaranteed to exist for the life of the program

(I would need to check the exact wording to figure out
if they still exist while processing a routine registed
via atexit() )
 
J

Joakim Hove

and the answer is NO, you
cannot exhaust memory that way. Constant string literals such as
"Circle" are guaranteed to exist for the life of the program

Thanks a lot - that was nice.

Joakim
 
J

Jack Klein

That's a good question to be asking, and the answer is NO, you
cannot exhaust memory that way. Constant string literals such as
"Circle" are guaranteed to exist for the life of the program

There are no such thing as "constant string literals" in C. The type
of a string literal is "array of char with static storage duration" in
C, and not "array of const char with static storage duration".
(I would need to check the exact wording to figure out
if they still exist while processing a routine registed
via atexit() )

To quote C99 on any static object "Its lifetime is the entire
execution of the program and its stored value is initialized only
once, prior to program startup."

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
K

Keith Thompson

Jack Klein said:
[...]
That's a good question to be asking, and the answer is NO, you
cannot exhaust memory that way. Constant string literals such as
"Circle" are guaranteed to exist for the life of the program

There are no such thing as "constant string literals" in C. The type
of a string literal is "array of char with static storage duration" in
C, and not "array of const char with static storage duration".
[...]

Yes, but keep in mind that, even though it's counterintuitive, "const"
and "constant" are quite different things. The "const" really means
read-only. For example, given:

int x = 42;
int *ptr = &x;
const int *const_ptr = &x;

const_ptr is a pointer to const int, so the int object can't be
modified via const_ptr, but it *can* be modified via x or *ptr.

A constant, on the other hand, is a lexical element such as an
integer, floating, enumeration, or character constant, and a constant
expression is an expression that can be evaluated during translation
(the definition is a bit more involved than that). What the language
calls a "string literal" could easily have been referred to as a
"string constant" if the authors of the standard had chosen that term.

But you're right that the standard uses the term "string literal", not
"constant string literal" or "string constant". It's also true that a
string literal is not const (though attempting to modify it still
invokes undefined behavior).
 
K

Kenneth Brody

Jack said:
There are no such thing as "constant string literals" in C. The type
of a string literal is "array of char with static storage duration" in
C, and not "array of const char with static storage duration".


To quote C99 on any static object "Its lifetime is the entire
execution of the program and its stored value is initialized only
once, prior to program startup."

(Sorry... I couldn't think of anything to snip without losing some
of the context.)

I think this boils down to:

The above code only "creates"[1] one copy of the strings "Circle"
and "Rectangle", and will return the same pointer on each call.
There is no memory leak, as this routine does not allocate any
new storage.

[1] Yes, I'm rather loose in my use of the word "creates". It is
possible, in fact, that the compilation unit which contains
this code has many references to "Circle" and "Rectangle", and
all of them will point to but a single copy. I believe that
this is allowed by the standard. In other words, given:

char *foo = "Circle";
char *bar = "Circle";

it is possible for foo and bar to point to the same address.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top