PDA

View Full Version : C++ exc_bad_access




augustmonth
May 12, 2011, 03:49 PM
Hi, have the following simple 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


testvector[0]=1;



robbieduncan
May 12, 2011, 03:51 PM
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:

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

augustmonth
May 12, 2011, 03:56 PM
So what should I do!?

By the way, if I include the following line

testvector.push_back(1);

everything works!

parapup
May 12, 2011, 03:58 PM
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 -

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.

Hansr
May 12, 2011, 03:59 PM
The vector is declared but not allocated.

You should do testvector.push_back(1) or
vector<int> testvector(1,0);
testvector[0]=1;

http://www.cplusplus.com/reference/stl/vector/vector/

I think you should grab a C++ book before continuing.

augustmonth
May 12, 2011, 04:01 PM
Thank you very much guys!

I do have a book : Accelerated C++

Hansr
May 12, 2011, 04:26 PM
Best re-read chapter 3 again then.

lloyddean
May 12, 2011, 04:36 PM
Thank you very much guys!

I do have a book : Accelerated C++

I'd say it's to fast for you..., slow down a little.

gnasher729
May 12, 2011, 04:56 PM
The error (EXC_BAD_ACCESS) on the line


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.

lloyddean
May 12, 2011, 06:36 PM
First check to see if the 'testvector' is 'empty' before modifying it.


#include <iostream>
#include <vector>

int main()
{
using std::vector;

vector<int> testvector;

if ( ! testvector.empty() )
{
testvector[0] = 1;
}

return EXIT_SUCCESS;
}

augustmonth
May 12, 2011, 08:25 PM
I'd say it's to fast for you..., slow down a little.

What book would you recommend? I like Accelerated C++

Thanks everyone who replied again!

lloyddean
May 12, 2011, 10:13 PM
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

chrono1081
May 13, 2011, 03:08 AM
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.

Hansr
May 13, 2011, 03:49 AM
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

augustmonth
May 13, 2011, 10:10 AM
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

lloyddean
May 13, 2011, 11:08 AM
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

Here works.

Hansr
May 13, 2011, 11:10 AM
In Accelerated C++ they rarely access vector the way I have done. They either use iterators with push_back or some other tricks.

It's for execution safety and speed reasons.

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

Here is as good a place as any.

chown33
May 13, 2011, 12:53 PM
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.

augustmonth
May 13, 2011, 01:07 PM
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!

chrono1081
May 13, 2011, 02:43 PM
Here works.

+1 a lot of us have read it and Macrumors is actually really beginner friendly, unlike some of the other forums you may find.