Reputation: 1
I declare a vector in the .h file:
typedef std::vector<bool> bit_vector;
bit_vector decoded_lcw_vec;
In the .cc file I fill the vector like this:
bit_vector decoded_lcw_vec(corrected_array, corrected_array + sizeof corrected_array / sizeof corrected_array[0]) ;
and the the correct values are in it:
printf( "\n\n Decode LDU1 LCW Vector Complete:");
for(int i = 0 ; i < 72; i++ ) cout << decoded_lcw_vec[i];
gives:
Decode LDU1 LCW Vector Complete:000000000000000000000000000000000000111111111111000000000000000000000101
so the problem is that if I try to access the vector outside of the function where it is filled, then I get a seg fault. No matter how I try to access the vector, the program dies. I want to do:
uint16_t
ldu1::lcf() const
{
const size_t LCF_BITS[] = {
0, 1, 2, 3, 4, 5, 6 , 7
};
const size_t LCF_BITS_SZ = sizeof(LCF_BITS) / sizeof(LCF_BITS[0]);
const uint16_t lcf = extract(decoded_lcw_vec, LCF_BITS, LCF_BITS_SZ);
return lcf;
}
it seg faults. I've tried many other ways to access the vector. If I try any kind of print statement, or whatever, program seg faults. So, no matter how I try to do it, the program seg faults when trying to access the vector outside of the function where it is filled. The problem has to be i am trying to make illegal access right? How can this be since I declared the vector private in the .h file and the class has not been destructed?? That vector should, at least I was under the impression that it would, persist until the the class was destroyed.
Is it possible that this vector is on the stack and hence out-of-scope by the time control returns from the call?
Upvotes: 0
Views: 222
Reputation: 4939
The problem appears to be that you are declaring a new variable with the same name in your function. This variables scope is the function and is destroyed at the end of the function. If you want it to be the class member change this line:
bit_vector decoded_lcw_vec(corrected_array, corrected_array + sizeof corrected_array / sizeof corrected_array[0]) ;
to:
decoded_lcw_vec = bit_vector(corrected_array, corrected_array + sizeof corrected_array / sizeof corrected_array[0]);
Upvotes: 3