tags:

views:

123

answers:

5

Could somebody be kind to explain why in the world this gives me a segmentation fault error?

#include <vector>
#include <iostream>

using namespace std;

vector <double>freqnote;

int main(){

freqnote[0] = 16.35;

cout << freqnote[0];

return 0;
}

I had other vectors in the code and this is the only vector that seems to be giving me trouble.

I changed it to vector<int>freqnote; and changed the value to 16 and I STILL get the segmentation fault. What is going on?

I have other vector ints and they give me correct results.

+2  A: 

You're accessing vector out of bounds. First you need to initialize vector specifying it's size.

int main() {
    vector<int> v(10);
    v[0] = 10;
}
Leonid
Can you explain to me how other vectors haven't given me any issues without specifying the size?Also, I had a long vector (0-107 vectors) specified size of 108 and STILL Got a segmentation fault. What's going on?
Tek
You may have used them in a different context. For example you may have added elements using push_back, or insert functions. If you give the actual code example, then I'd be able to comment. Segmentation fault also may have been for a completely different reason.
Leonid
You should not really be declaring elements like that, its much better to use the push_back() syntax.
Tomas Cokis
Thanks leonid. And thanks for the suggestion Tomas.
Tek
Tomas, I don't agree as this depends on the context of your code very much. One good reason not to use push_back is that it increases a constant factor. I.e. it is cheaper to initialize 10 elements, then do push_back on 10 elements, as vector doubles it's size when there is not enough space, and makes calls to malloc (unless you overrode memory allocators), which is expensive -- and you'd rather want to do that once.
Leonid
@Leonid: If you know in advance how many elements you need, then resize() is fine to use. However, if you don't, then push_back is the main option.
DeadMG
Leonid: you can use `push_back` efficiently if you first call `vector::reserve` to reserve space.
Philip Potter
@DeadMG: but if you know the number of elements at the initialization time is there a reason to use resize? You can specify the size in the constructor to vector, as well as default element.
Leonid
@Philip: Yes, surely you can, but why would you want to do it in 2 lines of code, if you can do the same with 1 line.
Leonid
@DeadMG: in this case there is no advantage in using resize instead of the constructor, only disadvantages.
João Portela
I think you could argue that calling a function which is clearly named is much better for a project in terms of human readable code.ie, using reserve or resize instead of the constructor
Tomas Cokis
+10  A: 

Replace

freqnote[0] = 16.35;

with

freqnote.push_back(16.35);

and you'll be fine.

The error is due to that index being out-of-range. At the time of your accessing the first element via [0], the vector likely has a capacity of 0. push_back(), on the other hand, will expand the vector's capacity (if necessary).

stakx
+7  A: 

You can't initialise an element in a vector like that.

You have to go:

freqnote.push_back(16.35),

then access it as you would an array

Tomas Cokis
+2  A: 

As has been said, it's an issue about inserting an out of range index in the vector.

A vector is a dynamically sized array, it begins with a size of 0 and you can then extend/shrink it at your heart content.

There are 2 ways of accessing a vector element by index:

  • vector::operator[](size_t) (Experts only)
  • vector::at(size_t)

(I dispensed with the const overloads)

Both have the same semantics, however the second is "secured" in the sense that it will perform bounds checking and throw a std::out_of_range exception in case you're off bound.

I would warmly recommend performing ALL accesses using at.

The performance penalty can be shrugged off for most use cases. The operator[] should only be used by experts, after they have profiled the code and this spot proved to be a bottleneck.

Now, for inserting new elements in the vector you have several alternatives:

  • push_back will append an element
  • insert will insert the element in front of the element pointed to by the iterator

Depending on the semantics you wish for, both are to be considered. And of course, both will make the vector grow appropriately.

Finally, you can also define the size explicitly:

  • vector(size_t n, T const& t = T()) is an overload of the constructor which lets you specify the size
  • resize(size_t n, T const& t = T()) allows you to resize the vector, appending new elements if it gets bigger than it was

Both method allow you to supply an element to be copied (exemplar) and default to copying a default constructed object (0 if T is an int) if you don't supply the exemplar explicitly.

Matthieu M.
A: 

Besides using push_back() to store new elements, you can also call resize() once before you start using the vector to specify the number of elements it contains. This is very similar to allocating an array.

Frederik Slijkerman