Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

augustmonth

macrumors newbie
Original poster
May 12, 2011
6
0
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;
 
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...
 
So what should I do!?

By the way, if I include the following line

Code:
testvector.push_back(1);

everything works!
 
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.
 
The error (EXC_BAD_ACCESS) on the line


Code:
testvector[0]=1;

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.
 
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;
}
 
What book would you recommend? I like Accelerated C++

Thanks everyone who replied again!

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
 
What book would you recommend? I like Accelerated C++

Thanks everyone who replied again!

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.
 
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
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
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
 
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

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

awesome idea!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.