Memory Alloc/Dealloc query

Discussion in 'C Programming' started by B. Gandhi, Sep 23, 2003.

  1. B. Gandhi

    B. Gandhi Guest

    Hi friends

    I have a query regarding memory allocation deallocation in C.

    It is as follows:

    char *path = getenv("PATH");

    In this call to getenv(), where does the returned string stored?
    is it on heap or stack?

    and do I need to make a call like free(path) when there is
    no need for path anymore?

    Thanks in advance for ur help..
    Regards

    Bhavin
     
    B. Gandhi, Sep 23, 2003
    #1
    1. Advertising

  2. B. Gandhi wrote:

    > Hi friends
    >
    > I have a query regarding memory allocation deallocation in C.
    >
    > It is as follows:
    >
    > char *path = getenv("PATH");
    >
    > In this call to getenv(), where does the returned string stored?
    > is it on heap or stack?


    Who knows? Quite apart from the fact that the "heap" and "stack" are
    something specific to your implementation, all we know is that getenv()
    returns a pointer to a string, of which the standard tells us "The string
    pointed to shall not be modified by the program". The string may be in a
    system buffer, it may be in a static location inside the getenv() call.
    You don't need to know. All you need to know is that you shall not modify it.

    > and do I need to make a call like free(path) when there is
    > no need for path anymore?


    You did not allocate the space for the string; you do not own it; you shall
    not free it.




    --
    Martin Ambuhl
     
    Martin Ambuhl, Sep 23, 2003
    #2
    1. Advertising

  3. B. Gandhi

    Al Bowers Guest

    Martin Ambuhl wrote:
    > B. Gandhi wrote:
    >
    >> Hi friends
    >>
    >> I have a query regarding memory allocation deallocation in C.
    >>
    >> It is as follows:
    >>
    >> char *path = getenv("PATH");
    >>
    >> In this call to getenv(), where does the returned string stored?
    >> is it on heap or stack?

    >
    >
    > Who knows? Quite apart from the fact that the "heap" and "stack" are
    > something specific to your implementation, all we know is that getenv()
    > returns a pointer to a string, of which the standard tells us "The
    > string pointed to shall not be modified by the program". The string may
    > be in a system buffer, it may be in a static location inside the
    > getenv() call. You don't need to know. All you need to know is that you
    > shall not modify it.
    >


    Also, of note on the string returned by function getenv is that
    the string may be overwritten by a subsequent call to the getenv function.
    If the specified name, "PATH" in this case, cannot be matched in the
    environmental list provide by the host environment, a null pointer
    is returned.

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Sep 23, 2003
    #3
  4. B. Gandhi

    Bigdakine Guest

    >Subject: Memory Alloc/Dealloc query
    >From: (B. Gandhi)
    >Date: 9/22/03 1:30 PM Hawaiian Standard Time
    >Message-id: <>
    >
    >Hi friends
    >
    >I have a query regarding memory allocation deallocation in C.
    >
    >It is as follows:
    >
    >char *path = getenv("PATH");
    >
    >In this call to getenv(), where does the returned string stored?
    >is it on heap or stack?


    getenv returns a ptr to where the environment variable PATH is defined. You
    don't allocate for it, it is automatically allocated when the code is executed.
    It is not stack memory, because it is static.

    getenv doesn't allocate memory, it simply returns a pointer to where the string
    represented by PATH exists.

    >
    >and do I need to make a call like free(path) when there is
    >no need for path anymore?


    I don't think that would work since you didn't allocate it; it is static. And
    I'm not sure why you would want to, and if you could it would probably be
    dangerous to do so.

    Somehow I doubt the memory used by path is going to be the straw that breaks
    the camel's back.

    Stuart
     
    Bigdakine, Sep 23, 2003
    #4
  5. B. Gandhi

    Micah Cowan Guest

    (B. Gandhi) writes:

    > Hi friends
    >
    > I have a query regarding memory allocation deallocation in C.
    >
    > It is as follows:
    >
    > char *path = getenv("PATH");
    >
    > In this call to getenv(), where does the returned string stored?
    > is it on heap or stack?


    ISO C has no concept of heap or stack; moreover, the C standard places
    no constraints on just how the returned string was allocated. getenv()
    "owns" the memory, though: you should never free() it, nor should you
    attempt to modify the string.

    HTH,
    -Micah
     
    Micah Cowan, Sep 23, 2003
    #5
  6. B. Gandhi

    LibraryUser Guest

    "B. Gandhi" wrote:
    >
    > char *path = getenv("PATH");
    >
    > In this call to getenv(), where does the returned string stored?
    > is it on heap or stack?
    >
    > and do I need to make a call like free(path) when there is
    > no need for path anymore?


    The following quote (from N869) tells you all you know or need to
    know about the getenv function. Get a copy of the standard and
    look these things up yourself.

    7.20.4.4 The getenv function

    Synopsis
    [#1]

    #include <stdlib.h>
    char *getenv(const char *name);

    Description

    [#2] The getenv function searches an environment list,
    provided by the host environment, for a string that matches
    the string pointed to by name. The set of environment names
    and the method for altering the environment list are
    implementation-defined.

    [#3] The implementation shall behave as if no library
    function calls the getenv function.

    Returns

    [#4] The getenv function returns a pointer to a string
    associated with the matched list member. The string pointed
    to shall not be modified by the program, but may be
    overwritten by a subsequent call to the getenv function. If
    the specified name cannot be found, a null pointer is
    returned.

    --
    Replies should be to the newsgroup
    Chuck Falconer, on vacation.
     
    LibraryUser, Sep 23, 2003
    #6
  7. Groovy hepcat B. Gandhi was jivin' on 22 Sep 2003 16:30:34 -0700 in
    comp.lang.c.
    Memory Alloc/Dealloc query's a cool scene! Dig it!

    >char *path = getenv("PATH");
    >
    >In this call to getenv(), where does the returned string stored?
    >is it on heap or stack?


    Mu.

    >and do I need to make a call like free(path) when there is
    >no need for path anymore?


    No. To do so would cause undefined behaviour.
     
    Peter Shaggy Haywood, Sep 25, 2003
    #7
    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. Bob
    Replies:
    1
    Views:
    620
    .NET Follower
    Feb 13, 2004
  2. Vulcan Fire

    string vs const int memory alloc

    Vulcan Fire, Apr 12, 2005, in forum: C Programming
    Replies:
    6
    Views:
    650
    Vulcan Fire
    Apr 19, 2005
  3. Ryan Wang

    Memory alloc for a Function

    Ryan Wang, Nov 14, 2005, in forum: C Programming
    Replies:
    10
    Views:
    623
    Dik T. Winter
    Nov 15, 2005
  4. delete vs dealloc

    , Dec 20, 2006, in forum: C++
    Replies:
    3
    Views:
    1,481
    Daniel T.
    Dec 20, 2006
  5. Shen, Yu-Teh
    Replies:
    3
    Views:
    826
    Philip Semanchuk
    Jul 3, 2009
Loading...

Share This Page