473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Segmentation fault...

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
8 2224
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
"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

"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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.