Jump to content

Vector of pointers problem

- - - - -

  • Please log in to reply
4 replies to this topic

#1
arnes99

arnes99

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Here's a simple problem I can't figure out.
#include<vector>
#include<iostream>

int main(){
 std::vector<int*> vektor(3);
 int a = 7, b = 3, c = 5;
 vektor.push_back(&a);
 vektor.push_back(&b);
 vektor.push_back(&c);
 for(int i = 0; i < 3; i++){
  std::cout << *(vektor.at(i)) << " ";
 }
 return 0;
}
I get an error

Quote

Segmentation fault (core dumped)
However, if I change code above into this:

#include<vector>
#include<iostream>

int main(){
 std::vector<int*> vektor(1);
 int a = 7, b = 3, c = 5;
 vektor.push_back(&a);
 vektor.push_back(&b);
 vektor.push_back(&c);
 for(int i = 1; i < 4; i++){
  std::cout << *(vektor.at(i)) << " ";
 }
 return 0;
}
...it works! But why? I changed the vektor element count from 3 to 1 and changed inside for loop initial value of i from 0 to 1 and from i < 3 to i < 4.
How is this working now??? And more importantly, what mistake did I do first time?
Cause I can't see it. I set a vector holding a pointers to int variable and set the element count to 3. Then I put addresses of three int variables to pointers inside vector vektor with push_back() member function.

Since arrays and what not is always starting at index 0 I start to "read" from that index as well. But it doesn't work.
Only until I change element count for vector vektor from 3 to 1 and increase the starting index will it work.

Why is first example not working? :cursing:

#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Because the constructor you use:

std::vector<int *> vektor(3);

actually creates the vector with three elements (pointing to garbage.) When you add the next three later on they go into position 3,4,5.

It works in the second instance as your skipping over that one (your starting at 1 not 0) and actually looking at the data you put into the array.

To fix your code, either use the values you create in the constructure (i.e. use vektor[n] = &val; rather then push_back) or using the default constructor.

#3
arnes99

arnes99

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I see. Great explanation. Thank you abzero!

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I think I got it. You allocated 3 slots for vektor and those 3 slots have garbage value. Then you push back, meaning you add another 3 slots with addresses. Then you output first 3 garbage values.

EDIT: Didn't see it was already answered by abzero.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
arnes99

arnes99

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Well... there is no way to hide my shame I guess. Thing is, this was a problem I tried to fix for my friend and he even used a for loop to insert addresses into pointers(which all pointed to same i integer variable inside for loop ;)) so I fixed that quickly. However, I couldn't change a lot (and coudn't know what was a mistake),
Well, we learn on mistakes.

Edited by arnes99, 25 August 2010 - 01:25 PM.
added 'is' word





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users