Post by Virtual_XPost by TravisJust 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_X1- 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_X2- 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_Xbut 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_Xand 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_Xfinally 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_Xwhich 1 refer to JAN and 2 refer to FEB ....
I think you need to study this more.
Post by Virtual_Xbecause 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