LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2013, 01:49 AM   #1
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Rep: Reputation: Disabled
Segmentation fault in C program


Code:
#include <sys/socket.h>
#include <sys/types/h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <uniistd.h>
#include <errno.h>
#include <arpa/net.h>

int main (void)
{
	int sockfd, n;
	struct sockaddr_in serv_addr;
	struct hostent *server;
	
	char buffer[256];
	char hostname[1024];
	struct hostent *h;
	gethostname(hostname, sizeof(hostname));
	server = gethostbyname(hostname);
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
	error("ERROR opening socket");
	if(server == NULL)
	{
		fprintf(stderr, "Error no such host\n");
		exit 0;
	}
	bzero((char ) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	memcpy(&serv_addr.sin_addr, server->h_addr, server->h_length);
	bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
	serv_addr.sin_port = htons (SERVERPORT);
	if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0) 
	error ("ERROR connecting");
	printf("Please enter the message: ");
	bzero(buffer, 256);
	fgets(buffer,255,stdin);
	n = write(sockfd, buffer, strlen(buffer));
	if (n < 0)
	error ("ERROR writing to socket");
	bzero(buffer, 256);
	n = read (sockfd, buffer, 255);
	if (n < 0)
	error ("ERROR reading from socket");
	printf("here we are %s\n", buffer);
	close(sockfd);
}
Hi everyone, I am a bit newbie and Linux. When I run above program, I got an error like "segmentation fault". Why it is so? if anyone knows, pls kindly guide me.

thanks a lot
 
Old 10-25-2013, 01:52 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
there is no such error (like segmentation fault).
Try to use debug info, debug your code step by step or try to use valgrind to catch the problem. You can even try to analyze the coredump.
 
Old 10-25-2013, 01:54 AM   #3
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
there is no such error (like segmentation fault).
Try to use debug info, debug your code step by step or try to use valgrind to catch the problem. You can even try to analyze the coredump.
Hi thanks for your reply. But how to use debug info. Now I am using Geany as my ide. I am a bit new to all those. So sorry for my stupid question. And thanks for your understanding...
 
Old 10-25-2013, 02:03 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
In geany Build -> Set Build Commands -> modify the gcc command, add -g to both Compile and Build
 
Old 10-25-2013, 02:09 AM   #5
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
I still got same error even though I use gdb.
 
Old 10-25-2013, 02:12 AM   #6
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
In geany Build -> Set Build Commands -> modify the gcc command, add -g to both Compile and Build
Means behind the
Code:
gcc -Wall -c "%f" -g
and
Code:
gcc -Wall -o "%e" "%f" -g
????


I think behind gcc -Wall -g???

Last edited by why_so_serious; 10-25-2013 at 02:13 AM.
 
Old 10-25-2013, 02:22 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
that is ok, -Wall -g should also work. Surely it will not solve the issue but will generate additional information to help to solve this problem.
you will have a result, a program and you can try gdb <your program> to debug it. (better to try ddd, it has a nice gui)
but first you need to check if the compilation/building gave you warnings and you need to try to eliminate them.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help!! Getting Segmentation Fault from this program bballingsrule Programming 3 12-03-2011 04:28 PM
Segmentation fault in C program radhikamlk Programming 5 02-10-2011 11:46 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
C++ Program, Segmentation Fault Fireball7 Programming 6 12-07-2005 05:22 PM
why segmentation fault in this program? asahlot Programming 13 10-17-2005 01:47 PM


All times are GMT -5. The time now is 06:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration