gethostnameby_r crashes


 
Thread Tools Search this Thread
Operating Systems Linux gethostnameby_r crashes
# 1  
Old 06-23-2008
gethostnameby_r crashes

Hello all,
I'm trying to use gethostbyname_r function with 6 arguments in one of my functions. But the call to this crashes the program. Kindly help me in resolving this...

Compiler Info:
Linux target: i686-hardhat-linux
version: 3.3.1


The following is the piece of code I'm trying to compile:
Code:
#include <iostream.h>
#include <netdb.h>
#include <arpa/inet.h>
int main()
{
hostent *host, **host1;
in_addr **addr_ptr;
char hostname[255];
char buff[1024];
int err; 
gethostname(hostname,255);
cout<<"hostname="<<hostname<<"\n";
gethostbyname_r(hostname,host,buff,sizeof(buff),host1,&err);
cout<<"err="<<err<<"\n";
addr_ptr=(struct in_addr**)host->h_addr_list;
char* mgcip=new char[16];
mgcip=inet_ntoa(**addr_ptr);
cout<<"mgcip="<<mgcip<<"\n";
}

The following is the output I get on running this:
$a.out
hostname=oamce-1
Segmentation fault (core dumped)


The following is the result of running gdb over the core file:
Code:
$gdb a.out core
GNU gdb 6.0 (MontaVista 6.0-11.0.15.0500398 2005-03-19)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-hardhat-linux"...
Core was generated by `a.out'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /usr/lib/libstdc++.so.5...done.
Loaded symbols for /usr/lib/libstdc++.so.5
Reading symbols from /lib/libm.so.6...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Reading symbols from /lib/libnss_files.so.2...done.
Loaded symbols for /lib/libnss_files.so.2
#0  0x40251345 in internal_getent (result=0x804895c, buffer=0xbfffcb50 "\177", buflen=8192,
    errnop=0x40243be0, herrnop=0xbfffcb4c, af=2, flags=0) at nss_files/files-hosts.c:77
77      nss_files/files-hosts.c: No such file or directory.
        in nss_files/files-hosts.c
(gdb) bt
#0  0x40251345 in internal_getent (result=0x804895c, buffer=0xbfffcb50 "\177", buflen=8192,
    errnop=0x40243be0, herrnop=0xbfffcb4c, af=2, flags=0) at nss_files/files-hosts.c:77
#1  0x402517d5 in _nss_files_gethostbyname_r (name=0xbfffeb50 "oamce-1", result=0x804895c,
    buffer=0xbfffcb50 "\177", buflen=8192, errnop=0x40243be0, herrnop=0xbfffcb4c)
    at nss_files/files-hosts.c:268
#2  0x402066c8 in __gethostbyname_r (name=0xbfffeb50 "oamce-1", resbuf=0x804895c,
    buffer=0xbfffcb50 "\177", buflen=8192, result=0xbfffec58, h_errnop=0xbfffcb4c)
    at getXXbyYY_r.c:219
#3  0x0804882d in main () at 1.cc:13
Current language:  auto; currently c
(gdb)

Also I'm not sure what should be the size of buff passed to the function. Based on what is the size to be determined?

Thank you in advance for the kind help.


Regards,
Rajangam

Last edited by vino; 06-23-2008 at 04:03 AM.. Reason: added code tags
# 2  
Old 08-25-2008
problem resolved

The error is found. The problem is due to passing of unallocated pointer(host) to the function. Thank you all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Apache2 Crashes

The Apache server suddenly stops. I am running Debian Jessie Here are some diagnostics: root@meow:/var/www# apachectl configtest AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress... (4 Replies)
Discussion started by: Meow613
4 Replies

2. UNIX and Linux Applications

Apcupsd crashes

I am trying to run apcupsd, but it will not retain contact the UPS root@meow:/home/ethan/UPS/gapcmon-0.8.9# /etc/init.d/apcupsd start Starting UPS power management: A copy of the daemon is still running. If you just stopped it, please wait about 5 seconds for it to shut down.... (0 Replies)
Discussion started by: Meow613
0 Replies

3. Shell Programming and Scripting

Perl/TK script crashes in MainLoop

I am running a perl/tk function that suddenly crashes in MainLoop. The gui just disappears and an X error is thrown when the code tries to update the non-existent GUI. Any suggestions to debug the root cause which destroys the mw gui? -D- <block name> : <block_name> - 1 -D- row_column_id... (0 Replies)
Discussion started by: bob.pepple
0 Replies

4. Programming

C++ program crashes

Hi, Can anyone tell me why the below program is crashing? and where exactly it is crashing. What is the corrective measure to be taken to make it work fine? #include <iostream> #include <cstring> using namespace std; class CString { char* m_data; public: CString() :... (6 Replies)
Discussion started by: royalibrahim
6 Replies

5. Red Hat

7z crashes system

Can someone tell my why every time I try to use 7z it freezes my system? I can't move my mouse, I can't type, I can't kill my xsession. I then restart my system and everything returns to normal. When I try to use 7z my system again freezes. (11 Replies)
Discussion started by: cokedude
11 Replies

6. Solaris

Server crashes when not in use.

Production server crashes when there is no traffic on it. Can only recover by going to ALOM and reboot the server. This seems to happen about the same time every month. The only good thing is the server is not in production at the time of the crash. I have been unable to locate any information in... (5 Replies)
Discussion started by: Joeentech
5 Replies

7. SuSE

Chromium flashes and crashes

I am running openSUSE 11.2 with KDE4.5 on my eMachines e525. I just did a "zypper up" on my system and it reported that Chromium was going to be updated. I agreed and the when the update was finished I shut down Chromium and started it again. It flickered on the screen for a moment and crashed.... (6 Replies)
Discussion started by: Druonysus
6 Replies

8. Programming

Tomcat crashes after Xvfb shutdown

Hello, I'm running tomcat on linux and using Xvfb as a X server for creating the pdf's (gui components). I'm running the Xvfb in a different proccess than the tomcat. If the Xvfb is shutdown and no pdf was yet created then everything is ok. but if i create a pdf and then shutdown the Xvfb... (0 Replies)
Discussion started by: omerc85
0 Replies

9. Programming

C++ program crashes

Hi, Could anyone tell me the reason why the following program crashes? class A { int x; public: A() { cout << "from A()" << endl; } ~A() { cout << "from ~A()" << endl; } }; class B : public A { public: B() { cout << "from B()"... (2 Replies)
Discussion started by: royalibrahim
2 Replies

10. Solaris

application Crashes on memset ?? any suggestions

Hi All, we have an application that is written in 'C' programming to connects to various servers in the organization. The bellow code establish a TCP connection to connect to the remote servers. the application works perfectly ok, but, after some time the entire process get's crashed and... (2 Replies)
Discussion started by: sudharma
2 Replies
Login or Register to Ask a Question