lifetime of variables inside namespaces?

Jul 26, 2014 at 6:33pm
hello everybody. in the code below, what would be the lifetime of integer n?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

namespace Space{
    int n = 10;
}

int main(){
    cout << Space::n;
    return 0;
}


if i create a namespace with lots of variables, to be used as reference values to lots of different functions, will it hit the memory usage by the program? are the variables 'alive' while the program is in execution?

in my specific case, i have a class name "Style" and about 20 objects from this class, each one holding different parameters (like font color, font size, background image, etc) that will be used by hundreds of other classes. so my idea is to make a namespace and put these 20 styles inside it, to serve as reference. or is there any better way of doing it?

thanks in advance!
Jul 27, 2014 at 1:01am
Your namespace is global, therefore it will exist for the life of the program.

will it hit the memory usage by the program?

Yes.

are the variables 'alive' while the program is in execution

Yes.

Namespaces are a way of qualifying names to prevent naming collisions. Namespaces have no bearing on the lifetime of a variable.

Jul 28, 2014 at 1:55pm
oh i see. i'll try another approach then.
thanks!
Topic archived. No new replies allowed.