|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In article <8d225f92-babf-4eac-b233-d8d8a5d18a6a@s12g2000prg.googlegroups.com>,
Joakim Hove <joakim.hove@gmail.com> wrote: >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() ) -- This is a Usenet signature block. Please do not quote it when replying to one of my postings. http://en.wikipedia.org/wiki/Signature_block |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> 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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Tue, 29 Jan 2008 05:56:28 +0000 (UTC), roberson@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote in comp.lang.c: > In article <8d225f92-babf-4eac-b233-d8d8a5d18a6a@s12g2000prg.googlegroups.com>, > Joakim Hove <joakim.hove@gmail.com> wrote: > > >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 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Jack Klein <jackklein@spamcop.net> writes:
> On Tue, 29 Jan 2008 05:56:28 +0000 (UTC), roberson@ibd.nrc-cnrc.gc.ca > (Walter Roberson) wrote in comp.lang.c: [...] >> 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). -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Jack Klein wrote:
> > On Tue, 29 Jan 2008 05:56:28 +0000 (UTC), roberson@ibd.nrc-cnrc.gc.ca > (Walter Roberson) wrote in comp.lang.c: > > > In article <8d225f92-babf-4eac-b233-d8d8a5d18a6a@s12g2000prg.googlegroups.com>, > > Joakim Hove <joakim.hove@gmail.com> wrote: > > > > >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 > > 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." (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:ThisIsASpamTrap@gmail.com> |
|
![]() |
| Outils de la discussion | |
|
|