data corruption in vector

I have this code that is supposed to store numbers in one vector and another vector stores pointers to the values in the first vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <inttypes.h>
#define INIT(a,b) memset(a, b, sizeof(a))
#define even(_) ((~_&1))
#define all(G) G.begin(), G.end()
#define MAX 1000000

using namespace std;

typedef vector<int*> p_vec;
typedef vector<int> i_vec;

int main()
{	  	
  	i_vec line;
  	p_vec station;
  	int n, d;
  	for ( cin >> n; n--; ) {
  		cin >> d;
  		line.push_back( d );
  		station.push_back( &line.back() );
  	}
  	
  	for (auto k: station)
  		cout << *k << " ";//compile with option: -std=c++11
  	
	return 0;
}


However, when I print out the values stored by the pointer vector, the first 2 values are not the same as what the input was. I have tried it with 4 - 10 values and always, the first 2 are not the same as the input but the rest are. I tried printing out the values as they are stored in the vector in the first for-loop and it seems to contain them all, but after that first for-loop, when I try to print *station.front(), it is now changed to something else.

input:
4
1231 234354 4544343 121312

output:
34144320 0 4544343 121312 


input:
6
123 2435 353 12221 356567 4544452

output:
21704768 0 353 12221 356567 4544452 
Last edited on
See here: http://www.cplusplus.com/reference/vector/vector/push_back/ under the section labeled Iterator validity. Vectors try to occupy continuous blocks of memory, so when they get to big they relocate.
Ok, so to avoid this I should probably set the size of the vectors before using them, then access them by index? Do you have a better solution?
Last edited on
I'd keep it simple, anytime you need a pointer to the element in 'i_vec', just pass the element by reference (&). But I don't actually know what you're trying to do so this might not be an option.
Use std::deque for i_vec.

The only changes you will have to make is adding #include <deque>
and changing line 18 to:

typedef deque<int> i_vec;
I don't know if what I am trying to do is possible, but I want to make a way for all the values in the station vector to have a pointer to each other, so that when I increment the value of one of the elements, it will also increment the value it is pointing to and in turn that one increments the next and so on... this is to replace the use of for-loops to iterate through the elements and increment them one at a time. So when I print out the numbers in the line vector, they are all updated to the new values

E: Thanks cire, that's awesome!
Last edited on
In that case I like cire's solution. Deque's don't move around in memory.
so that when I increment the value of one of the elements, it will also increment the value it is pointing to and in turn that one increments the next and so on


Sounds a little misguided, and also not possible with pointers. Incrementing a pointer means the pointer is no longer pointing at the original object. You could use a reference wrapper to approximate it, but I really don't see the point of the misdirection. It's more efficient and straightforward to iterate through the original vector and increment each element.

If you REALLY want to do away with for loops then try this out: http://www.cplusplus.com/reference/algorithm/for_each/?kw=for_each

It does the same thing but if you just don't like the look of for loops then this is a solution. Just keep in mind, assuming no compiler optimization this is technically slower because you're pushing and popping the stack for each element in the array. Also you'll have to dereference your elements when you increment them the way you described above.
Last edited on
well then for-loops it is. thank you both
Last edited on
Topic archived. No new replies allowed.