Segmentation Fault when Calling Functions

This is a discussion on Segmentation Fault when Calling Functions within the C++ Programming forums, part of the General Programming Boards category; When I run the following code I get a segmentation fault. Code: #include <iostream> using namespace std; typedef int* IntPointer; ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    Segmentation Fault when Calling Functions

    When I run the following code I get a segmentation fault.
    Code:
    #include <iostream>
    using namespace std;
    
    typedef int* IntPointer;
    
    void notsneaky(IntPointer temp);
    void sneaky(IntPointer temp);
    void sneakybutnotsneaky(IntPointer& temp);
    
    int main()
    {
    IntPointer p;
    
    	p = new int;
    	*p = 77;
    	cout<<"Before call to function *p == "
               << *p << endl;
        
        
           notsneaky(p);
        
           cout<<"After call to \"notsneaky\" function *p == "
               << *p << endl;
               
           sneaky(p);
        
           cout<<"After call to \"sneaky\" function *p == "
               << *p << endl;
               
           sneakybutnotsneaky(p);
        
           cout<<"After call to \"sneaky_but_notsneaky\" function *p == "
               << *p << endl;
            
            return 0;
    }
    
    void notsneaky(IntPointer temp)
    {
          int *ptr;
          *ptr=23;
    	temp = ptr;
    	cout<<"Inside the function call *temp == "
    	    <<*temp<<endl;
    }
    
    void sneaky(IntPointer temp)
    {
    	*temp = 99;
    	cout<<"Inside the function call *temp == "
    	    <<*temp<<endl;
    }
    
    void sneakybutnotsneaky(IntPointer& temp)
    {
          int *ptr;
          *ptr=37;
    	temp = ptr;
    	cout<<"Inside the function call *temp == "
    	    <<*temp<<endl;
    }
    Here is the output:
    Code:
    thetinman@computer:~/code$ ./a.out
    Before call to function *p == 77
    Inside the function call *temp == 23
    After call to "notsneaky" function *p == 77
    Inside the function call *temp == 99
    After call to "sneaky" function *p == 99
    Segmentation fault
    Does anyonone know what is causing this segmentation fault? How should I go about fixing it?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    void notsneaky(IntPointer temp)
    {
          int *ptr;
          *ptr=23;
    You are attempting to dereference an uninitialized pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  3. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 07:32 AM
  4. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  5. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 12:43 PM

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