Thread: Segmentation fault?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    43

    Segmentation fault?

    Take a look at the following code:
    Code:
    typedef std::vector<unsigned int> VecUnsRAM;
    VecUnsRAM myPos(vertices.size());
    unsigned int count=0;
    for(unsigned int j=0; j<vertices.size();++j) {
            myP t=std::make_pair(count, vertices[j].D);
    	std::cout<<"Got here"<<std::endl;
    	PQ[j]=t;
    	std::cout<<"Got here"<<std::endl;
    	myPos[j]=count;// or myPos[j]=j; produces Seg fault error
    	std::cout<<"Got here"<<std::endl;
    	++count;
    	
        }
    The output is "Got here" twice, and then the Seg fault. Any ideas why this happens?
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Progress on answering this has begun elsewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Unfortunately, that was not a good answer. Your idea?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look at the last link in laserlight's signature.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Take a look at the following code:
    A meaningless exercise, when you're dealing with segfaults.

    It is highly likely that your problems started earlier in the program, and this is just where the problem shows up.

    Clone the project then
    - delete all the code that cannot have run until this moment
    - simplify the test case so there is less code and less data to deal with (deleting yet more code).

    While you're busy deleting, make sure it still crashes in the same place, for the same reason.

    When you're done, you can post your simpler program in its entirety, and your simple test case. Only then would we have a reasonable chance of spotting cause and effect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 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, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM