Segmentation Fault

This is a discussion on Segmentation Fault within the C++ Programming forums, part of the General Programming Boards category; Hi All. I am a Student of Computer Science. I have just started programming in C++. I was running this ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    India
    Posts
    14

    Segmentation Fault

    Hi All.

    I am a Student of Computer Science. I have just started programming in C++.
    I was running this program which compiled and run successfully on Windows machine. But when I run it on Linux machine it compiled successfully but on running it gives SEGMENTATION FAULT. This is program....

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    
    class first{
    public:
    int a;
    };
    
    class second{
    public:
    void show(int x);
    first *p;
    };
    
    void second::show(int x){
    p->a=x;
    cout<<(p->a);
    }
    
    void main(){
    second s, *t;
    clrscr();
    t=&s;
    t->show(2);
    getch();
    }
    Please help.
    Thanks in Advance.

    --Ajay

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Firstly, that code shouldn't compile on a linux machine because linux compilers don't support conio.h. Secondly, the segmentation fault comes from the fact that you never point p to anywhere. This'll work better:
    Code:
    #include<iostream>
    #include<stdio.h>
    
    using namespace std;
    
    class first{
    public:
      int a;
    };
    
    class second{
    public:
      second() { p = new first; }
      ~second() { delete p; }
      void show(int x);
      first *p;
    };
    
    void second::show(int x){
      p->a=x;
      cout<<(p->a);
    }
    
    void main(){
      second s, *t;
    
      t=&s;
      t->show(2);
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
    int main() {

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The problem is that you have a pointer variable p inside class second, but you never initialize that variable so it doesn't point at anything. When you try to use it anything can happen. It might appear to work, or it might crash, or it might give strange values. That is called undefined behavior. The fact that it worked on Windows and not on Linux is just coincidence.

    So you need to initialize the pointer to something. One way is to use dynamic allocation like Noir did.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21