'const' saves space??

Q

qazmlp

As I understand, there will be 100 copies of "myTestClass", when
CLASSNAME is declared as below:
#define CLASSNAME "myTestClass"

But, there will be only 1 copy of "myTestClass", when
CLASSNAME is declared as below:
const char* CLASSNAME = "myTestClass" ;

Is my understanding correct ? Isn't that an advantage of 'const'
over '#define' declaration for constants?
 
P

Pieter Droogendijk

On 9 Aug 2003 03:21:14 -0700
As I understand, there will be 100 copies of "myTestClass", when
CLASSNAME is declared as below:
#define CLASSNAME "myTestClass"

No. "myTestClass" will exist once, and all literals will be translated to a
pointer to that address.

printf ("%d\n", "foo" - "foo");
will print 0.
 
J

Jakob Bieling

Pieter Droogendijk said:
On 9 Aug 2003 03:21:14 -0700


No. "myTestClass" will exist once, and all literals will be translated to a
pointer to that address.

printf ("%d\n", "foo" - "foo");
will print 0.


Will the Standard ensure that this is 0 or does it just be 0 on some
implementation(s)?

regards
 
S

Simon Saunders

On 9 Aug 2003 03:21:14 -0700


No. "myTestClass" will exist once, and all literals will be translated to a
pointer to that address.

printf ("%d\n", "foo" - "foo");
will print 0.

Not necessarily. Some compilers do this by default, others support it as
an optional feature, while others don't support it at all.
 
P

Pieter Droogendijk

Not necessarily. Some compilers do this by default, others support it as
an optional feature, while others don't support it at all.

I see. Thanks. I guess this is OT after all...
 
E

E. Robert Tisdale

qazmlp said:
As I understand, there will be 100 copies of "myTestClass"
when CLASSNAME is declared as below:
#define CLASSNAME "myTestClass"

But there will be only 1 copy of "myTestClass"
when CLASSNAME is declared as below:
const char* CLASSNAME = "myTestClass" ;

Is my understanding correct? Isn't that an advantage of 'const'
over '#define' declaration for constants?


GCC(1) GNU GCC(1)

NAME
gcc - GNU project C and C++ compiler
 
D

Dan Pop

In said:
As I understand, there will be 100 copies of "myTestClass", when
CLASSNAME is declared as below:
#define CLASSNAME "myTestClass"

Where did you get that understanding from? There *may* be 100 copies,
but there may be only one. It's up to the compiler to decide.
But, there will be only 1 copy of "myTestClass", when
CLASSNAME is declared as below:
const char* CLASSNAME = "myTestClass" ;

In principle. There is nothing preventing the compiler from generating
as many copies as it likes to.
Is my understanding correct ? Isn't that an advantage of 'const'
over '#define' declaration for constants?

Not in C, where you can write, at file scope:

#define CLASSNAME "myTestClass"
const char *classname = CLASSNAME;

but you cannot write:

const char *CLASSNAME = "myTestClass";
const char *classname = CLASSNAME;

const cannot replace #define in C: it's not part of the common subset of
C and C++, despite being a feature of both languages.

Then again, you would have known this, if you bothered to read the c.l.c
FAQ before (cross)posting.

Dan
 
D

Dan Pop

In said:
I see. Thanks. I guess this is OT after all...

Nope, it ain't. The exact behaviour of your compiler is OT, but not the
fact that this behaviour is compiler dependent.

Most commercial compilers don't optimise identical string literals (by
default, at least), in order to avoid surprising the user. For the same
reason, they don't make them read only. This was the documented behaviour
of pre-ANSI C.

gcc does the opposite: by default, string literals are read only and
optimised. If you make them writable, with -fwritable-strings, they are
no longer optimised.

Dan
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top