Enum vs Char * array

T

Travis

Just curious, which takes less memory?

enum Months {
JAN, FEB, MAR,
APR, MAY, JUN,
JUL, AUG, SEP,
OCT, NOV, DEC
};

static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"
};
 
V

Virtual_X

Just curious, which takes less memory?
static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"};

this method is invalid because of two reasons
1- char *Months[12] is refer to an array of pointers which consist of
12 pointer
2- if we make it as char so cannot assign data like that
"JAN","FEB",.....
you need only single character such as 'a','b'
but if you need to make some thing like that you can use the type
string

ie:
#include <string>

main()
{
string Months[12] = {"JAN","FED",......}
}

and for the memory use , i think the enum is less than string in
memory usage because it's refer to numbers not strings

finally if you need to more save to the memory you can use

bool x[12]={1,2,3,.....};

which 1 refer to JAN and 2 refer to FEB ....

because bool is use only 1 byte unlike string or enum
 
D

Default User

Virtual_X said:
Just curious, which takes less memory?
static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"};

this method is invalid because of two reasons
1- char *Months[12] is refer to an array of pointers which consist of
12 pointer
2- if we make it as char so cannot assign data like that
"JAN","FEB",.....
you need only single character such as 'a','b'

What are you talking about? The code shown creates an array of 12
pointer to constant char, which are intialized with pointers to various
string literals. Perfectly valid, other than the OP left off one of the
" around APR.




Brian
 
A

Alf P. Steinbach

* Travis:
Just curious, which takes less memory?

enum Months {
JAN, FEB, MAR,
APR, MAY, JUN,
JUL, AUG, SEP,
OCT, NOV, DEC
};

This is a type. A type doesn't use any memory. If you declare a
variable of that type, the variable uses memory (unless optimized away).

static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"
};

This is a variable. It uses 12*sizeof(char*) bytes of memory directly,
plus sizes of the strings (about 12*4 bytes), plus any padding the
compiler wishes to introduce (not in the array of pointers, but between
the string constants).

Why don't you check what sizes you end up with with /your/ compiler and
compilation options?
 
D

Default User

Travis said:
Just curious, which takes less memory?

enum Months {
JAN, FEB, MAR,
APR, MAY, JUN,
JUL, AUG, SEP,
OCT, NOV, DEC
};

static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"
};

Implementation-specific. Probably the enum.

However, the two constructs are so different that a size comparison is
fairly useless.





Brian
 
T

Travis

Virtual_X said:
Just curious, which takes less memory?
static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"};
this method is invalid because of two reasons
1- char *Months[12] is refer to an array of pointers which consist of
12 pointer
2- if we make it as char so cannot assign data like that
"JAN","FEB",.....
you need only single character such as 'a','b'

What are you talking about? The code shown creates an array of 12
pointer to constant char, which are intialized with pointers to various
string literals. Perfectly valid, other than the OP left off one of the
" around APR.

Brian

Thank you. I didn't know what that person was talking about either.
Sorry I forgot a " near April. That's what you get for so much rain
April!

I was just curious, because some of the older guys I work with do the
char * thing and I like the clean look of ENUM but if we're talking
performance hit (we are in an OS dept), then maybe I change my ways.
 
R

Robert Bauck Hamar

Virtual_X said:
Just curious, which takes less memory?
static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"};

this method is invalid because of two reasons
No.

1- char *Months[12] is refer to an array of pointers which consist of
12 pointer

char *Months[12] declares Months as an array of twelve pointers. Each of
them is initialised with a pointer to static data.
2- if we make it as char so cannot assign data like that
"JAN","FEB",.....
you need only single character such as 'a','b'

Using a single character is probably not an option.
but if you need to make some thing like that you can use the type
string

ie:
#include <string>

main()
{
string Months[12] = {"JAN","FED",......}
}

This would work, but if the only use of the array is to print out, using
pointers to const char is more than enough. In addition, the string
literals are located in static memory, which means less overhead, and
probably a smaller footprint than string objects.
and for the memory use , i think the enum is less than string in
memory usage because it's refer to numbers not strings

An enum by itself uses only compiler memory. But it's a different beast. The
idea behind enum is to create related constants. One could equally well say

const int jan = 0, feb = 1, ..., dec = 11;

And as long as the address of one of the objects are never taken, these
objects does not need to use any memory.

But compared to the strings, enum values cannot be output by their symbolic
name.

If the intent is to output month names, appendix D of Bjarne Stroustrup's
The C++ Programming Language can be downloaded from
finally if you need to more save to the memory you can use

bool x[12]={1,2,3,.....};

or
bool x[12] = {true, true, true, ...};
which is the same in terms of C++.
which 1 refer to JAN and 2 refer to FEB ....

I think you need to study this more.
because bool is use only 1 byte unlike string or enum

Depends on the compiler. A bool object needs at least 1 bit, but for
performance reasons, I would imagine that using a different quantity for
bool objects would be sane. It might depend on the compiler, and its flags
and settings.

An object of some enum type needs to be large enough to hold it's largest
value. Very often, this could be one byte. Again depending on your
compiler, and its flags and settings.
 
J

James Kanze

I was just curious, because some of the older guys I work with
do the char * thing and I like the clean look of ENUM but if
we're talking performance hit (we are in an OS dept), then
maybe I change my ways.

As Alf said, they don't do anywhere near the same thing. In
particular, if you use the enum:

Months m = JAN ;
std::cout << m ;

isn't going to display anything useful. On the other, you can't
even write something like the above with the array of char
const*.

If all you're concerned about is storing the values, it will
depend on the machine and the compiler. In one case, you're
storing an enum value, in the other, a pointer. On many
machines, they have the same size. (On the other hand, it's
possible to use bit fields with the enum type, but not with the
pointer.)

In sum, both have their place. (I actually have a program
which, given the enum, will generate a table with the strings,
and mapping functions each way. I often need string
representations of enum values.)
 
J

Jim Langston

Travis said:
Virtual_X said:
Just curious, which takes less memory?
static const char *Months[12] =
{
"JAN", "FEB", "MAR",
"APR, "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"};
this method is invalid because of two reasons
1- char *Months[12] is refer to an array of pointers which consist of
12 pointer
2- if we make it as char so cannot assign data like that
"JAN","FEB",.....
you need only single character such as 'a','b'

What are you talking about? The code shown creates an array of 12
pointer to constant char, which are intialized with pointers to various
string literals. Perfectly valid, other than the OP left off one of the
" around APR.

Brian

Thank you. I didn't know what that person was talking about either.
Sorry I forgot a " near April. That's what you get for so much rain
April!

I was just curious, because some of the older guys I work with do the
char * thing and I like the clean look of ENUM but if we're talking
performance hit (we are in an OS dept), then maybe I change my ways.

It depends on how you are planning on using the months. Enumerators on my
system are stored in 4 bytes. A char pointer on my system also takes up 4
bytes. I can cast from and to enums from ints sometimes with difficulty.

Now, the problem comes in when you have some information stored in a tabke,
such as "JAN". How do you determine that that's the enum JAN? There is no
text string "JAN" in the program unless you define it elsewhere. However,
it is possible to store it as an int value, such as "0" and read it, then
cast to an enum.

I tend to use enums myself, especially when there is no user input on the
value. I store them as integers, read them as integers then cast them to
the enum.

Can enums be faster? Well, yes, it's fairly quick for the computer to
figure out if 1 == 1. Takes longer to determine if "JAN" == "JAN".

As to weather this is your bottle neck or not is very hard to say without
seeing code.

I would say, however, that without trying to determine if "JAN" == JAN (text
== enum) an enum generally will be faster. If you have to convert from text
to an enum, an enum will be slower, and more bug prone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,063
Latest member
StormyShuf

Latest Threads

Top