C++ exc_bad_access

Discussion in 'Mac Programming' started by augustmonth, May 12, 2011.

  1. augustmonth macrumors newbie

    Joined:
    May 12, 2011
    #1
    Hi, have the following simple code:

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    
    using std::string;
    using std::vector;
    
    int main () {
        
    	vector<int> testvector;
    	testvector[0]=1;
    	
    	    return 0;
    }
    The error (EXC_BAD_ACCESS) on the line


    Code:
    testvector[0]=1;
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    I certainly don't claim to be a C++ expert but it seems clear to me that you've declared a variable but not actually created a vector on this line:

    Code:
     vector<int> testvector;
    You then try and use the vector you've not actually created yet. I'm pretty sure you need to use new...
     
  3. augustmonth thread starter macrumors newbie

    Joined:
    May 12, 2011
    #3
    So what should I do!?

    By the way, if I include the following line

    Code:
    testvector.push_back(1);
    everything works!
     
  4. parapup macrumors 65816

    parapup

    Joined:
    Oct 31, 2006
    #4
    You created an uninitialized vector with no space allocated for any elements. Then used the [] operator on it which references element 0 which doesn't exist.

    If you explicitly allocate space for elements like this -

    Code:
     vector<int> testvector(10);
    Then it should work.

    Edit: push_back works because it allocates space if necessary. The [] operator doesn't - it directly references the element.
     
  5. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #5
  6. augustmonth thread starter macrumors newbie

    Joined:
    May 12, 2011
    #6
    Thank you very much guys!

    I do have a book : Accelerated C++
     
  7. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    I'd say it's to fast for you..., slow down a little.
     
  9. gnasher729 macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    Of course. You have a variable holding a vector. The number of elements in that vector is zero. You try to assign a value to the first element of a vector that doesn't have any elements. Obvious that it will crash.

    You should find a method that changes the number of elements in the vector.
     
  10. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #10
    First check to see if the 'testvector' is 'empty' before modifying it.

    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
        using std::vector;
    
        vector<int> testvector;
    
        if ( ! testvector.empty() )
        {
            testvector[0] = 1;
        }
        
        return EXIT_SUCCESS;
    }
    
     
  11. augustmonth thread starter macrumors newbie

    Joined:
    May 12, 2011
    #11
    What book would you recommend? I like Accelerated C++

    Thanks everyone who replied again!
     
  12. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #12
    Other than requests for free on-line books I don't recommend anything to people I don't already know and have some experience with. My reason is I don't know you so don't know how you learn best thus I can't make a reasonable recommendation that would be best for you.

    Sorry
     
  13. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #13
    Accelerated C++ is a good book, I've gone through it and use it for reference sometimes but if your brand new to C++ or programming in general it may be a bit much.

    One that may be of benefit is "Beginning Game Programming through C++" It still has you make little projects like Accelerated C++ does but doesn't hammer you with advanced concepts quite so fast.

    That being said unless you really get stuck or frequently frustrated in Accelerated C++ then I wouldn't see a reason to switch.
     
  14. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #14
    Accelerated C++ is written for people that have done some basic levels of coding before and are comfortable with the basic concepts and can easily jump to using libraries like STL without the need to know how the underlying works.

    If this is your first time learning programming I'd recommend shelfing the book for now and pick up a copy of Walter Savitch's Absolute C++ which is in my opinion the most beginner friendly C++ textbook as it's structure will teach you everything a beginner needs to know from the ground up: http://www.amazon.com/Absolute-C-4th-Walter-Savitch/dp/0136083811
     
  15. augustmonth thread starter macrumors newbie

    Joined:
    May 12, 2011
    #15
    Thanks everyone for the book recommendations.

    I did some java programming before and vba (mostly in school).

    In Accelerated C++ they rarely access vector the way I have done. They either use iterators with push_back or some other tricks.

    I will re-read Chapter 3 and if all fail give it try to another book.

    Thanks again!

    P.S. Is there good place to post simple questions like mine? I would image with any book I will run into something simple and stupid
     
  16. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #16
    Here works.
     
  17. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #17
    It's for execution safety and speed reasons.

    Here is as good a place as any.
     
  18. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #18
    Most programming books these days have a companion website with forums. So look in the book for what its companion website is, or do some googling with the book's title and author as search terms.
     
  19. augustmonth thread starter macrumors newbie

    Joined:
    May 12, 2011
    #19
    awesome idea!
     
  20. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #20
    +1 a lot of us have read it and Macrumors is actually really beginner friendly, unlike some of the other forums you may find.
     

Share This Page