473,388 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Memory Alloc/Dealloc query

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
Nov 13 '05 #1
6 7962
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

Nov 13 '05 #2


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: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #3
>Subject: Memory Alloc/Dealloc query
From: b_********@rediffmail.com (B. Gandhi)
Date: 9/22/03 1:30 PM Hawaiian Standard Time
Message-id: <ad**************************@posting.google.com >

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

Nov 13 '05 #4
b_********@rediffmail.com (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
Nov 13 '05 #5
"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.
Nov 13 '05 #6
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.

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Philip Lawatsch | last post by:
Hi, I have some questions about whats written in http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14 (Describing some memory pool) #1 From what i understand this will also work for new...
4
by: hall | last post by:
Hi. I have some problems with a class i've written that allocates memory dynamicaly. I want to put these objects into a std::vector, but it does not work. My class looks (simplified) like this: ...
8
by: cody | last post by:
i basically want to create an object which contains an array (the last element of the class). the size of the array is determined when the object is created. for performance reasons (avoiding...
6
by: Rex_chaos | last post by:
I have allocated a array like double a; We know that a 1-D array just like an 1-D pointer. So I wonder that should I release the memory of the array myself like free(a); Thanks in...
1
by: John Hagstrand | last post by:
Hello, I'm getting the following error when I try to update or insert into a table. This just started happening today. The table has been working fine up to now. The table has 27,000 rows and...
5
by: Janning Vygen | last post by:
Hi, tonight my database got corruppted. before it worked fine. since two days i do the following tasks every night psql -c 'CLUSTER;' $DBNAME psql -c 'VACUUM FULL ANALYZE;' $DBNAME ...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
11
by: Grey Alien | last post by:
Any one know of an open source memory pool library?. I can't seem to find any implemented in C (many available in C++ e.g. Boost). Google is not turning up anything useful ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.