S
s
Can I do this:
#define MYSTRING "ABC"
..
..
..
char mychar = MYSTRING[0];
..
..
..
-Thanks,
s
#define MYSTRING "ABC"
..
..
..
char mychar = MYSTRING[0];
..
..
..
-Thanks,
s
Hellos said:Can I do this:
#define MYSTRING "ABC"
.
char mychar = MYSTRING[0];
-Thanks,
s
s said:Can I do this:
#define MYSTRING "ABC"
.
.
.
char mychar = MYSTRING[0];
.
.
.
-Thanks,
s
I would suggest using:
const char* const MYSTRING = "ABC";
const char mystring[] = "ABC";
s said:Can I do this:
#define MYSTRING "ABC"
.
.
.
char mychar = MYSTRING[0];
s said:Can I do this:
#define MYSTRING "ABC"
.
.
.
char mychar = MYSTRING[0];
.
.
.
-Thanks,
s
s said:Can I do this:
#define MYSTRING "ABC"
.
.
.
char mychar = MYSTRING[0];
.
Ioannis Vranos said:Avoid macros completely.
Christopher Benson-Manica said:const char mystring[] = "ABC";
Are there any situations where the behavior of these two constructs
differs?
David said:I would suggest using:
const char* const MYSTRING = "ABC";
Why the gratuitous pointer?
const char mystring[] = "ABC";
Christopher Benson-Manica said:Not all macros are evil.
Christopher Benson-Manica said:David Harmon said:const char* const MYSTRING = "ABC";const char mystring[] = "ABC";
Are there any situations where the behavior of these two constructs
differs?
Behaviour no, but implementation yes. "ABC" is stored in a separate space
reserved from the implementation, while the pointer MYSTRING and the array
mystring with all its members are stored in the stack.
David Harmon said:On Fri, 16 Apr 2004 19:07:38 +0300 in comp.lang.c++, "Ioannis Vranos"
David Harmon <[email protected]> spoke thus:
const char* const MYSTRING = "ABC";
const char mystring[] = "ABC";
Are there any situations where the behavior of these two constructs
differs?
Behaviour no, but implementation yes. "ABC" is stored in a separate space
reserved from the implementation, while the pointer MYSTRING and the array
mystring with all its members are stored in the stack.
Sorry, but no. There is no "stack" at the point in namespace or file
scope where those declarations are presumably found.
It could only be
on the stack if it were written local to a function, in which case I
would have written it
static const char mystring[] = "ABC";
Unfortunately, "static" means different things at function scope and at
file scope.
Either way it should be merely a name referring to the same
constant area that the literal does occupy in the various other idioms.
Putting it on the stack would be very bad, calling for a copy to be made
from constant area every time you use it.
The possible extra level of indirection, and possible wasted space for
a pointer variable that was not called for in the original #define
example, is a mistake and a red herring and should never have been
introduced into the discussion. But, if it was at function scope, Pete
would presumably have declared it "static" also, and still have nothing
extra on the stack, I hope.
Ioannis Vranos said:You are wrong. static variables inside a function scope are still created in
the stack.
At first, i assumed the definition takes place in a local scope and not in
the global or a namespace scope.
But except of that, where do you think global "non-static" variables are
created?
It could only be
on the stack if it were written local to a function, in which case I
would have written it
static const char mystring[] = "ABC";
There would be no point for this. The time cost for creating this thing is a
joke. (Even the compiler may optimise it entirely out).
Yes, and instead of using static keyword in the global scope it is better to
use an anonymous namespace.
You are wrong. static variables inside a function scope are still created in
the stack.
David Harmon said:On Fri, 16 Apr 2004 20:46:22 +0300 in comp.lang.c++, "Ioannis Vranos"
I assumed otherwise, since I have seen some huge number of such #define
constants in C code, and a somewhat lesser number of const strings in
C++ code, and the vast majority of them were near the top of the file
before any functions, if not in headers included near the top of the
file before any functions.
static const char mystring[] = "ABC";
There would be no point for this. The time cost for creating this thing is a
joke. (Even the compiler may optimise it entirely out).
No point for what? There is certainly a point for putting "static" on
the declaration of that, at function scope, however small the cost of
omitting it on a string that happens to be very short.
No, not this time. Static variables wouldn't be static if they were on
the stack. If they were on the stack, where do you think their values
would be preserved from one call of the function to the next?
To illustrate, here is an compilable example:
const char* const MYSTRING = "ABC";
const char mystring[] = "ABC";
int main()
{
const char * cptr;
cptr = mystring; // line 6
cptr = MYSTRING; // line 7
static const char mystring[] = "ABC";
cptr = mystring; // line 9
static const char* const MYSTRING = "ABC";
cptr = MYSTRING; // line 11
}
I compiled that with MSVC 6.0 with default no optimization options (for
most straightforward translation) and /Fa for assembly listing output.
Here is the generated initialized data. You will notice the extra DD
storage for the two pointers, and that both of the "static" local
variables with the mangled names are in compile-time initialized
constant memory, not on the stack.
CONST SEGMENT
_MYSTRING DD FLAT:$SG265
_mystring DB 'ABC', 00H
_?mystring@?1??main@@9@4QBDB DB 'ABC', 00H
_?MYSTRING@?1??main@@9@4QBDB DD FLAT:$SG275
CONST ENDS
_DATA SEGMENT
$SG265 DB 'ABC', 00H
$SG275 DB 'ABC', 00H
_DATA ENDS
Now here is the MSVC generated code from the body of main(). Please
notice that the only variable on the stack is the non-static "cptr".
At no time does the code copy the characters "ABC\0" to the stack from
somewhere else.
Christopher said:...const char mystring[] = "ABC";
Are there any situations where the behavior of these two constructs
differs?
[snip]
In this case, i wouldn't use an array myself but a const char * const, but
never mind. If there isn't a specific advantage for making it static, why
should one make it of static storage?
No, not this time. Static variables wouldn't be static if they were on
the stack. If they were on the stack, where do you think their values
would be preserved from one call of the function to the next?
And where they get stored? As far as i know it is implementation-dependent
but i think the usual is stack.
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.