segment fault with the program

This is a discussion on segment fault with the program within the C++ Programming forums, part of the General Programming Boards category; I am receiving segment fault with this program Code: #include <iostream> #include <new> #include <cstdlib> using namespace std; class ar ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    segment fault with the program

    I am receiving segment fault with this program

    Code:
    #include <iostream>
    #include <new>
    #include <cstdlib>
    
    using namespace std;
    
    class ar
    {
    	int *p;
    	int size;
    	public:
    	ar(int sz){
    		cout<<"Initializing array in Constructor\n";
    		try {
    			p = new int[sz];
    		}
    		catch(bad_alloc xa){
    		cout <<"Allocation Failure\n";
    		exit(EXIT_FAILURE);	
    		}
    	}
    	ar (const ar &a);
            ~ar(){cout << "Destructor is called\n";delete []p;}
    	void set_val(int i,int j){if(i>0&&i<size) p[i]=j; }
            void get_val(int i){cout<<"Val is :"<<p[i];cout<<"\t";}
    };
    
    
    ar::ar(const ar &a)
    {
    	int i;
    	try 
    	{
    		p = new int[a.size];
    	}
    	catch(bad_alloc sp)
    	{
    		cout<<"Allocation Failure\n";
    		exit(EXIT_FAILURE);
    	}
    	for (i=0;i<a.size;i++) p[i]=a.p[i];
    }
    
    int main()
    {
    	ar a(5),b(5);
            
            b=a;	
    
            for(int i=0;i<5;i++)
            	a.set_val(i,i);        
            
    	b.set_val(2,3);          
    	b.set_val(3,63);          
    	b.set_val(4,983);          
    
            for(int i=0;i<5;i++)
            	a.get_val(i);        
            cout<<"\n============ Copy Constructor goes here==============\n"  ;
            ar c(b);        
             
    	for(int i=0;i<5;i++)
            	c.set_val(i,i);        
            
    	//c.set_val(2,3);          
    	//c.set_val(3,63);          
    	//c.set_val(4,983);          
    
            for(int i=0;i<5;i++)
            	a.get_val(i);
    	return 0;
    }
    1. I am getting segment fault after the following statement
    cout<<"\n============ Copy Constructor goes here==============\n" ;

    Thanks in advance

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Where do you assign a value to the member variable size? Shouldn't it be set in the constructor?


    Jim

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    thanks...........

    but the segmentation fault still persists even after adding.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by sanddune008 View Post
    thanks...........

    but the segmentation fault still persists even after adding.
    1. you need to set 'size' in both constructors

    2. since you don't have an assignment operator defined, you get a compiler defined assignment, so when you assign b = a the original pointer that b had is overwritten with the pointer from a. then at the end of your program when the desttructors are called, that pointer is released twice which can cause a fault. plus your original pointer from 'b' is lost and never deallocated.

    you need to define an assignment operator that handles the transfer of pointers properly.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,989
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program that segment faults..
    By sanddune008 in forum C Programming
    Replies: 12
    Last Post: 04-28-2010, 03:48 AM
  2. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  3. Replies: 3
    Last Post: 01-13-2010, 11:16 PM
  4. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  5. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 08:16 PM

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