LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2003, 12:31 AM   #1
khutze
LQ Newbie
 
Registered: Jul 2002
Posts: 13

Rep: Reputation: 0
sementation fault


can't figure out why program crashes
get segmentation fault


#include <iostream.h>

const int MAXLINE = 100;

int main()
{
char line[MAXLINE]="The quick brown fox jumps over the lazy dogs";
int j, i=0;
char newLine[MAXLINE];

while (line[i] != '\0')
{
j = 0;
while (line[i] != ' ')
{
newLine[j] = line[i];
i++;
j++;
}

i++;
}
return 0;
}
 
Old 06-06-2003, 03:26 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well bascially it's a bit of a nonsense... ;-)

you are doing some strange things like incrementing i in two seperate locations...

I'm guessing the segfault itself though is that you first check once that the current character is not \0 but then you blindy increment i without ever checking it again, so it will *always* go over the end of the variable.
 
  


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
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
c++ semantation fault siemens Linux - General 7 04-12-2005 07:10 AM
why segmentation fault??? andrewnow Programming 8 03-26-2005 06:50 PM
sementation fault slackware 9.0 on 486 syrtsardo Slackware 2 09-28-2003 05:41 AM
What is segmentation fault? koyi Programming 3 08-30-2003 12:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:56 PM.

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
Open Source Consulting | Domain Registration