Segmentation fault...

M

Michal J

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
 
R

Rob van der Leek

Michal J wrote: said:
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,
 
A

Alex Fraser

Michal J said:
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);

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;
}

Now, the local variable server has gone out of scope, causing a memory leak.

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
 
A

Alex Vinokur

Michal J said:
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]
 
X

xarax

Dan Pop said:
In <[email protected]> (e-mail address removed)
(Michal J) said:
You're posting off topic code to comp.lang.c.

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!
 
D

Dan Pop

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

Allin Cottrell

Dan said:
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
 
D

Dan Pop

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

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top