Thread: Seg fault in easy, easy code

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    Seg fault in easy, easy code

    I've got this snippet of code that is very, very simple... yet I get a segmentation fault right when it is simply printing cout statements...

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
      vector<int> intV;
      intV.push_back(4);
      intV.push_back(6);
      intV.push_back(2);
      intV.push_back(5);
      intV.push_back(10);
      intV.push_back(12);
      intV.push_back(8);
      intV.push_back(0);
      intV.push_back(3);
    
      vector<int> intVG(intV);
      vector<int> intVL(intV);
    
      int i;
    
      cout << "Size of vector is: " << intV.size() << endl;
      cout << "Unsorted vector: ";
      
      for (i = 0; i < intV.size(); i++)
      {  
    	cout << "\nNumber " << i << ": " << intV[i] << " ";
      }
      cout << "\nCheck";
    
    return 0;
    }
    It gets the error right after it prints all 8 integers, but before the word "Check." Here is my output on g++:

    Size of vector is: 9
    Unsorted vector:
    Number 0: 4
    Number 1: 6
    Number 2: 2
    Number 3: 5
    Number 4: 10
    Number 5: 12
    Number 6: 8
    Number 7: 0
    Number 8: 3
    Segmentation fault (core dumped)


    Any help is appreciated!!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I get no such issue. Are you sure that's exactly as you compiled it?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    Well, it's part of a file that uses Heap Sort which is implemented in another file... but the part that's seg faulting in this file is way before any of the heap stuff is even created. I suppose I can post the rest of the code in this file, but I'm not sure if it'll help:

    Code:
    #include <iostream>
    #include <vector>
    #include <deque>
    #include <BasicContainer.h>
    #include <priority_queue.h>
    
    using namespace std;
    using namespace cop4530;
    
    template <class C, class P>
    void HeapSort(const Priority_Queue<C, P>& q, const C& c);
    
    template <class C, class P>
    void HeapSort(Priority_Queue<C, P>& q, C& c)
    {
    	for (int i = c.size()/2; i >= 0; i--)
    		q.PercolateDown(i, c.size());
    	for (int j = c.size() - 1; j > 0; j--)
    	{
    		swap(c[0], c[j]);
    		q.PercolateDown(0, j);
    	}
    }
    
    
    int main()
    {
      //Test 1: container = vector
      vector<int> intV;
      intV.push_back(4);
      intV.push_back(6);
      intV.push_back(2);
      intV.push_back(5);
      intV.push_back(10);
      intV.push_back(12);
      intV.push_back(8);
      intV.push_back(0);
      intV.push_back(3);
    
      vector<int> intVG(intV);
      vector<int> intVL(intV);
    
      int i;
    
      cout << "Size of vector is: " << intV.size() << endl;
      cout << "                     Unsorted vector: ";
      for (i = 0; i < intV.size(); i++)
      {  
    	cout << "\nNumber " << i << ": " << intV[i] << " ";
      }
      cout << "\nCheck";
      
      Priority_Queue< vector<int> > Q1 (intV);
    
      HeapSort(Q1, intV);
    
      cout << "             Sorted vector (default): ";
      for (i = 0; i < intV.size(); i++)
        cout << intV[i] << " ";
      cout << "\n";
    
      Priority_Queue< vector<int>, greater<int> > Q2 (intVG);
      HeapSort(Q2, intVG);
    
      cout << "          Sorted vector (increasing): ";
      for (i = 0; i < intVG.size(); i++)
        cout << intVG[i] << " ";
      cout << "\n";
    
      Priority_Queue< vector<int>, less<int> > Q3 (intVL);
      HeapSort(Q3, intVL);
    
      cout << "          Sorted vector (decreasing): ";
      for (i = 0; i < intVL.size(); i++)
        cout << intVL[i] << " ";
      cout << "\n\n\n";
    
    
      //Test 2: container = deque
      deque<char> charD;
      charD.push_back('F');
      charD.push_back('D');
      charD.push_back('K');
      charD.push_back('M');
      charD.push_back('A');
      charD.push_back('S');
      charD.push_back('U');
      charD.push_back('C');
      charD.push_back('E');
    
      deque<char> charDG(charD);
      deque<char> charDL(charD);
    
      cout << "                      Unsorted deque: ";
      for (i = 0; i < charD.size(); i++)
        cout << charD[i] << " ";
      cout << "\n";
    
      Priority_Queue< deque<char> > Q4 (charD);
      HeapSort(Q4, charD);
    
      cout << "              Sorted deque (default): ";
      for (i = 0; i < charD.size(); i++)
        cout << charD[i] << " ";
      cout << "\n";
    
      Priority_Queue< deque<char>, greater<char> > Q5 (charDG);
      HeapSort(Q5, charDG);
    
      cout << "           Sorted deque (increasing): ";
      for (i = 0; i < charDG.size(); i++)
        cout << charDG[i] << " ";
      cout << "\n";
    
      Priority_Queue< deque<char>, less<char> > Q6 (charDL);
      HeapSort(Q6, charDL);
    
      cout << "           Sorted deque (decreasing): ";
      for (i = 0; i < charDL.size(); i++)
        cout << charDL[i] << " ";
      cout << "\n\n\n";
    
    
      //Test 3: container = BasicContainer
      BasicContainer<float> floatBC;
      floatBC.push_back(4.5);
      floatBC.push_back(6.2);
      floatBC.push_back(2.1);
      floatBC.push_back(5.6);
      floatBC.push_back(10.3);
      floatBC.push_back(12.4);
      floatBC.push_back(8.3);
      floatBC.push_back(0.5);
      floatBC.push_back(3.4);
    
      BasicContainer<float> floatBCG(floatBC);
      BasicContainer<float> floatBCL(floatBC);
    
      cout << "             Unsorted BasicContainer: ";
      for (i = 0; i < floatBC.size(); i++)
        cout << floatBC[i] << "\t";
      cout << "\n";
    
      Priority_Queue< BasicContainer<float> > Q7 (floatBC);
      HeapSort(Q7, floatBC);
    
      cout << "     Sorted BasicContainer (default): ";
      for (i = 0; i < floatBC.size(); i++)
        cout << floatBC[i] << "\t";
      cout << "\n";
    
      Priority_Queue< BasicContainer<float>, greater<float> > Q8 (floatBCG);
      HeapSort(Q8, floatBCG);
    
      cout << "  Sorted BasicContainer (increasing): ";
      for (i = 0; i < floatBCG.size(); i++)
        cout << floatBCG[i] << "\t";
      cout << "\n";
    
      Priority_Queue< BasicContainer<float>, less<float> > Q9 (floatBCL);
      HeapSort(Q9, floatBCL);
    
      cout << "  Sorted BasicContainer (decreasing): ";
      for (i = 0; i < floatBCL.size(); i++)
        cout << floatBCL[i] << "\t";
      cout << "\n";
    
      cout <<"\n\n";
    
    
      
      return 0;
    }
    I'm compiling it plain and simple, like this:
    g++ -I. fpq.cpp
    (fpq.cpp is the name of this file, of course)
    Last edited by lisa1901; 12-09-2007 at 07:51 PM. Reason: clarity

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    cout << "\nCheck";
    Just so you know, this won't be printed to stdout anytime soon. stdout is line buffered generally. This means everything is flushed to the terminal window when a '\n' is encountered or the stream is explicitly flushed (printing std::endl provides a '\n' and explicitly flushes.)

    If you either tack on a '\n' or std::endl to the end of the message to be printed, you'll see it printed just fine. This means your segmentation fault is actually occurring later on. So guess where. Yes, your complication load of other stuff.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    ahhh... yes. I see I have a wonderful evening ahead of me.

    Thanks for the help!

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    g++ -g -I. fpq.cpp
    gdb -ex r ./a.out
    will give you exactly where the segfault is.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    also, instead of using "-I.", the convention is to write
    Code:
    #include "custom_header.h"
    instead of
    Code:
    #include <custom_header.h>
    that way you don't need the "-I.".

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Lisa, the first code you posted should not cause a segmentation fault. The second code is doing a lot more, and it's not obvious where the problem (if any, assuming that it is actually the code that is exhibiting the problem) is. With your use of non-standard headers, for example, it is not possible to exclude the possibility of a function inlined in a header being the root cause.

    I suggest you selectively eliminate parts of your code in an endeavour to find a small but complete example that exhibits your problem. Depending on how you go about it, the process of trying to find such a small example might help you find the cause on your own. If not, then you will have a small code sample to present so that people here will (hopefully) have a reasonable chance of being able to help you. When posting that code, it will also be helpful if you identify the compiler and options you are using, and list any error messages from the compilation or linking.

    The main problem for forum readers at present is that there is obviously code you're working with that we can't see, but might be contributing to your troubles. We're not omnipotent, so can't guess what the problems might be.
    Last edited by grumpy; 12-09-2007 at 09:33 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cyberfish View Post
    also, instead of using "-I.", the convention is to write
    Code:
    #include "custom_header.h"
    instead of
    Code:
    #include <custom_header.h>
    that way you don't need the "-I.".
    As far as I'm aware - #include "" includes in the current directory and #include <> searches common directories, but it may be that compilers interpret them a little differently.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    As far as I'm aware - #include "" includes in the current directory and #include <> searches common directories, but it may be that compilers interpret them a little differently.
    that is what I meant. #include "" searches the current dir, while #include <> searches the include path, which the "-I." option modifies to include the current directory.

    Both should work, but the convention, as far as I am aware, is to just use #include "". #include <> is normally reserved for library stuff, not program-specific headers.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by MacGyver View Post
    Code:
    cout << "\nCheck";
    Just so you know, this won't be printed to stdout anytime soon. stdout is line buffered generally. This means everything is flushed to the terminal window when a '\n' is encountered or the stream is explicitly flushed (printing std::endl provides a '\n' and explicitly flushes.)
    IIRC cout is unbuffered by default according to the c++ standard. There should be no output-lag (if the compiler is standard compliant)

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cyberfish View Post
    that is what I meant. #include "" searches the current dir, while #include <> searches the include path, which the "-I." option modifies to include the current directory.

    Both should work, but the convention, as far as I am aware, is to just use #include "". #include <> is normally reserved for library stuff, not program-specific headers.
    I just have no idea what the -I setting is ('cause I don't use compiler flags nor gcc). That's why I replied...
    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. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. next_permutation seg fault
    By zxcv in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2008, 07:40 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. seg fault with c string conversion
    By axon in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2004, 10:03 PM
  5. free and seg fault
    By dkt in forum C Programming
    Replies: 5
    Last Post: 09-19-2001, 10:10 PM