Problem with array objects

P

Paul

Leigh Johnston said:
Leigh Johnston said:
On 23/03/2011 16:27, Paul wrote:
[...]
int (*p3)[dim] = &arr; /*Pointless extra level of indirection [see
note]*/
int** p4 = &arr; /*Same as above, but without typeinfo*/

Untrue; int (*p3)[dim] is not the same as int** (they are totally
unrelated types).
No they are related types. Related because:
int (*p3)[dim]; /*-- points to an array of int.---*/
int** p4; /* ---- points to an int* ---- */

The followings shows they both point to the same place:
int arr[5]={1,2,3,4,5};
int (*p)[5] = &arr;
int** pp = (int**)&arr;
std::cout<< "value of p:\t"<<p <<"\nvalue of pp:\t" <<pp;

I just forgot the cast.

Using the result of that cast is UB:

std::cout<< **pp;
If you really must know how to use it without crashing here is one way:
int* temp = (int*)*p; /* --Convert p to int* first--*/
int** pp = &temp; /* ---Assign address of p to pp---*/
will probably cause a crash.
So what it was an example to explain the extra level of indirection. If
wasn't meant to be compileable code.

I have now shown you how to do it with compileable code and the same context
still applies.
 
P

Paul

SG said:
The followings shows they both point to the same place:
int arr[5]={1,2,3,4,5};
int (*p)[5] = &arr;
int** pp = (int**)&arr;

std::cout<< "value of p:\t"<<p <<"\nvalue of pp:\t" <<pp;

I just forgot the cast.

How do you access an element of arr using pp? Is (*pp)[3] going to be
an lvalue referring to the fourth element of arr?
I have just explained to Leigh the casting is not the point its the context
of indirection I am explaining , there are different ways to properly cast
this and you are just attempting to divert the point.
 
P

Peter Remmers

Am 23.03.2011 17:27, schrieb Paul:
int arr[5] = {1,2,3,4,5};
int (*p3)[dim] =&arr; /*Pointless extra level of indirection [see note]*/
int** p4 =&arr; /*Same as above, but without typeinfo*/
It definitely is *not* the same. p1 is a pointer to a pointer to int.
That means, the memory location that p4 points to is interpreted as
another pointer value, which it isn't - it contains an int element of
arr. The moment you dereference this false pointer, you invoke UB.
int x = **p3; /*Same as (**p4) */

Not the same.
Just because you can write p4[foo][bar] does not mean it is equivalent
to arr[foo][bar].

"**p3" works because the result of the first dereference is of type
"int[dim]", and for the second asterisk, that result is first converted
to "int*" and then dereferenced.
That doesn't help.

Peter
 
P

Paul

On 23/03/2011 16:27, Paul wrote:
[...]
int (*p3)[dim] = &arr; /*Pointless extra level of indirection [see
note]*/
int** p4 = &arr; /*Same as above, but without typeinfo*/

Untrue; int (*p3)[dim] is not the same as int** (they are totally
unrelated types).

Good catch!
This is utter nonsense the types are related and can be properly converted.
As I will demonstrate:
int* ptemp = (int*)*p3; /* --Convert p3 to int* --*/
int** p4 = &ptemp;

And I was saying int** is the same level of indirection as &arr , or
int(*p)[dim].

This just proves how silly you are if you :
a) didn't realise the types can be converted
b) didn't realise I was talking about levels of indirection.


This example speaks volumes. His wrongness apparently goes
beyond terminology issues. If his mental model makes him predict that
the above code will compile then his mental model is obviously wrong.

I'm not surprized, though. He's not the first one to trip over the
rather sneaky
- array-to-pointer standard conversion (§4.2)
- function parameter type transformations (§8.3)
- template argument deduction (§14.8.2)
rules. It's one of dangers of learning C++ via trial-and-error.

Once again you fail to demonstrate anything other than obnoxious disregard
for the truth.

SG
 
P

Paul

You have a cheek posting in this newsgroup after that other subthread of
yours in this thread.

<snip>
 
P

Paul

Leigh Johnston said:
Leigh Johnston said:
On 23/03/2011 17:01, Paul wrote:

On 23/03/2011 16:27, Paul wrote:
[...]
int (*p3)[dim] = &arr; /*Pointless extra level of indirection [see
note]*/
int** p4 = &arr; /*Same as above, but without typeinfo*/

Untrue; int (*p3)[dim] is not the same as int** (they are totally
unrelated types).

No they are related types. Related because:
int (*p3)[dim]; /*-- points to an array of int.---*/
int** p4; /* ---- points to an int* ---- */

The followings shows they both point to the same place:
int arr[5]={1,2,3,4,5};
int (*p)[5] = &arr;
int** pp = (int**)&arr;
std::cout<< "value of p:\t"<<p <<"\nvalue of pp:\t" <<pp;

I just forgot the cast.

Using the result of that cast is UB:

std::cout<< **pp;
If you really must know how to use it without crashing here is one way:
int* temp = (int*)*p; /* --Convert p to int* first--*/
int** pp = &temp; /* ---Assign address of p to pp---*/
will probably cause a crash.
So what it was an example to explain the extra level of indirection. If
wasn't meant to be compileable code.

I have now shown you how to do it with compileable code and the same
context still applies.

Why don't you just admit to your mistakes rather than trying to mask them
by writing something different?

You said: int** p4 = &arr;

which was wrong; you tried to correct it with a cast but is was still
wrong.
Yes that is wrong in terms of wrong C++ code, but what i was trying to
explain was the equality in levels of indirection, which is correct.
 
P

Paul

Leigh Johnston said:
SG said:
On 23 Mrz., 18:01, Paul wrote:

The followings shows they both point to the same place:
int arr[5]={1,2,3,4,5};
int (*p)[5] = &arr;
int** pp = (int**)&arr;

std::cout<< "value of p:\t"<<p <<"\nvalue of pp:\t" <<pp;

I just forgot the cast.

How do you access an element of arr using pp? Is (*pp)[3] going to be
an lvalue referring to the fourth element of arr?
I have just explained to Leigh the casting is not the point its the
context of indirection I am explaining , there are different ways to
properly cast this and you are just attempting to divert the point.

FAIL.
THe only fail around here is you in the other thread ref: You snipped
something :)
 
P

Paul

cg_chas said:
Leigh Johnston said:
On 23/03/2011 17:01, Paul wrote:

On 23/03/2011 16:27, Paul wrote:
[...]
int (*p3)[dim] = &arr; /*Pointless extra level of indirection [see
note]*/
int** p4 = &arr; /*Same as above, but without typeinfo*/

Untrue; int (*p3)[dim] is not the same as int** (they are totally
unrelated types).

No they are related types. Related because:
int (*p3)[dim]; /*-- points to an array of int.---*/
int** p4; /* ---- points to an int* ---- */

The followings shows they both point to the same place:
int arr[5]={1,2,3,4,5};
int (*p)[5] = &arr;
int** pp = (int**)&arr;
std::cout<< "value of p:\t"<<p <<"\nvalue of pp:\t" <<pp;

I just forgot the cast.

Using the result of that cast is UB:

std::cout<< **pp;
If you really must know how to use it without crashing here is one way:
int* temp = (int*)*p; /* --Convert p to int* first--*/
int** pp = &temp; /* ---Assign address of p to pp---*/
will probably cause a crash.
So what it was an example to explain the extra level of indirection. If
wasn't meant to be compileable code.
LOL priceless. This is about as bad as making a statment such as "An
array is
just a pointer"

I really think you cannot hear yourself. Man up and stand by what you say,
Paul.
What is priceless is this:
<quote ref: http://c-faq.com/aryptr/aryptr2.html >
The pointer declaration char *p, on the other hand, requests a place which
holds a pointer, to be known by the name ``p''. This pointer can point
almost anywhere: to any char, or to any contiguous array of chars, or
nowhere .
</quote>

:)
 
P

Paul

cg_chas said:
You've quoted a snip. You are replete with failure and infinitely
regress.

Do I need to put on a raincoat for the monkey feces now?
No level of abuse can change the fact that I have proven you to be a
complete idiot here.

*shrug*
 
P

Paul

Peter Remmers said:
Am 23.03.2011 17:27, schrieb Paul:
int arr[5] = {1,2,3,4,5};
int (*p3)[dim] =&arr; /*Pointless extra level of indirection [see note]*/
int** p4 =&arr; /*Same as above, but without typeinfo*/
It definitely is *not* the same.
This is a slight error I made but has no consequence on the point i was
explaining that &arr is the same as int** in terms of levels of indirection.

I think this has already been discussed :)
 
P

Paul

cg_chas said:
All that you have proven is that you can start half a dozen threads in a
C++
newsgroup and fail to make a single coherent assertion in any of them.
No I have proven that you do not only misinterpret and misquote authorative
texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say that p(in
the following example) doesn't point to an array:
int* p = new int[4];
 
P

Paul

Leigh Johnston said:
cg_chas said:
<snip>
You can't even quote a book properly , let alone understand it.

*plonk*

You've quoted a snip. You are replete with failure and infinitely
regress.

Do I need to put on a raincoat for the monkey feces now?

No level of abuse can change the fact that I have proven you to be a
complete idiot here.

*shrug*
All that you have proven is that you can start half a dozen threads in
a C++
newsgroup and fail to make a single coherent assertion in any of them.
No I have proven that you do not only misinterpret and misquote
authorative texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say that
p(in the following example) doesn't point to an array:
int* p = new int[4];

You have been told many times that saying "p points to an array" is
technically inaccurate: p points to the initial element of an array; in
addition saying "p is a pointer to an array" is not just technically
inaccurate it is plain wrong.
You are technically innacurate, p points to an array. You're terminology is
in direct conflict with authorative experts.
<quote ref: http://c-faq.com/aryptr/aryptr2.html>
The pointer declaration char *p, on the other hand, requests a place which
holds a pointer, to be known by the name ``p''. This pointer can point
almost anywhere: to any char, or to any contiguous array of chars, or
nowhere
</quote>
 
P

Paul

A. Bolmarcich said:
No I have proven that you do not only misinterpret and misquote
authorative
texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say that
p(in
the following example) doesn't point to an array:
int* p = new int[4];

Here is paragraph 5 of section 3.4.5, about the new expression, from the
1998
C++ standard.

"When the allocated object is an array (that is, the direct-new-declarator
syntax is used or the new-type-id or type-id denotes an array type), the
new-expression yields a pointer to the initial element (if any) of the
array.
[Note: both new int and new int [10] have type int* and the type of new
int
[10] is int (*)[10].]"

According to the standard, p (in the example) doesn't point to an array,
that
is, p does not point to an object with an array type.


The standard doesn't say p doesn't point to an array. Quite the opposite, it
clearly states that "When the allocated object is an array". The object it
points to is an array of ints.
You are correct to say it doesn't point to an array-type object, this is
because a pointer to an array of n-dimensions is of type n-1 dimensions.
That is:
A pointer to a 2dim array is of type..... T (*)[dim2]
A pointer to a 1dim array is of type..... T*

Note that a pointed to (n-1) array is a 0dim array.
What p points to is an int. The value of p is the int* value returned by
"new
int[4]". That value is a pointer to the first of the four contiguous ints
that
were allocated.
A pointer of type int* can point to a single int or an array of ints. If you
don't believe that read this:

<quote ref="http://c-faq.com/aryptr/aryptr2.html">
"The pointer declaration char *p, on the other hand, requests a place which
holds a pointer, to be known by the name ``p''. This pointer can point
almost anywhere: to any char, or to any contiguous array of chars, or
nowhere "
</quote>
 
P

Paul

cg_chas said:
A. Bolmarcich said:
No I have proven that you do not only misinterpret and misquote
authorative
texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say that
p(in
the following example) doesn't point to an array:
int* p = new int[4];

Here is paragraph 5 of section 3.4.5, about the new expression, from the
1998
C++ standard.

"When the allocated object is an array (that is, the
direct-new-declarator
syntax is used or the new-type-id or type-id denotes an array type), the
new-expression yields a pointer to the initial element (if any) of the
array.
[Note: both new int and new int [10] have type int* and the type of new
int
[10] is int (*)[10].]"

According to the standard, p (in the example) doesn't point to an array,
that
is, p does not point to an object with an array type.


The standard doesn't say p doesn't point to an array.

The standard says exactly that. Of course, the section is 5.3.4 not
3.4.5, but
the quote is verbatim. No it doesn;t.
Quite the opposite, it
clearly states that "When the allocated object is an array".
Perhaps you can cite the part of the Standard that you got this quote
from?


See A. Bolmarcich's quote. Its on the very page, which is why I said it
*clearly* states this.
When I do a search for "When the allocated object is an array" the
response I
get is, "No matches were found."
I don't care what you search for TBH.
The object it
points to is an array of ints.
You are correct to say it doesn't point to an array-type object, this is
because a pointer to an array of n-dimensions is of type n-1 dimensions.
That is:
A pointer to a 2dim array is of type..... T (*)[dim2]
A pointer to a 1dim array is of type..... T*

Note that a pointed to (n-1) array is a 0dim array.
What p points to is an int. The value of p is the int* value returned
by
"new
int[4]". That value is a pointer to the first of the four contiguous
ints
that
were allocated.
A pointer of type int* can point to a single int or an array of ints. If
you
don't believe that read this:

<quote ref="http://c-faq.com/aryptr/aryptr2.html">
"The pointer declaration char *p, on the other hand, requests a place
which
holds a pointer, to be known by the name ``p''. This pointer can point
almost anywhere: to any char, or to any contiguous array of chars, or
nowhere "
</quote>

Nice try, but A. Bolmarcich's citation was correct and yours is broad at
best,
but clearly not a technical exception to the quoted Standard.

Seems like you don't know what you're talking about. TBH you say nothing but
absolute bullshit.

plonk.
 
P

Paul

cg_chas said:
We could easily skip to the end of this debate. Look what happens when
your
statement is made with too little context.

You are saying:
an int* points to an array
No I'm saying an int* can point to a single int or an array of integers.
everybody else is saying:
an int* points to an int element of an array
No everyone else is saying the same as me.
Only a few idiots in here are saying what you are saying that:
An int* points to only a single int and doesn't point to an array.
The later is not only clear and technically precise,

The latter is bullshit and totally incorrect.

<snip>
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
No I have proven that you do not only misinterpret and misquote
authorative
texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say that
p(in
the following example) doesn't point to an array:
int* p = new int[4];

Here is paragraph 5 of section 3.4.5, about the new expression, from the
1998
C++ standard.

Typo correction: it is section 5.3.4, not 3.4.5.
"When the allocated object is an array (that is, the
direct-new-declarator
syntax is used or the new-type-id or type-id denotes an array type), the
new-expression yields a pointer to the initial element (if any) of the
array.
[Note: both new int and new int [10] have type int* and the type of new
int
[10] is int (*)[10].]"

According to the standard, p (in the example) doesn't point to an array,
that
is, p does not point to an object with an array type.


The standard doesn't say p doesn't point to an array. Quite the opposite,
it
clearly states that "When the allocated object is an array". The object
it
points to is an array of ints.
You are correct to say it doesn't point to an array-type object, this is
because a pointer to an array of n-dimensions is of type n-1 dimensions.
That is:
A pointer to a 2dim array is of type..... T (*)[dim2]
A pointer to a 1dim array is of type..... T*


The standard says that p points to an int, not an array.

No it doesn't is says it points to the first element of an array, You are
changing the meaning by adding ... "not an array".. to this.
This misinterpreted meaning is compeletly wrong , see my attached quote, and
there are lots of other quotes around from people like Bjarne Stroustrup
that state the same.
Accoriding to the
standard "both new int and new int [10] have type int*".
Exaclty , the pointer type int* can point to a single int or an array of
int.
After the
initialization, p either points to an int or the value of p is the null
pointer (if the implementaion of new returns the null pointer instead of
throwing a bad_alloc exception).

The int that p points to is an element of an array. An element of an
array
is a different entity than the array.
Note that a pointed to (n-1) array is a 0dim array.

Where does the C++ standard mention array of 0 dimensions?
What p points to is an int. The value of p is the int* value returned
by
"new
int[4]". That value is a pointer to the first of the four contiguous
ints
that
were allocated.
A pointer of type int* can point to a single int or an array of ints. If
you
don't believe that read this:

<quote ref="http://c-faq.com/aryptr/aryptr2.html">
"The pointer declaration char *p, on the other hand, requests a place
which
holds a pointer, to be known by the name ``p''. This pointer can point
almost anywhere: to any char, or to any contiguous array of chars, or
nowhere "
</quote>

The faq is not a worded as strictly as a specification. What a char* can
point to is an element of an array of chars, not to an array. Trying to
have a char* point to an array of chars as with

char arr[1];
char *p = &arr;

is a type mismatch.

No its bad programming.
char* p = arr;
is how its done.

p points to an array of elements not only a single int, if you don't accept
quotes from recognised autohriities and the C++ standards I don't have
anything more to say.

See the thread titled "You missed something " for the quotes from the
standard I refer to.
 
G

Garrett Hartshaw

A. Bolmarcich said:
No I have proven that you do not only misinterpret and misquote
authorative
texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say that
p(in
the following example) doesn't point to an array:
int* p = new int[4];

Here is paragraph 5 of section 3.4.5, about the new expression, from the
1998
C++ standard.

Typo correction: it is section 5.3.4, not 3.4.5.


"When the allocated object is an array (that is, the
direct-new-declarator
syntax is used or the new-type-id or type-id denotes an array type), the
new-expression yields a pointer to the initial element (if any) of the
array.
[Note: both new int and new int [10] have type int* and the type of new
int
[10] is int (*)[10].]"

According to the standard, p (in the example) doesn't point to an array,
that
is, p does not point to an object with an array type.

The standard doesn't say p doesn't point to an array. Quite the opposite,
it
clearly states that "When the allocated object is an array". The object
it
points to is an array of ints.
You are correct to say it doesn't point to an array-type object, this is
because a pointer to an array of n-dimensions is of type n-1 dimensions.
That is:
A pointer to a 2dim array is of type..... T (*)[dim2]
A pointer to a 1dim array is of type..... T*

The standard says that p points to an int, not an array.

No it doesn't is says it points to the first element of an array, You are
changing the meaning by adding ... "not an array".. to this.

He has not changed the meaning, he simply has asserted that the Standard has
given a strict definition of "what is".

It is you that is making the assertion that the Standard also silently implies
this additional other meaning.

<pruned the rest which is predicated on Paul's false assertion>


The first element of an array *is not* an array.
A pointer to the first element of an array *is not* a pointer to an
array.
A pointer to the first element of an array *is not* an array.
A array *can be converted to* a pointer to the first element of an
array.
 
P

Paul

Garrett Hartshaw said:
No I have proven that you do not only misinterpret and misquote
authorative
texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say that
p(in
the following example) doesn't point to an array:
int* p = new int[4];

Here is paragraph 5 of section 3.4.5, about the new expression, from the
1998
C++ standard.

Typo correction: it is section 5.3.4, not 3.4.5.


"When the allocated object is an array (that is, the
direct-new-declarator
syntax is used or the new-type-id or type-id denotes an array type), the
new-expression yields a pointer to the initial element (if any) of the
array.
[Note: both new int and new int [10] have type int* and the type of new
int
[10] is int (*)[10].]"

According to the standard, p (in the example) doesn't point to an array,
that
is, p does not point to an object with an array type.

The standard doesn't say p doesn't point to an array. Quite the opposite,
it
clearly states that "When the allocated object is an array". The object
it
points to is an array of ints.
You are correct to say it doesn't point to an array-type object, this is
because a pointer to an array of n-dimensions is of type n-1 dimensions.
That is:
A pointer to a 2dim array is of type..... T (*)[dim2]
A pointer to a 1dim array is of type..... T*

The standard says that p points to an int, not an array.
No it doesn't is says it points to the first element of an array, You are
changing the meaning by adding ... "not an array".. to this.

He has not changed the meaning, he simply has asserted that the Standard has
given a strict definition of "what is".

It is you that is making the assertion that the Standard also silently implies
this additional other meaning.

<pruned the rest which is predicated on Paul's false assertion>


The first element of an array *is not* an array.

The first element of an array is an element of an array. Nobody said it was
an array.
A pointer to the first element of an array *is not* a pointer to an array.
Yes it is. How can it not point to the array if it points to an element
within the array?
A pointer to the first element of an array *is not* an array.
Nobody said it was. Its a pointer to an array.
A array *can be converted to* a pointer to the first element of an array.
An array is a contiguous array of elements in memory, It can be "converted"
to almost anything.

If you cannot understand that an pointer to an element within an array also
points to the array, then you need to give up programming.

plonk
 
B

Bo Persson

A. Bolmarcich said:
No I have proven that you do not only misinterpret and misquote
authorative texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say
that p(in the following example) doesn't point to an array:
int* p = new int[4];

Here is paragraph 5 of section 3.4.5, about the new expression,
from the 1998 C++ standard.

"When the allocated object is an array (that is, the
direct-new-declarator syntax is used or the new-type-id or type-id
denotes an array type), the new-expression yields a pointer to the
initial element (if any) of the array. [Note: both new int and new
int [10] have type int* and the type of new int [10] is int
(*)[10].]"

According to the standard, p (in the example) doesn't point to an
array, that is, p does not point to an object with an array type.

What p points to is an int. The value of p is the int* value
returned by "new int[4]". That value is a pointer to the first of
the four contiguous ints that were allocated.


Paul is right and everybody else is wrong, including the standard.

Live with it! :)


Bo Persson
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top