LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2004, 04:42 PM   #1
Wszebor
LQ Newbie
 
Registered: Nov 2004
Location: Cracow, Poland
Distribution: Fedora Core 2
Posts: 2

Rep: Reputation: 0
Question gethostbyname, getprotobyname, getservbyname...


Functions "getprotobyname()", "getservbyname()" and other similar are returning a pointer to a structure. But who allocates the memory for these? I suppose it is done by these functions. Question is: have I to free returned pointers?

If these functions allocate memory for these structures then after some hundreds of calls i'll have no memory?

Perhaps there is a one static place for these structures and when I call functions next time, the same place for structure is used (this overwrites old data in structure?).

Please help me.
 
Old 11-11-2004, 06:57 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
According to man gethostbyname:
Quote:
The functions gethostbyname() and gethostbyaddr() may
return pointers to static data, which may be overwritten
by later calls. Copying the struct hostent does not suf_
fice, since it contains pointers - a deep copy is
required.
Which means that they don't necessarily allocate memory. They have a hostent struct declared as static which makes its value valid after the function has returned. Your assumption about overwriting previous data with subsequent calls is correct. You may also want to look at gethostbyname_r() which is a reentrant variation of it.

Last edited by itsme86; 11-11-2004 at 06:58 PM.
 
Old 11-12-2004, 06:04 AM   #3
Wszebor
LQ Newbie
 
Registered: Nov 2004
Location: Cracow, Poland
Distribution: Fedora Core 2
Posts: 2

Original Poster
Rep: Reputation: 0
Smile

Thx - I missed this fragment in man. Now I understand.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the gethostbyname command mdlister Linux - Newbie 5 03-14-2005 07:50 AM
gethostbyname in script? farry Linux - Newbie 2 03-14-2005 02:14 AM
gethostbyname() error jnusa Programming 0 01-12-2005 04:56 AM
how to recover when getprotobyname("tcp") fails? BrianK Programming 2 10-25-2002 01:37 PM
gethostbyname error for aimstr8 Linux - Security 7 03-09-2002 04:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration