473,624 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum vs Char * array

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"
};

Jul 12 '07 #1
7 8274
On Jul 12, 4:06 pm, Travis <travis.bow...@ gmail.comwrote:
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

Jul 12 '07 #2
Virtual_X wrote:
On Jul 12, 4:06 pm, Travis <travis.bow...@ gmail.comwrote:
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
Jul 12 '07 #3
Travis wrote:
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
Jul 12 '07 #4
On Jul 12, 4:51 pm, "Default User" <defaultuse...@ yahoo.comwrote:
Virtual_X wrote:
On Jul 12, 4:06 pm, Travis <travis.bow...@ gmail.comwrote:
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.

Jul 13 '07 #5
On Jul 13, 1:56 am, Travis <travis.bow...@ gmail.comwrote:
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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 13 '07 #6
"Travis" <tr***********@ gmail.comwrote in message
news:11******** **************@ n60g2000hse.goo glegroups.com.. .
On Jul 12, 4:51 pm, "Default User" <defaultuse...@ yahoo.comwrote:
>Virtual_X wrote:
On Jul 12, 4:06 pm, Travis <travis.bow...@ gmail.comwrote:
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.
Jul 13 '07 #7
James Kanze <ja*********@gm ail.comwrote:
(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
Jul 13 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11968
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to convert between data types, but was thinking that this isn't really necessary since I can access the memory directly in c. I want to treat the text as native integer so that I can perform fast bit-level manipulations. This code works fine...but I'm...
11
9849
by: Ger | last post by:
Hi, I have been trying to return a data packet (as a char array) at the end of a function but that seems to be impossible... I would like to use this char array in a different class..anyone some ideas? greetz, Ger
3
1980
by: s.subbarayan | last post by:
Dear all, I encountered the following piece of program: #include <stdio.h> void strprint(char *str); void main() { char *spr="hello"; strprint(spr); }
15
34582
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
15
435
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; for(int i=0; i<line->get_length(); i++) {
5
2114
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
4
7054
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills. thanks. I have this, but I get the index was out of bounds.... char char1 =
13
6236
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things get me, which brings me to... I'm trying to create a program that gets a string from standard input and then manipulates it a little bit. It has to be a char array and not use string from the library. Here are my prototypes:
8
16241
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc = {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
5
3787
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.