Memory Alloc/Dealloc query

B

B. Gandhi

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
 
M

Martin Ambuhl

B. Gandhi said:
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.
 
A

Al Bowers

Martin said:
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.
 
B

Bigdakine

Subject: Memory Alloc/Dealloc query
From: (e-mail address removed) (B. Gandhi)
Date: 9/22/03 1:30 PM Hawaiian Standard Time
Message-id: <[email protected]>

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
 
M

Micah Cowan

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
 
L

LibraryUser

B. Gandhi said:
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.
 
P

Peter Shaggy Haywood

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.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top