Re: Simple question on Pointers
On Sun, 30 Nov 2008 03:09:11 +0700, "Alan Carre" <alan@twilightgames.com>
wrote:
"Tommy" <bad@reallybad.com> wrote in message
news:O2PB1qlUJHA.5952@TK2MSFTNGP06.phx.gbl...
Alan Carre wrote:
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message I don't know what a
"char[12]" is. Perhaps you're thinking we're talking about FORTRAN or
something. There is no such thing as a char[12] as far as the C++
language is concerned.
It looks like you're attributing *your* statement and commentary to me, and
looking at Tommy's message, from which you are quoting, it's hard to
believe you didn't do this deliberately. I mean, there is selective
clipping and the joining of separate lines. In fact, while you used my
name, you didn't quote a single word I've written.
How about?
char *p = new char[12];
Hehe, astonishing! Where'd you get that MVP from?
As you've directed that at Tommy, who I don't believe is an MVP, what is
that supposed to mean?
You DO know that that is how to specify a Type* using the "new" operator
What do you mean by a "Type*"? The new-expression above does not create a
pointer; it creates an array.
and is entirely a feature of "new".
Even if you were correct, it was enough to refute your assertion that
"There is no such thing as a char[12] as far as the C++ language is
concerned." That (and several other things you've been told) should have
given you pause, but I see that it didn't. I wonder if this will?
*****
#include <iostream>
#include <typeinfo>
int main()
{
std::cout << sizeof(char[12]) << '\n';
std::cout << typeid(char[12]).name() << '\n';
}
X>cl -EHsc a.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for
80x86
Copyright (C) Microsoft Corporation. All rights reserved.
a.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:a.exe
a.obj
X>a
12
char [12]
*****
Do you see how this refutes your latest claim, that char[12] is "entirely a
feature of new"?
In this case char[12] is not a type, it is a
collection of parameters passed to the new operator.
No. It's a type that can be used in new-expressions, with sizeof, with
typeid, and possibly other contexts I haven't thought of. Oh, here's
another:
typedef char X[12];
The standard says that typedef names are synonyms for other types.
Therefore, the typedef name X must be a synonym for char[12], which must be
a type.
The way it works is like this:
T* pT = new Type[Size];
This is how we allocate an array dynamically using "new". "Type" is an
actual C++ type, and Size is a "size_t". The brackets are special indicators
which tell the compiler to insert 'secret' code allowing for the use of
delete[] (vector deleting destruction) for non-trivial types (if you go and
look at the assembler code generated you'll see it allocates some extra
space to store the size of the array, meaning that when you use delete[],
the destructors of the individual elements pointed to by pT can be called).
Why do you feel it's worthwhile to "share" that? It's not relevant, and
it's only somewhat correct. When you use new X[n], you are creating an
array, and you must use delete[]. It doesn't matter if X is a "non-trivial"
type or not, and you don't need to talk about implementation details such
as "vector deleting destructors". I could mention other restrictions on the
use of delete[], but they are not relevant, so I won't.
Would you consider std::map's use of the square bracket operator proof that
if you put a type before some square brackets and an int, that that
expression is a novel C++ type?
class NewType : public std::map<int, int > {}
NewType[13];
The above is not a type, it is an expression.
That's just very confused. If you don't think std::map<int, int> is a type,
you are profoundly mistaken. That said, it's impossible to understand what
you mean, because you start off talking about "std::map's use of the square
bracket operator", then you go on to (correctly) use angle brackets with
std::map, and finally you derive a class "NewType" from std::map for
reasons unknown and use square brackets with it. And please realize that
none of these brackets are "operators". Taken literally and simplified,
you've either said:
class X {} // Did you omit the semicolon on purpose?
X[13]; // I guess you wouldn't use a semicolon unless you meant it...
or:
class X {}; // Or did you mean this?
X[13];
The former declares an array X with the same name as the type X, which
hides the unadorned "X" from subsequent use, so you need to say "class X"
to refer to it, a strange thing to do. The latter's second statement is
simply illegal. Your claim that "The above is not a type, it is an
expression" is wrong under both of these two interpretations.
--
Doug Harrison
Visual C++ MVP