473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Vector at()

Hello,

in my program I use vectors, and I get a segfault. I use gcc on windows 10 x64. I could reproduce it with a test file:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     cout << "Hello world!" << endl;
  9.     std::vector<int> tvec = vector<int>();
  10.     tvec.reserve(256);
  11.     for(int counter = 0; counter < 256; counter++){
  12.         tvec->at(counter) = 0;
  13.     }
  14.     return 0;
  15. }
  16.  
  17.  
Does somebody know why I get a sagfault? Did I miss something in vectors behaviour?
Jul 29 '18 #1
3 1837
weaknessforcats
9,208 Expert Mod 8TB
When you declare:

Expand|Select|Wrap|Line Numbers
  1. vector<int> tvec;

the number of elements is 0. So using the "at" member function won't work because there are no elements set up.


Instead you use push_back. After you push_back there is an element and you can use "at" on it.


When you declare:

Expand|Select|Wrap|Line Numbers
  1. vector<int> tvec;
  2. tvec.reserve(256);
  3.  
  4.  
memory is allocated for 256 elements but no elements are in the vector. The "at" crashes again for the reason above.



To get 256 elements which are initialized as elements you declare:

Expand|Select|Wrap|Line Numbers
  1. vector<int> tvec(256);
  2.  
Since the elements are in the vector, the "at" works the first time.
Jul 29 '18 #2
So if I understand it correctly now: The reserve() still needs push_backs to fill the vector, but it takes care that the vector doesn't need to reallocate until I made 256 push_backs right?
Jul 30 '18 #3
weaknessforcats
9,208 Expert Mod 8TB
That's correct.

On that subject, when you declare vector<int> tvec as a local variable, do not assume the vector is on the stack. Most of the designs I have seen will keep the first 20-30 items on the stack and after that, the vector moves to the heap. So creating the vector on the heap is really a waste of time. You need do nothing to manage the vector memory.
Jul 31 '18 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Clement RAMBACH | last post by:
Hi, here is my problem: I have a std::vector< A* >. This vector contains pointers to the objects A i create (lets say about 4000 items). I do this several times in my application, and at the...
13
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
2
by: agenevera | last post by:
I am trying to insert a vector into a larger vector at various positions. This, however, will not compile. vector<double>::iterator pos; vector<double> tmp; vector<double> m_parameters;...
3
by: eriwik | last post by:
I use the following structure to store filenames for one or more "sets" grouped together by a number: map<int, map<string> > fileSets; As arguments to the constructor I send a...
9
by: Gernot Frisch | last post by:
Hi, I want to be able to write: class foo { std::vector<intm_i (64); }
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
12
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to...
9
by: Alexander Dong Back Kim | last post by:
Dear all, I want to create a method that return an element in a vector by selecting the element position by vector's at(). The method should be look like vector<Apple_appleBox; Apple &...
10
by: Prasad | last post by:
Hi, I have been using vector::iterators for a while now. This is the first time I have encountered this problem. The vector contains one element. 1. vector<GroupSetTemplate>::iterator gstIt;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.