Discussion:
Enum vs Char * array
(too old to reply)
Travis
2007-07-12 23:06:09 UTC
Permalink
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"
};
Virtual_X
2007-07-12 23:26:50 UTC
Permalink
Post by Travis
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
Default User
2007-07-12 23:51:29 UTC
Permalink
Post by Virtual_X
Post by Travis
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
Travis
2007-07-12 23:56:58 UTC
Permalink
Post by Default User
Post by Virtual_X
Post by Travis
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.
James Kanze
2007-07-13 08:27:10 UTC
Permalink
Post by Travis
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.)

--
James Kanze (GABI Software) email:***@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Marcus Kwok
2007-07-13 14:14:46 UTC
Permalink
Post by James Kanze
(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.)
For those curious on how to do this, here is a page that gives a few
approaches:

http://www.comeaucomputing.com/techtalk/#enumtostring
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jim Langston
2007-07-13 09:19:25 UTC
Permalink
Post by Travis
Post by Default User
Post by Virtual_X
Post by Travis
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.
Robert Bauck Hamar
2007-07-13 00:27:10 UTC
Permalink
Post by Virtual_X
Post by Travis
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.
Post by Virtual_X
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.
Post by Virtual_X
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.
Post by Virtual_X
but if you need to make some thing like that you can use the type
string
#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.
Post by Virtual_X
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
<URL:http://www.research.att.com/~bs/3rd_loc.pdf>. I suggest to the OP that
he looks in this.
Post by Virtual_X
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++.
Post by Virtual_X
which 1 refer to JAN and 2 refer to FEB ....
I think you need to study this more.
Post by Virtual_X
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.
--
rbh
Alf P. Steinbach
2007-07-12 23:51:39 UTC
Permalink
Post by 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).
Post by Travis
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?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Default User
2007-07-12 23:53:33 UTC
Permalink
Post by 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"
};
Implementation-specific. Probably the enum.

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





Brian
Continue reading on narkive:
Loading...