gethostbyname() ---- Delete Resulting Pointer?

A

aj

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?

struct hostent *lHostInfo;
lHostInfo = gethostbyname(ipHost.c_str());
memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list[0], lHostInfo-
h_length);
delete lHostInfo;

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

fred.l.kleinschmidt

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?

struct hostent *lHostInfo;
lHostInfo = gethostbyname(ipHost.c_str());
memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list[0], lHostInfo->h_length);

delete lHostInfo;

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

There is no 'new' or 'delete' in the C language.

gethostbyname is not defined by the C standard.
 
L

Lew Pitcher

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?

struct hostent *lHostInfo;
lHostInfo = gethostbyname(ipHost.c_str());
memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list[0], lHostInfo->h_length);

delete lHostInfo;

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

Sorry, but C does not have
- a keyword called "delete",
- a keyword called "new"
- a standard function called gethostbyname()
- a standard struture called hostent

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++

HTH
 
A

aj

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?
struct hostent *lHostInfo;
lHostInfo = gethostbyname(ipHost.c_str());
memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list[0], lHostInfo->h_length);
delete lHostInfo;
I am under the impression that you don't delete anything unless you
personally "new'ed" it. Is this theory correct in this situation?

Sorry, but C does not have
- a keyword called "delete",
- a keyword called "new"
- a standard function called gethostbyname()
- a standard struture called hostent

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++

HTH

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

Thanks for the non-help.
 
B

Ben Bacarisse

aj said:
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?

You are doubly off-topic. delete in C++, not C, and gethostbyname is
a *nix function. If you want to post somewhere, I'd choose
comp.unix.programmer, but man gethostbyname would be quicker (the
delete is wrong, BTW).
 
L

Lew Pitcher

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?
struct hostent *lHostInfo;
lHostInfo = gethostbyname(ipHost.c_str());
memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list[0], lHostInfo->h_length);
delete lHostInfo;
I am under the impression that you don't delete anything unless you
personally "new'ed" it. Is this theory correct in this situation?
Sorry, but C does not have
- a keyword called "delete",
- a keyword called "new"
- a standard function called gethostbyname()
- a standard struture called hostent
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.

You're welcome.

After all, what else is comp.lang.C here for, but to *not* discuss
COBOL, JAVA, C++, .NET, C#, how to tune your car, oriental cooking,
and observance of the Jewish faith?
 
J

Joachim Schmitz

aj said:
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?

struct hostent *lHostInfo;
lHostInfo = gethostbyname(ipHost.c_str());
memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list[0], lHostInfo-
h_length);
delete lHostInfo;

I am under the impression that you don't delete anything unless you
personally "new'ed" it. Is this theory correct in this situation?
for gethostbyname() ask in comp.unix.programmer, it's not topical here

for new/delete ask in comp.lang.c++ as neither exists in C, although I think
that a delete without a corresponding new won't work.

Bye, Jojo
 
J

Joachim Schmitz

aj said:
....
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

Bye, Jojo
 
A

aj

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

Bye, Jojo

My question wasn't about "delete" or "new". My question was if I am
responsible for deallocating the pointer provided by a certain
function. 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.
 
F

Flash Gordon

aj wrote, On 06/12/07 18:36:

Firstly gethostbyname is not part of standard C so questions about it
would be better directed to a group dedicated to your platform such as
comp.unix.programmer, but before you do that you should at least do some
basic research, such as reading the man page for the function.
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?

struct hostent *lHostInfo;
lHostInfo = gethostbyname(ipHost.c_str());
memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list[0], lHostInfo-
h_length);
delete lHostInfo;

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

Secondly delete C++ and not C. You need to decide which language you are
using since there are major differences (delete being one of them) and
stick to it and not post questions to do with C++ here.
 
L

Lew Pitcher

My question wasn't about "delete" or "new". My question was if I am
responsible for deallocating the pointer provided by a certain
function. 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.

When you provided example code that could not be C (even though you
say that you compile it with a C compiler) and stated that
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 took the question out of the realm of the C language.

How can we correctly answer the (implied) question of "Is the theory
that you don't delete anything unless you personally 'new'ed it?" when
the C language does not recognize the concept of "new"ing anything,
nor of "delete"ing anything? You are asking a question (apparently)
about the C++ language, and the responsibility of a C++ programmer to
write correct C++ programs. That question we cannot answer.

Had you asked
"Is the theory that you don't free() anyting unless you've personally
malloc()ed (or calloc()ed or realloc()ed) it?"
then we could have answered, for your question would be answerable
within the confines of the C language and best-practices of C
programming.

Just so you know...
 
J

John Gordon

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

aj

When you provided example code that could not be C (even though you
say that you compile it with a C compiler) and stated that> I am under the impression that you don't delete anything unless you

you took the question out of the realm of the C language.

How can we correctly answer the (implied) question of "Is the theory
that you don't delete anything unless you personally 'new'ed it?" when
the C language does not recognize the concept of "new"ing anything,
nor of "delete"ing anything? You are asking a question (apparently)
about the C++ language, and the responsibility of a C++ programmer to
write correct C++ programs. That question we cannot answer.

Had you asked
"Is the theory that you don't free() anyting unless you've personally
malloc()ed (or calloc()ed or realloc()ed) it?"
then we could have answered, for your question would be answerable
within the confines of the C language and best-practices of C
programming.

Just so you know...- Hide quoted text -

- Show quoted text -

Lew,

I understand your point. You are right, the sample program I
provided is not pure C. I should have removed the "delete" call prior
to posting, and should have rephrased the question to be more C'ish.
That being said, I have come to conclusion that I am not responsible
for the resulting memory, and have removed the delete call.

Thanks,
AJ
 
A

aj

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 ;) !
 
F

Flash Gordon

aj wrote, On 06/12/07 19:59:
My question wasn't about "delete" or "new".

Your code used delete and your question mentioned them, and they are
enough to show that your code is NOT C code.
My question was if I am
responsible for deallocating the pointer provided by a certain
function. I wasn't aware that that particular function,
gethostbyname(), isn't in the C standard.

That is almost forgiveable, but basic research (i.e. reading the man
page since you mentioned this was a problem on Linux) would have shown
you that it is not standard C.
Regardless, it is still C
syntax, compiled by a C compiler.

Any C compiler when operating as a C compiler would have rejected your
code because it was not C code. As you were told new and delete are not
part of C.
I will save future (read: all)
questions for a more appropriate newsgroup. Sorry you felt the need
to plonk me.

Well, you've saved me from having to plonk you. Had I seen this post
earlier I would not have bothered with the post telling you the correct
group to ask in.
 
L

Lew Pitcher

Lew,

I understand your point. You are right, the sample program I
provided is not pure C. I should have removed the "delete" call prior
to posting, and should have rephrased the question to be more C'ish.
That being said, I have come to conclusion that I am not responsible
for the resulting memory, and have removed the delete call.

As a rule of thumb, you are responsible for deallocating any object
that you dynamically allocate through the malloc() tools. However, in
C (especially in many C standard and 3rd-party library functions, the
opposite is not true: if the dynamic object was allocated elsewhere,
it /may/ be your responsibility to free() it at the appropriate place.

Some functions return "dynamic" objects that are allocated as static
variables within the function. From the outside (the caller's POV)
there is no way to distinguish these objects from dynamic objects that
are allocated using malloc() within the called function. For
malloc()ed objects, the caller is often responsible for free()ing the
memory, but for static objects, the caller *must not* free() the
memory. You have to read the documentation on the function that you
are calling, and find out which is expected of you.

Think of this example...

char * dyn_A(void)
{
static char array[] = "Do not free()";
return array;
}

char * dyn_B(void)
{
return malloc(4);
}

void MyFunc(void)
{
char *objA, *objB;

objA = dyn_A();
objB = dyn_B();

/* OK, do I free() objA? How about objB? How does MyFunc() know?
*/

}
 
A

Al Balmer

That being said, I have come to conclusion that I am not responsible
for the resulting memory, and have removed the delete call.

Usually, it's not good or necessary to guess about this sort of thing.
The documentation for the function call should tell you whether it is
your responsibility to free the memory.

Another pitfall - some functions might provide a pointer to static
data, which will be overwritten on the next call. In this case, you
need to allocate memory yourself, copy the data, and free the memory
when you're done.
 
J

jameskuyper

aj said:
....
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 ;) !

Just because the stockboy works with food, would you go to him for
advice about how to grow a cucumber? He might happen to know, but
would you go out of your way to pose the question to the stockboy
rather than to a gardner or a farmer?

Just because we're C experts doesn't mean we know a lot about every
library written with a C interface. When you have a question about a
library which contains a function named gethostbyname(), you should
pose your questions in a forum devoted to that library.

Many of us have used that library; some are even familiar with that
particular function (not including me), but you'll get a lot more help
in the right forum than in this one.
 
D

Default User

Lew said:
Sorry, but C does not have
- a keyword called "delete",
- a keyword called "new"
- a standard function called gethostbyname()
- a standard struture called hostent
You probably want to discuss this topic in comp.lang.c++

As C++ is missing items 3 and 4, that seems unlikely.



Brian
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top