?
Solved

Error: 'Segmentation fault'

Posted on 2007-08-12
6
Medium Priority
?
804 Views
Last Modified: 2008-03-17
Hi experts,

I just beginner in computer programming- not very far from the 'Hello world'. OS is linux and I use it through 'putty' in a pc (Windows XP).  My problem is that the following little piece of code compiles(c++) but gives an error message "Segmentation Fault". Any help with explanation? Thanks.
0
Comment
Question by:rajpol
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19680678
Where is the code?
0
 

Author Comment

by:rajpol
ID: 19680779
Sorry
Its here:

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
    int n;
    double x[25];
    double y[25];

    x[n] = n*15/180;
    y[n] = sin(M_PI*x[n]);

    ofstream output("sine_data.txt");

    for(int i =0;i <25;i++)
    {
        output <<"\t" << x[i] <<"\t\t"<<y[i]<<'\n';

    }

    return 0;
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 19681055
'n' is unintialized, thus the error (it is just a random value at that point) - try

int main()
{
    int n;
    double x[25];
    double y[25];

    for (n = 0; n < 25; n++)
    {
      x[n] = n*15/180;
      y[n] = sin(M_PI*x[n]);
    }

    ofstream output("sine_data.txt");

    for(int i =0;i <25;i++)
    {
        output <<"\t" << x[i] <<"\t\t"<<y[i]<<'\n';

    }

    return 0;
}

instead to fill in the values. You might want to define M_PI as well.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:rajpol
ID: 19681095
Thanks jkr. It solved the problem. Just one more dumb question. Do the arrays, as in this example,  initialize by default? Any precautions?

rajpol
0
 
LVL 86

Expert Comment

by:jkr
ID: 19681156
Arrays initialize to whatever was in the memory location where they are created, so it is always a good idea to initialize them "manually". In the above, that is done by the calculation foop.
0
 

Author Comment

by:rajpol
ID: 19681226
Thank you very much.

rajpol
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question