By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Segmentation fault...

P: n/a
I don't know what I'm doing wrong ??

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
struct irc_server
{
//glowne dane
char *hostname;
int port;
char *username;
//kanaly
int number_of_channels;
char *channels[IRC_MAX_CHANNELS];
//socket
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;

};

int irc_init_server(struct irc_server *server, char *username, char
*hostname, int port)
{
//alokowanie pamieci glownej struktory
server = (struct irc_server*)malloc(sizeof(struct irc_server));
//wpisujemy podstawowe informacje
server->hostname = (char*)malloc(sizeof(char)*strlen(hostname));
strcpy(server->hostname, hostname);

server->username = (char*)malloc(sizeof(char)*strlen(username));
strcpy(server->username, username);

server->port=port;
//socket
if((server->sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
{
return 1;
}
//reszte informacji o serververze
server->server = gethostbyname(server->hostname);
if(server->server == NULL)
{
return 2;
}
server->serv_addr.sin_family = AF_INET;
server->serv_addr.sin_port = htons(server->port);
server->serv_addr.sin_addr = *((struct in_addr
*)server->server->h_addr);
memset(&(server->serv_addr.sin_zero), '\0', 8);
// printf("hostname %s\n", server->hostname);
return 0;
}

main()
{
struct irc_server* server;
printf("start\n");
printf("initcode: %d\n", irc_init_server(server, "bak",
"www.wp.pl", 7666));
if(server == NULL)
{
printf("null\n");
}
else
{
printf("not null> %d\n", server->hostname); //and I get
Segmentation fault.
}
}

what is wrong??

PS. THX
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <2a*************************@posting.google.com> , Michal J wrote:
....SNIP...
int irc_init_server(struct irc_server *server, char *username, char
*hostname, int port)
{
....SNIP...
main()
{
struct irc_server* server;
printf("start\n");
printf("initcode: %d\n", irc_init_server(server, "bak",
"www.wp.pl", 7666));
if(server == NULL)
{
printf("null\n");
}
else
{
printf("not null> %d\n", server->hostname); //and I get
Segmentation fault.
}
}

what is wrong??


Hint: initialize 'server' as NULL before calling irc_init_server() and
look what happens. Also remember that C passes parameters by value.

Regards,
--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
2614 JE Delft, The Netherlands
+31 (0)6 155 244 60
Nov 14 '05 #2

P: n/a
"Michal J" <ba*********@elektromet.pl> wrote in message
news:2a*************************@posting.google.co m...
I don't know what I'm doing wrong ??

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
Various non-standard headers here, although that is not the source of your
problem.
struct irc_server
{
//glowne dane
"//" comments are not part of C, except for C99. They are also generally a
bad idea in code posted to Usenet.
char *hostname;
int port;
char *username;
//kanaly
int number_of_channels;
char *channels[IRC_MAX_CHANNELS];
//socket
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;

};

int irc_init_server(struct irc_server *server, char *username, char
*hostname, int port)
{
//alokowanie pamieci glownej struktory
server = (struct irc_server*)malloc(sizeof(struct irc_server));
The above cast is not necessary and, had you inadvertently done so, would
have hidden the fact that you forgot to include <stdlib.h> causing undefined
behaviour. I prefer:

server = malloc(sizeof *server);

This, BTW, modifies the local variable server, which is initialised with the
value specified as the caller's argument.
//wpisujemy podstawowe informacje
server->hostname = (char*)malloc(sizeof(char)*strlen(hostname));
sizeof(char) is 1 by definition, so it is a useless factor here. You need to
allocate one more byte than strlen() returns, to accomodate the terminating
0:

server->hostname = malloc(strlen(hostname) + 1);
strcpy(server->hostname, hostname);

server->username = (char*)malloc(sizeof(char)*strlen(username));
Same as above.
strcpy(server->username, username);

server->port=port;
//socket
if((server->sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
{
return 1;
}
//reszte informacji o serververze
server->server = gethostbyname(server->hostname);
<OT> Better to check if the string specifies an IP address first. </OT>
if(server->server == NULL)
{
return 2;
}
server->serv_addr.sin_family = AF_INET;
server->serv_addr.sin_port = htons(server->port);
server->serv_addr.sin_addr = *((struct in_addr
*)server->server->h_addr);
memset(&(server->serv_addr.sin_zero), '\0', 8);
<OT> memset() here is a waste of time </OT>
// printf("hostname %s\n", server->hostname);
return 0;
}
Now, the local variable server has gone out of scope, causing a memory leak.

main()
Yuck.

int main(void)
{
struct irc_server* server;
printf("start\n");
printf("initcode: %d\n", irc_init_server(server, "bak",
"www.wp.pl", 7666));
The call to irc_init_server() is passed the _value_ of the uninitialised
pointer server. Nothing that irc_init_server() does will change the value
seen here.
if(server == NULL)
Undefined behaviour, since server is uninitialised.
{
printf("null\n");
}
else
{
printf("not null> %d\n", server->hostname); //and I get

The format specifier and type of the supplied argument do not agree.
Segmentation fault.
Syntax error. Now do you see why it's a bad idea to post code with "//"
comments?
}
return 0;
}

what is wrong??


Quite a bit :).

Alex
Nov 14 '05 #3

P: n/a

"Michal J" <ba*********@elektromet.pl> wrote in message news:2a*************************@posting.google.co m...
I don't know what I'm doing wrong ?? [snip] struct irc_server
{
//glowne dane
char *hostname;
int port;
char *username;
//kanaly
int number_of_channels;
char *channels[IRC_MAX_CHANNELS];
//socket
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;

}; [snip]
main()
{
struct irc_server* server; server = malloc(sizeof(struct irc_server));
if (!server)
{
printf ("malloc failed\n");
return 1;
}
printf("start\n");
printf("initcode: %d\n", irc_init_server(server, "bak", "www.wp.pl", 7666));
if(server == NULL)
{
printf("null\n");
}
else
{ /* Attention! server->hostname not initialized */ printf("not null> %d\n", server->hostname); //and I get Segmentation fault. /*
Perhaps
printf("not null> %s\n", server->hostname);
*/ }
}

[snip]
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Nov 14 '05 #4

P: n/a
In <2a*************************@posting.google.com> ba*********@elektromet.pl (Michal J) writes:
I don't know what I'm doing wrong ??
You're posting off topic code to comp.lang.c.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>


Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

P: n/a
"Dan Pop" <Da*****@cern.ch> wrote in message
news:cb**********@sunnews.cern.ch...
In <2a*************************@posting.google.com> ba*********@elektromet.pl (Michal J) writes:
I don't know what I'm doing wrong ??


You're posting off topic code to comp.lang.c.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>


The off-topic pieces are irrelevant to the OP's problem.

He is passing the parameter "server" by value, rather
than by address, so the pointer to the allocated memory
is lost upon return to main(). He needs to change the
function to accept a pointer to a pointer to the struct,
and to store the malloc() pointer through a dereference.
He also needs to initialize the "server" variable, and
test for NULL from malloc(), change '//' comments to
the '/* */' style, fix the main() signature, and so on.

--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!

Nov 14 '05 #6

P: n/a
In <Ur******************@newsread2.news.pas.earthlink .net> "xarax" <xa***@email.com> writes:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:cb**********@sunnews.cern.ch...
In <2a*************************@posting.google.com> ba*********@elektromet.pl

(Michal J) writes:
>I don't know what I'm doing wrong ??


You're posting off topic code to comp.lang.c.
>#include <sys/types.h>
>#include <sys/socket.h>
>#include <netinet/in.h>
>#include <netdb.h>


The off-topic pieces are irrelevant to the OP's problem.


Then, they should have been removed *before* posting the code, which
was littered with items coming from them.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

P: n/a
Dan Pop wrote:
In <Ur******************@newsread2.news.pas.earthlink .net> "xarax" <xa***@email.com> writes:

"Dan Pop" <Da*****@cern.ch> wrote in message
news:cb**********@sunnews.cern.ch...
In <2a*************************@posting.google.com> ba*********@elektromet.pl


(Michal J) writes:
I don't know what I'm doing wrong ??

You're posting off topic code to comp.lang.c.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>


The off-topic pieces are irrelevant to the OP's problem.


Then, they should have been removed *before* posting the code, which
was littered with items coming from them.


However, you have prided yourself on several past occasions on making
just the move xarax made, namely noticing that the *actual* issue in
an apparently off-topic post was in fact an issue addressable within
standard C.

Allin Cottrell
Nov 14 '05 #8

P: n/a
In <cb***********@f1n1.spenet.wfu.edu> Allin Cottrell <co******@wfu.edu> writes:
Dan Pop wrote:
In <Ur******************@newsread2.news.pas.earthlink .net> "xarax" <xa***@email.com> writes:

"Dan Pop" <Da*****@cern.ch> wrote in message
news:cb**********@sunnews.cern.ch...

In <2a*************************@posting.google.com> ba*********@elektromet.pl

(Michal J) writes:

>I don't know what I'm doing wrong ??

You're posting off topic code to comp.lang.c.
>#include <sys/types.h>
>#include <sys/socket.h>
>#include <netinet/in.h>
>#include <netdb.h>

The off-topic pieces are irrelevant to the OP's problem.


Then, they should have been removed *before* posting the code, which
was littered with items coming from them.


However, you have prided yourself on several past occasions on making
just the move xarax made, namely noticing that the *actual* issue in
an apparently off-topic post was in fact an issue addressable within
standard C.


Indeed. But the code did make sense in the absence of the platform
specific stuff.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.