Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > gethostbyname() ---- Delete Resulting Pointer?

Reply
Thread Tools

gethostbyname() ---- Delete Resulting Pointer?

 
 
CBFalconer
Guest
Posts: n/a
 
      12-06-2007
aj wrote:
> Lew Pitcher <(E-Mail Removed)> wrote:
>> aj <(E-Mail Removed)> wrote:
>>
>>> I have the following snippet of code. On some platforms, the
>>> delete calls works, on Linux, it core dumps (memory dump) at
>>> the delete call. Am I responsible for deleting the memory that
>>> gethostbyname allocated?

>>

.... snip ...
>>
>> I suspect that you are writing C++ code (your mention of "new"
>> and "delete", and your use of method calls in your example code),
>> which is off-topic in comp.lang.c
>>
>> You probably want to discuss this topic in comp.lang.c++

>
> As usual, you guys are no help, just sticklers for details.
>
> Thanks for the non-help.


Congratulations. Your second post attained PLONK status. A new
record here.

PLONK

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      12-07-2007
>I have the following snippet of code. On some platforms, the delete
>calls works, on Linux, it core dumps (memory dump) at the delete
>call. Am I responsible for deleting the memory that gethostbyname
>allocated?


C does not have "new" or "delete".

*ANY* function that returns a pointer should include in its
documentation something to address this issue. Various possibilities
include (assuming the function doesn't return NULL):

(a) The pointer points into data that was passed in (e.g. strchr(),
strrchr(), etc.) You *MUST NOT* pass this pointer to free() since
the pointer may not be at the beginning of an allocated buffer,
even if it was allocated with malloc().

(a1) The pointer is a copy of a pointer passed in (e.g. fgets()).
If the original pointer was obtained from malloc(), you should
pass it to free() *once*, meaning you free either the pointer
passed in or the pointer returned, but *not* both.

(b) The pointer points into static (or maybe global) data that must
be copied before calling the function again if the value is
still needed. (e.g. localtime()) You *MUST NOT* pass this
pointer to free() since it was not malloc()ed in the first
place.

(b1) Some systems return pointers into "thread-specific storage",
and you should copy the data before calling the function again
if the value is still needed. You *MUST NOT* pass this pointer
to free() since it was not malloc()ed in the first place. The
only difference between this case and (b) is that in (b), the
function can't be thread-safe, and in this one, it might be.

(c) The function has an associated function to dispose of the pointer
returned. To avoid memory leaks, you should use this function
when you are through with the data. (e.g. fopen() returns a
pointer which should be passed to fclose()). Since there may
be more than one block of memory involved, and the pointer might
not point at the beginning of a block, you should use the
("destructor") function rather than calling free() to avoid
leaking memory or other resources.

(d) The function returns a pointer allocated by malloc(). When you
are through with the result, you should pass the pointer to
free(). (malloc() itself is an example of this. There's also
the infamous non-standard function strdup().) Functions like
this are the C analogy to C++ "constructors" where a simple
call to free() is adequate as a "destructor".

(e) The function returns a pointer allocated by malloc(), but it
still is keeping track of it. You should not pass this pointer
to free() since the data is still in use. (Some in-memory database
functions return pointers to retrieved records but you shouldn't
free() the records until you close the whole database, and the
close function is probably responsible for that.)

(f) The function returns a pointer that may or may not be allocated
with malloc(), points to static data, or points into data passed
into the function, with no obvious way to tell which. You can't
risk calling free() on the resulting pointer, and you should
fire the guy who writes code like this.

>I am under the impression that you don't delete anything unless you
>personally "new'ed" it. Is this theory correct in this situation?


You don't free() anything if it was not malloc()ed or it is still
in use. "personally" doesn't enter into it. It is common for a
function (C's analogy to a "constructor") to return a pointer to
data which the caller is responsible for dealing with (see cases
(c) and (d)) by calling either free() or a "destructor" function.
A function like this needs to be documented.

<off-topic>
On one particular system, gethostbyname() comes under (b1) above,
and on others it is either in (b) or (b1). Don't free the pointer
it returns.
</off-topic>

 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      12-07-2007
aj <(E-Mail Removed)> wrote:
# I have the following snippet of code. On some platforms, the delete
# calls works, on Linux, it core dumps (memory dump) at the delete
# call. Am I responsible for deleting the memory that gethostbyname
# allocated?

What does the documentation say?

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Death is the worry of the living. The dead, like myself,
only worry about decay and necrophiliacs.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-07-2007
"aj" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Dec 6, 2:39 pm, "Joachim Schmitz" <(E-Mail Removed)>
> wrote:
>> "aj" <(E-Mail Removed)> schrieb im
>> Newsbeitragnews:(E-Mail Removed)...>
>> On Dec 6, 2:14 pm, Lew Pitcher <(E-Mail Removed)> wrote:
>> >> On Dec 6, 1:36 pm, aj <(E-Mail Removed)> wrote:

>> ...
>> > As usual, you guys are no help, just sticklers for details.

>>
>> > Thanks for the non-help.

>>
>> And you believe that this statement helps in getting helped? I guess this
>> just helps in getting you plonked...
>>
>> You've been given all help there is, within topicalitiy of this group,
>> which
>> in this case it pointing you to the groups where you might find help

>
> My question wasn't about "delete" or "new". My question was if I am
> responsible for deallocating the pointer provided by a certain
> function.

And the answer to that is implementation/function specific, your
documentation should tell you about it. As it wasn't a Standard C function,
we can't really tell here
On one system I work on gethostbyname() is documented to return a value that
points to static data, so calling free() (or delete) won't work.

> I wasn't aware that that particular function,
> gethostbyname(), isn't in the C standard. Regardless, it is still C
> syntax, compiled by a C compiler. I will save future (read: all)
> questions for a more appropriate newsgroup. Sorry you felt the need
> to plonk me.

I didn't (still the proud owner of an entirely empty killfile), but others
might, just due to the tone of your reply to the help you've been given.

Bye, Jojo


 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      12-07-2007
aj wrote:
>
> On Dec 6, 3:25 pm, John Gordon <(E-Mail Removed)> wrote:
> > In <(E-Mail Removed)> aj <(E-Mail Removed)> writes:
> >
> > > As usual, you guys are no help, just sticklers for details.

> >
> > One would infer that when you have the flu, you go to your local grocery
> > store and ask a stockboy what to do about it, and are offended when he
> > suggests you ask a doctor.

>
> Acknowledged. Except you guys aren't stockboys. Stockboys don't know
> what to do about the flu. You hardened C types, however, have the
> knowledge to provide insight into my dilemna, so I dismiss your
> analogy !


Perhaps this wording is better?

One would infer that when you have a strange growth on your arm,
you go to your local medical building and ask a pediatrician what
to do about it, and are offended when he suggests you ask a
dermatologist or oncologist. ("You guys are no help, just
sticklers for details.")

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      12-09-2007
Gordon Burditt wrote:
>> Am I responsible for deleting the memory that gethostbyname
>> allocated?

>
> *ANY* function that returns a pointer should include in its
> documentation something to address this issue. Various possibilities
> include (assuming the function doesn't return NULL):
>
> (a) The pointer points into data that was passed in (e.g. strchr(),
> ... [several other categories of returned pointers]
> (f) The function returns a pointer that may or may not be allocated
> with malloc(), points to static data, or points into data passed
> into the function, with no obvious way to tell which. You can't
> risk calling free() on the resulting pointer, and you should
> fire the guy who writes code like this.


Thanks for the excellent response, Gordon.

--
Thad
 
Reply With Quote
 
Paul Sinnett
Guest
Posts: n/a
 
      12-09-2007
On Dec 7, 3:36 am, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
tango.fake.org> wrote:
> What does the documentation say?


It says you don't delete the memory. It also says the returned
structure may be overwritten by subsequent calls. And if you want to
make a copy it should be a deep copy because it contains pointers to
other static memory.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with decoding: resulting image is corrupt Bjorn Jensen Perl 2 03-22-2005 06:52 PM
How get resulting HTML of datagrid VB Programmer ASP .Net 3 08-04-2004 10:44 PM
SHA1 length of resulting hash Dil via .NET 247 ASP .Net 0 08-04-2004 12:53 PM
ASP Control names in resulting HTML Anatoli Trifonov ASP .Net 5 06-07-2004 05:36 PM
Setting a Range Parameter in a crystal report and streaming resulting report as a pdf James Wallace ASP .Net 0 10-17-2003 04:42 PM



Advertisments