Segmentation fault?

This is a discussion on Segmentation fault? within the C++ Programming forums, part of the General Programming Boards category; 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; ...

  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
    21,593
    Progress on answering this has begun elsewhere.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,246
    Look at the last link in laserlight's signature.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 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