PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > What happens to this memory
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
What happens to this memory

Réponse
 
LinkBack Outils de la discussion
Vieux 29/01/2008, 05h50   #1
Joakim Hove
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut What happens to this memory

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


  Réponse avec citation
Vieux 29/01/2008, 05h56   #2
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What happens to this memory

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
  Réponse avec citation
Vieux 29/01/2008, 05h59   #3
Joakim Hove
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What happens to this memory


> 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
  Réponse avec citation
Vieux 29/01/2008, 21h32   #4
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What happens to this memory

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
  Réponse avec citation
Vieux 29/01/2008, 23h13   #5
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What happens to this memory

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"
  Réponse avec citation
Vieux 30/01/2008, 15h13   #6
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What happens to this memory

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>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 21h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17369 seconds with 14 queries