Unix Programming - Segmentation fault...

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2004 > Segmentation fault...





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Segmentation fault...
Michal J

2004-06-26, 10:11 am

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(hostna
me));
strcpy(server->hostname, hostname);

server->username = (char*)malloc(sizeof(char)*strlen(userna
me));
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
Rainer Temme

2004-06-26, 10:11 am


"Michal J" <baniak_spam@elektromet.pl> wrote:

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


> main()
> {
> struct irc_server* server;
> 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.
> }
> }


Hi Michal...

Two things are woth being mentioned here:

1) you start with an uninitialized server-variable ...
2) C-language does the parameter-transfer into functions "by value" (not "by
reference").

For "server" this means ...
with 1 ... it has an undefined contend ... it points "somewhere"
with 2 ... the function irc_init_server() will get a _copy_ of the
actual content of the server-pointer, but will not
be able to modify the server-variable in main itself.
therefore server is still uninitialized after the call...(pointing
"somewhere")
and it's not unlikely that its content is not NULL....and now
you try to dereference the pointer (to access server->hostname) and
print
this as an integer...if the pointer would point to legal memory (which
might
happen) you get some number printed (not the one you expect of corse)
if it points outside of legal memory you get a seg-fault.

If you want irc_init_server() be able to modify the original
server-variable...
you should change the declaration to

int irc_init_server (struct irc_server **server, ... more parameters ...)

and call it with ... irc_init_server(&server,...mode params).

inside the irc_init_server-function the access to the variable changes
from
server->something to (*server)->something
dont forget to ... *server=malloc(sizeof(struct irc_server)) ... assign
memory first!

Regards ... Rainer



Nils O. Selåsdal

2004-06-26, 10:11 am

Michal J wrote:
> I don't know what I'm doing wrong ??

...

>
> main()
> {
> struct irc_server* server;
> printf("start\n");

The following won't work, as C doesn't have call by reference,
thus the malloc you do in irc_init_server, won't be seen here.
You could do e.g irc_init_server(&server, ...
and *server = malloc(..) in irc_init_server..

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

Fred L. Kleinschmidt

2004-06-26, 10:11 am



Michal J wrote:
>
> 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(hostna
me));
> strcpy(server->hostname, hostname);


In addition to what others have said about how you are passing *server
as a parameter, there is a serious error here - you haven't malloc'd
enough space. You need (strlen(hostname)+1) in order to contain the
trailing NULL.

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


Same here - malloc is one byte short
>
> 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



--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com