What happens to this memory

Discussion in 'C Programming' started by Joakim Hove, Jan 29, 2008.

  1. Joakim Hove

    Joakim Hove Guest

    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
    Joakim Hove, Jan 29, 2008
    #1
    1. Advertising

  2. In article <>,
    Joakim Hove <> 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
    Walter Roberson, Jan 29, 2008
    #2
    1. Advertising

  3. Joakim Hove

    Joakim Hove Guest


    > 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
    Joakim Hove, Jan 29, 2008
    #3
  4. Joakim Hove

    Jack Klein Guest

    On Tue, 29 Jan 2008 05:56:28 +0000 (UTC), -cnrc.gc.ca
    (Walter Roberson) wrote in comp.lang.c:

    > In article <>,
    > Joakim Hove <> 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
    Jack Klein, Jan 29, 2008
    #4
  5. Jack Klein <> writes:
    > On Tue, 29 Jan 2008 05:56:28 +0000 (UTC), -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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 29, 2008
    #5
  6. Jack Klein wrote:
    >
    > On Tue, 29 Jan 2008 05:56:28 +0000 (UTC), -cnrc.gc.ca
    > (Walter Roberson) wrote in comp.lang.c:
    >
    > > In article <>,
    > > Joakim Hove <> 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:>
    Kenneth Brody, Jan 30, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    4,154
  2. Replies:
    0
    Views:
    426
  3. PCHOME

    What happens if I do not free memory....

    PCHOME, Apr 7, 2005, in forum: C Programming
    Replies:
    3
    Views:
    359
    Mark McIntyre
    Apr 11, 2005
  4. Replies:
    4
    Views:
    300
    Kenneth Brody
    Aug 9, 2006
  5. NM
    Replies:
    6
    Views:
    452
    Default User
    Sep 20, 2006
Loading...

Share This Page