Thread: access violation (segmentation fault)

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    access violation (segmentation fault)

    Why does this code cause my program to crash with the error "access violation (segmentation fault)"? and what is it?
    Code:
    for (counter=0;counter<UsedSpace;counter++)
    {
       if (Numbers1[counter]<Numbers2[counter])
         {
         sort[counter]=Numbers1[counter];
         }
         else sort[counter]=Numbers2[counter];
    }
    thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you are accessing an array out of bounds.
    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are providing a bit too little info to answer that.
    Most probably you have out-of-bounds accesses. For example, is sort resized to UsedSpace.

    Anyway, if this is meant for merging two vectors, you'll need to figure out the algorithm first (for example, on paper).

    Something like this (X marks past end of array, if any of the pointers reaches that, you'll need to figure out something in addition):
    Code:
    1 2 5 X
    ^
    a
    
    3 4 7 X
    ^
    b
    
    
    
    a points to smaller. Pick *a and advance a.
    
    1 2 5 X
      ^
      a
    
    3 4 7 X
    ^
    b
    
    1
    
    a still points to smaller. Pick *a and advance it again.
    
    1 2 5 X
        ^
        a
    
    3 4 7 X
    ^
    b
    
    1 2
    
    Now b points to a smaller value. Pick *b and advance it.
    
    1 2 5 X
        ^
        a
    
    3 4 7 X
      ^
      b
    
    1 2 3
    
    etc.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Ok thanks guys, guess its back to the drawing board. Again thank you

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here's another tip for you:
    Use std::vector if you aren't, then use the .at member function.
    If you try to access out-of-bounds, it will throw an exception.
    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.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are also probably better off adding items to Sort (which is initially empty) using push_back(), rather than indices.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    But then you might want to precede the 'push_back' by a 'reserve' for efficiency...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But then you might want to precede the 'push_back' by a 'reserve' for efficiency...
    Take a look at the last paragraph of Stroustrup's answer to the FAQ: Why are the standard containers so slow?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. access violation segmentation fault raised in your program
    By dinamit875 in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 11:44 AM
  2. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  3. Replies: 3
    Last Post: 07-17-2008, 08:45 AM
  4. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 03:28 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM