0

Problem: Values of global vector t1 is not accessible in main destructor function i.e. "after_main()" function. Other types of global variable values are accessible.

PFB Code I have tried & output:

  1. Declared global vector t1.
  2. In main function added values in t1
  3. When printed values inside "after_main()" function, it prints garbage values.
  4. When printed size it shows correct size of the vector inside "after_main()" function.

From 3rd and 4th point I can say that vector is not getting destroyed but values inside vector are getting destroyed. But I am not able to understand why.

Lifetime of the global variable is entire runtime of the program. Hence vector should not get destroyed during "after_main()" function execution.

Supporting link: https://en.wikipedia.org/wiki/Global_variable

Questions:

  1. Can someone please explain, why this is happening i.e. why vector is behaving strange?
  2. I want to access values (any dynamically growing structure like vector) modified during main in "after_main()" function, is there any solution for this?

With Dynamic Vector

#include <vector>
#include<iostream>
using namespace std;
void after_main() __attribute__((destructor));
vector<int> t1;
void after_main(){
    cout << t1.size() << endl; //output: 2
    cout << t1[0] << endl; //output: garbage value
    cout << t1[1] << endl; //output: garbage value
}
int main(){
    t1.push_back(10);
    t1.push_back(20);
}

predefined vector size

#include <vector>
#include<iostream>
using namespace std;
void after_main() __attribute__((destructor));
vector<int> t1(2);
void after_main(){
    cout << t1.size() << endl; //output:2 
    cout << t1[0] << endl; //output:0
    cout << t1[1] << endl; //output:0
}
int main(){
    t1[0] = 10;
    t1[1] = 20;
}

Example of global integer variable accessible in "after_main()" function:

#include

#include<iostream>
using namespace std;
void after_main() __attribute__((destructor));
int x;
void after_main(){
    cout << x << endl;//output: 5
}
int main(){
    x = 5; 
}

Example of global array variable accessible in "after_main()" function:

#include <vector>
#include<iostream>
using namespace std;
void after_main() __attribute__((destructor));
int x[10];
void after_main(){
    cout << x[0] << endl; //output: 10
    cout << x[1] << endl; // output: 20
}
int main(){
    x[0] = 10;
    x[1] = 20;
}
yrb
  • 1
  • 1
  • 1
    how do you know that the vector is not getting destroyed? If it is destroyed, calling `t1.size()` is undefined behavior. Did you expect to get `0` from calling `size` ? – 463035818_is_not_an_ai Oct 11 '21 at 07:29
  • you cannot do anything meaningful with an already destroyed object, let alone check if it has been destroyed – 463035818_is_not_an_ai Oct 11 '21 at 07:30
  • @463035818_is_not_a_number calling size on a vector object gives correct value 2, i.e. number of values pushed during main. From this i thought that vector is not destroyed. Also other global variables are accessible in after_main function, only vector is behaving strange. – yrb Oct 11 '21 at 07:35
  • 1
    "I can say that vector is not getting destroyed" your conclusion is based on the false assumption that you could distinguish a "good `2`" from a "garbage `2`". – 463035818_is_not_an_ai Oct 11 '21 at 07:43
  • @463035818_is_not_a_number Why other data types are not getting destroyed but vector – yrb Oct 11 '21 at 07:48
  • @yrb How do you know they are not getting destroyed? BTW, for trivial types such as `int`, "destruction" is effectively no-op (it has no observable behavior) and happens once the object storage is deallocated. There is no explicit destruction operation (such as a counterpart of placement new). – Daniel Langr Oct 11 '21 at 07:54
  • @463035818_is_not_a_number Why other data types are not getting destroyed but vector. Also lifetime of the global variable is during program is execution how it can get destroyed? Supporting Link: https://en.wikipedia.org/wiki/Global_variable – yrb Oct 11 '21 at 07:55
  • Relevant: [C++ static initialization vs __attribute__((constructor))](https://stackoverflow.com/q/8433484/580083), [Checking whether current object is destroyed or not...](https://stackoverflow.com/q/62118553/580083) – Daniel Langr Oct 11 '21 at 08:00
  • actually I don't know if first `after_main` will be called or first the vector is destroyed (see Daniels link for that). I am just saying that your conclusion is wrong. Just because you get `2` as output does not imply that the vector has not yet been destroyed. You cannot access something after it has been destroyed. If you do your program has undefined behavior and output could be anything, for example `2` (or anything else) – 463035818_is_not_an_ai Oct 11 '21 at 08:04
  • @463035818_is_not_a_number As daniel comment is talks about checking whether object is destroyed or not withing class. Thats not restriction with me. So, is there any way I can check whether reference variable i am accessing has valid object pointer or not? where I can debug actual issue – yrb Oct 11 '21 at 08:27
  • @yrb No, at least in general, there is no way to check object destruction even from outside. – Daniel Langr Oct 11 '21 at 08:35
  • @DanielLangr So how can i check why this is happening. Also it has been stated that lifetime of the global variable is until program ends. Now I dont know what program end mean over here. Also After reading all this I am still not clear why this is happening? – yrb Oct 11 '21 at 09:12
  • According to the C++ standard, your code has _undefined behavior_ anyway, since you are using a non-standard C++ extension (`__attribute__(destructor))`). Then, basically the only option is to look for answers in the documentation of your implementation (GCC, Clang,...). Even then, it likely won't explain what it does when you access an object that has been destroyed. Then, the only option is usually to study the generated assembly. Sometimes a debugger can help as well. – Daniel Langr Oct 11 '21 at 09:16

0 Answers0