Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerations and their memory allocation 1

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
If anybody could add some clarification on this:
are enumerations and const members in classes allocated once over the lifetime of a program or are they allocated each time a new instance of the class is created?

I am dealing with the following problem:
In a file, there are several records - 122 of them. I've added an enumeration which specifies their names, for easy array access. Now, this enumeration is defined in the File class, rather than in the record class and it seems to be against the nature of things. I was thinking that the enumeration would be actually allocated as a 122 integer array or something like that (right or wrong?) and I was considering what would happen in the case when a file contains 10.000 records.

But, if the enumerations are placed in a separate memory area and only allocated once, there is no memory problem.

So, can anybody enlighten me on this issue? [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Something like that...

Funny you should mention this, about a week ago I asked a similar question about the difference between enums and const ints. The reply I got was that they take up about the same amount of memory, the only difference being that the enum may need an extra few bytes to store some enum-specific details. No big deal there.

As far as instantiation goes, yes, they will be allocated each time you create an instance of the class. The way around that is to use static data members. Those are only allocated once. The syntax for static members is a bit weird, though. First, you need to declare them in the class, and then initialize them in the implementation, like this:

Code:
(inside Foo.h)

class Foo {
public:
    static const int stat;
};

----------------------------------------------

(inside Foo.cpp)

const int Foo::stat=123;

-----------------------------------------------

(inside main.cpp)

cout << Foo::stat;

The redeclaration in Foo.cpp is not a mistake. Simply saying Foo::stat=123 will not work; you need the whole thing.

Or, you can do this:

Code:
(inside Foo.h)

class Foo {
public:
    static enum vars {
        a=1,
        b,
        c
    };
};

-----------------------------------------

(inside main.cpp)

cout << Foo::b;

Notice that you don't have to redeclare the enum outside the class. Apparently, the compiler takes care of that stuff for you this time.
 
Ok, Ok... Cool. I am really not a C++ rookie even if the question I've posed might be sounded like a rookie question (did it?). So, I was familiar with the static stuff and I heavily use it.

However, the static on enumerations is a very helpful tip, thanks a bunch for that!
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top