Re: Simple question on Pointers



On Sun, 30 Nov 2008 06:52:16 +0700, "Alan Carre" <alan@xxxxxxxxxxxxxxxxx>
wrote:

1. This and the stuff following is all nit-picky nagging

It was not "nagging" to point out a pointless diversion, nor was it
"nagging" by "the stuff following" to respond to a factually incorrect
statement and show you in some detail why your example made no sense.

not even correct. You don't need to use delete[] for trivial types allocated with new[].

If you look at the actual disassembly for char* pc = new char[12]; and delete[]
pc; (did I miss a semicolon Doug????) then you will see that they're
identical to scalar versions. The reason is so clear that I'm surprised YOU
never noticed: No destructors. The array length is NOT saved off for trivial
types. You can verify that easily. delete will work properly (or else please
name a compiler where it fails) because "new char[]" returns *the malloced
pointer* and not "pointer + 4".

There you go again. It is undefined to use plain old scalar delete on
pointers returned by new[] and vice versa. The C++ Standard is quite
explicit in saying that. Unlike other points of dispute, such as the notion
that std::vector was always meant to be contiguous, I'm unaware of anyone
(much less *everyone*) arguing that the allocation/deallocation rules are
to be relaxed due to de facto practice and unspoken intent. Even for those
compilers that behave as you describe, destructors are not the only issue.
It is possible to replace the operator delete[] function on a global or
per-class basis, and if you use plain old scalar delete, operator delete
will be called instead of operator delete[]. Not good, but not a problem
for those who follow the rules.

2. That you must say "typedef char X[6];" is most revealing isn't it? How
come X doesn't follow? It does for any other type, why not here? Could it be
that "typedef char[6] X;" doesn't compile? That char[6] is not an actual C++
type? It's certainly not in any *useable* way. Have you ever been able to
declare a variable list of that type? (of course *without* any typedef
hacks) You can't declare a char[6] EVER.

Yet you've been given several "*useable* ways" in which char[6] was used as
a type. As for the inability to declare a variable with that syntax, why
keep harping on something I've already agreed is not legal?

3. How do I declare an array of 6 chars:

char pc[6];

Notice how they're split up???? CAN NOBODY SEE THIS????

Everyone can, but you appear to be the only who considers it relevant.

Look, if char[6] were a type why can't I declare 10 of them on the same line
without having to introduce a typedef to insert the variable names before
the brackets for me?

char[6] a,b,c,d; // ERROR

Why? Because char[6] is not a C++ type.

Period.

I'll just repeat what I wrote to you in another message:

*****
But as demonstrated, char[12] can be used in several different places where
C++ requires a "type". Just because you can't directly declare a variable
using that syntax doesn't mean it doesn't represent a "type". If that were
true, you'd have to say that char(*)[12] isn't a "type", but in another
message, you said, "clearly I meant to say that &a has type char(*)[12]".
Your example of the "untypeness" of char[12]:

char[12] c12; // Won't compile

applies equally to:

char(*)[12] c; // Also won't compile

So it appears you're being inconsistent WRT what you consider a "type".
Perhaps more to the point, it's common practice to write char[12] instead
of the lengthier "array of 12 char" when discussing C++.
*****

HTH.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: Simple question on Pointers
    ... typedef char C12; ... As for looking at implementaion before standard, ... It is undefined to use plain old scalar delete on ... hacks) You can't declare a charEVER. ...
    (microsoft.public.vc.language)
  • Re: use constant XX => ( ...
    ... There are two syntax for the "use constant" pragma: to declare a scalar or to declare a list. ... use constant CONST => scalarvalue; ...
    (comp.lang.perl.misc)
  • Re: I dont understand typedef example
    ... Since the only use of such a typedef is to make a type "pointer to ... typedef can only be used to declare the type of return value ... ** are functions declared with a copyfunc typedef. ... a language may provide a syntax to declare a function using the ...
    (comp.lang.c)
  • Re: I dont understand typedef example
    ... all the author wanted to say is that there is no syntax in C to use a ... You *can* use a typedef to declare a function. ... returning a pointer to a funtion of this type." ...
    (comp.lang.c)
  • use constant XX => ( ...
    ... There are two syntax for the "use constant" pragma: to declare a scalar or to declare a list. ... use constant CONST => scalarvalue; ...
    (comp.lang.perl.misc)

Quantcast