Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > An array is just a pointer

Reply
Thread Tools

An array is just a pointer

 
 
Paul
Guest
Posts: n/a
 
      03-17-2011
Hi
I have this array:

int (*array)[4] = new int[4][4];
++array;
array[-1][3] = 4;

Is this an array? Or is it not an array?

Also I have another array:

int* arr1 = new int[12];
arr1[0] = 33;
int* arr2 = new(++arr1) int[11];
std::cout<< arr2[-1];


Is this an array or is it just a pointer?
Or as Noah says its a pointer that doesn't even point to an array. Surely
this is pushing the limits of plain idiocy.


 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      03-17-2011
Leigh Johnston <(E-Mail Removed)> wrote:
> On 17/03/2011 20:28, Paul wrote:
>> Hi
>> I have this array:
>>
>> int (*array)[4] = new int[4][4];
>> ++array;
>> array[-1][3] = 4;
>>
>> Is this an array? Or is it not an array?

>
> It is not an array; it is a pointer to an array.


In fact, it's just a pointer to some memory location. The language
makes no difference whether that memory location contains one object
or several objects at contiguous memory locations. (Well, with one
single exception: delete[] makes the distinction and is able to resolve
how many objects there are. But other than this there is no way to
distinguish whether the pointer is pointing to a single object, to an
array, or even to an invalid memory location.)

You can jump forwards and backwards with the pointer (either by using
the indexing syntax or operator+, it doesn't really make much of a
difference in terms of functionality, only in terms of syntax), but
there is no guarantee that it will work correctly, if the pointer ends
up pointing to an invalid location.

This behavior comes from C, to which in turn it comes from machine code:
In most CPU architectures (especially the ones C was designed for) the
CPU doesn't make any distinction between memory addresses pointing to
single objects or arrays either. It's up to the code to do things
correctly; the CPU won't do any checks (except for segment violations,
if the architecture's memory model supports that).
 
Reply With Quote
 
 
 
 
Peter Remmers
Guest
Posts: n/a
 
      03-17-2011
Am 17.03.2011 21:28, schrieb Paul:
> Hi
> I have this array:
>
> int (*array)[4] = new int[4][4];


array is a pointer to an array of 4 ints.
That means what it points to is not just one integer but 4 integers.
That means that if you do
> ++array;

it then points to an address that is 4*sizeof(int) higher, not just
1*sizeof(int).

> array[-1][3] = 4;

Assuming these 4*4 ints are contiguous im memory, without padding, then
that assignment writes a 4 to the top-right corner of the matrix.

> Is this an array? Or is it not an array?

There is an unnamed array of 4*4 integers on the heap. And all you have
is a pointer named "array" of type "int (*)[4]" to the first 4 integers.

> Also I have another array:
>
> int* arr1 = new int[12];
> arr1[0] = 33;
> int* arr2 = new(++arr1) int[11];

This is actually only harmless because these are integers. It's (in this
case) equivalent to
int *arr2 = ++arr1;

However, if the elements were of some complex datatype, something that
has a constructor and destructor, it would be UB I guess, because you
construct new instances at memory locations where other instances
already live, without destroying those first.

> std::cout<< arr2[-1];

Well, as arr2 is just a pointer you can offset it by any number you
want, provided you don't end up accessing memory you shouldn't touch. It
works in this case because it points to the second element in the array.

int arr3[12];
std::cout<< arr3[-1];

As arr3 can be implicitly converted to a pointer, you can offset it by
any number you want, but the difference is that in this case it is
pretty certain that you're accessing a memory location you shouldn't be
touching.

> Is this an array or is it just a pointer?
> Or as Noah says its a pointer that doesn't even point to an array. Surely
> this is pushing the limits of plain idiocy.
>
>


Peter
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-17-2011
Leigh Johnston <(E-Mail Removed)> wrote:
> Not quite true: the type of "array" is "int (*)[4]" not "int *" so it is
> both a pointer and a pointer to an array just as "int *" is both a
> pointer and a pointer to a scalar.


Technically speaking only in the sense of how indexing the pointer
changes its value (iow. how big of a jump it takes when you add 1 to it).
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-17-2011

"Leigh Johnston" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> On 17/03/2011 20:51, Juha Nieminen wrote:
>> Leigh Johnston<(E-Mail Removed)> wrote:
>>> On 17/03/2011 20:28, Paul wrote:
>>>> Hi
>>>> I have this array:
>>>>
>>>> int (*array)[4] = new int[4][4];
>>>> ++array;
>>>> array[-1][3] = 4;
>>>>
>>>> Is this an array? Or is it not an array?
>>>
>>> It is not an array; it is a pointer to an array.

>>
>> In fact, it's just a pointer to some memory location. The language
>> makes no difference whether that memory location contains one object
>> or several objects at contiguous memory locations. (Well, with one

>

No it's not the standards states that when the typeid denotes an array type,
the newexpression
yields a pointer to the initial element of the array.

This means that
int* x = new int[12];

Yields a pointer to an array. Not to *any* memory location.


> Not quite true: the type of "array" is "int (*)[4]" not "int *" so it is
> both a pointer and a pointer to an array just as "int *" is both a pointer
> and a pointer to a scalar.
>

Leigh seems to think that int* cannot be a pointer to an array but that is
in direct conflict with the C++ standards.

Also note how stupid his argument is with the following code:

void foo(int p[]){
std::cout<< p << typeid(p).name();
}

int main()
{
int arr[];
foo("Is this an array?" );
}


Is an array passed to foo, has an array been processed by cout? Of course it
has. See the C++ standards to confirm the definition of string literals and
character arrays.
It's quite apparent some people around here cannot understand the very
basics about C++ arrays.






 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      03-17-2011
On 3/17/2011 1:51 PM, Juha Nieminen wrote:
> Leigh Johnston<(E-Mail Removed)> wrote:
>> On 17/03/2011 20:28, Paul wrote:
>>> Hi
>>> I have this array:
>>>
>>> int (*array)[4] = new int[4][4];
>>> ++array;
>>> array[-1][3] = 4;
>>>
>>> Is this an array? Or is it not an array?

>>
>> It is not an array; it is a pointer to an array.

>
> In fact, it's just a pointer to some memory location. The language
> makes no difference whether that memory location contains one object
> or several objects at contiguous memory locations. (Well, with one
> single exception: delete[] makes the distinction and is able to resolve
> how many objects there are. But other than this there is no way to
> distinguish whether the pointer is pointing to a single object, to an
> array, or even to an invalid memory location.)


That exception is even more of an exception than that. The pointer
passed to delete[] had darn well better be the same one returned by
new[]. The little ++array thing in the example code had better be
undone or delete[] is going to totally freak out.

The delete[] operator in fact doesn't actually know if the pointer
points at an array or not any better than we can discover, it just
assumes so and tries to fetch some implementation defined information
using the supplied address as a key. If that address isn't in the
'database' then delete[] is going to misbehave.

--
http://crazycpp.wordpress.com
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-17-2011

"Peter Remmers" <(E-Mail Removed)> wrote in message
news:4d827ef5$0$6766$(E-Mail Removed)-online.net...
> Am 17.03.2011 21:28, schrieb Paul:
>> Hi
>> I have this array:
>>
>> int (*array)[4] = new int[4][4];

>
> array is a pointer to an array of 4 ints.
> That means what it points to is not just one integer but 4 integers. That
> means that if you do
>> ++array;

> it then points to an address that is 4*sizeof(int) higher, not just
> 1*sizeof(int).
>
>> array[-1][3] = 4;

> Assuming these 4*4 ints are contiguous im memory, without padding, then
> that assignment writes a 4 to the top-right corner of the matrix.
>
>> Is this an array? Or is it not an array?

> There is an unnamed array of 4*4 integers on the heap. And all you have is
> a pointer named "array" of type "int (*)[4]" to the first 4 integers.
>
>> Also I have another array:
>>
>> int* arr1 = new int[12];
>> arr1[0] = 33;
>> int* arr2 = new(++arr1) int[11];

> This is actually only harmless because these are integers. It's (in this
> case) equivalent to
> int *arr2 = ++arr1;
>
> However, if the elements were of some complex datatype, something that has
> a constructor and destructor, it would be UB I guess, because you
> construct new instances at memory locations where other instances already
> live, without destroying those first.
>
>> std::cout<< arr2[-1];

> Well, as arr2 is just a pointer you can offset it by any number you want,
> provided you don't end up accessing memory you shouldn't touch. It works
> in this case because it points to the second element in the array.
>
> int arr3[12];
> std::cout<< arr3[-1];
>
> As arr3 can be implicitly converted to a pointer, you can offset it by any
> number you want, but the difference is that in this case it is pretty
> certain that you're accessing a memory location you shouldn't be touching.
>
>> Is this an array or is it just a pointer?
>> Or as Noah says its a pointer that doesn't even point to an array. Surely
>> this is pushing the limits of plain idiocy.
>>
>>

>

You don't seem to be clear, maybe you don't like committing yourself to say
if it is an array or if its not. Let me explain further:

void foo(char p[]){std::cout<< p;}

foo("Is this an array?");


Is an array passed to the function or not? Is an array processed by cout or
not?
An array is a pointer in most situations and it's complete nonsense to
suggest its not an array because its a pointer. That's my opinion, I don't
know yours because you didn't make your opinion too clear.
As for Leigh and co's opinion about arrays not being arrays because they are
pointers, well that's obviously complete nonsense, I hope you don't share
their opinion :-S



 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-17-2011

"Leigh Johnston" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 17/03/2011 21:45, Paul wrote:
>>
>> "Leigh Johnston" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ...
>>> On 17/03/2011 20:51, Juha Nieminen wrote:
>>>> Leigh Johnston<(E-Mail Removed)> wrote:
>>>>> On 17/03/2011 20:28, Paul wrote:
>>>>>> Hi
>>>>>> I have this array:
>>>>>>
>>>>>> int (*array)[4] = new int[4][4];
>>>>>> ++array;
>>>>>> array[-1][3] = 4;
>>>>>>
>>>>>> Is this an array? Or is it not an array?
>>>>>
>>>>> It is not an array; it is a pointer to an array.
>>>>
>>>> In fact, it's just a pointer to some memory location. The language
>>>> makes no difference whether that memory location contains one object
>>>> or several objects at contiguous memory locations. (Well, with one
>>>

>> No it's not the standards states that when the typeid denotes an array
>> type, the newexpression
>> yields a pointer to the initial element of the array.
>>
>> This means that
>> int* x = new int[12];
>>
>> Yields a pointer to an array. Not to *any* memory location.

>
> x is a pointer to a scalar not a pointer to an array; the scalar just
> happens to be the first element of an array.
>

You are wrong to say that its not a pointer to the array ref C++ standards:
"the newexpression yields a pointer to the initial element (if any) of the
array."

>>
>>
>>> Not quite true: the type of "array" is "int (*)[4]" not "int *" so it
>>> is both a pointer and a pointer to an array just as "int *" is both a
>>> pointer and a pointer to a scalar.
>>>

>> Leigh seems to think that int* cannot be a pointer to an array but that
>> is in direct conflict with the C++ standards.

>
> I see no conflict with the C++ standard; int* is a pointer to an int
> scalar not a pointer to an array.
>

See above.

>> Also note how stupid his argument is with the following code:
>>
>> void foo(int p[]){
>> std::cout<< p << typeid(p).name();
>> }
>>
>> int main()
>> {
>> int arr[];
>> foo("Is this an array?" );
>> }
>>
>>
>> Is an array passed to foo, has an array been processed by cout? Of
>> course it has. See the C++ standards to confirm the definition of string
>> literals and character arrays.

>
> WTF are you talking about? Your code is nonsense (it won't compile). The
> type of a string literal is an array; nobody said otherwise.
>

Obviously the function parameter should be char p[]. SO I don't proof read
posting to zonks like you.

>> It's quite apparent some people around here cannot understand the very
>> basics about C++ arrays.

>
> It is quite apparent to everyone who is lacking a clue here.
>

Yes you.

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-17-2011

"Leigh Johnston" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> On 17/03/2011 22:10, Paul wrote:
>>
>> "Leigh Johnston" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 17/03/2011 21:45, Paul wrote:
>>>>
>>>> "Leigh Johnston" <(E-Mail Removed)> wrote in message
>>>> news:(E-Mail Removed) ...
>>>>> On 17/03/2011 20:51, Juha Nieminen wrote:
>>>>>> Leigh Johnston<(E-Mail Removed)> wrote:
>>>>>>> On 17/03/2011 20:28, Paul wrote:
>>>>>>>> Hi
>>>>>>>> I have this array:
>>>>>>>>
>>>>>>>> int (*array)[4] = new int[4][4];
>>>>>>>> ++array;
>>>>>>>> array[-1][3] = 4;
>>>>>>>>
>>>>>>>> Is this an array? Or is it not an array?
>>>>>>>
>>>>>>> It is not an array; it is a pointer to an array.
>>>>>>
>>>>>> In fact, it's just a pointer to some memory location. The language
>>>>>> makes no difference whether that memory location contains one object
>>>>>> or several objects at contiguous memory locations. (Well, with one
>>>>>
>>>> No it's not the standards states that when the typeid denotes an array
>>>> type, the newexpression
>>>> yields a pointer to the initial element of the array.
>>>>
>>>> This means that
>>>> int* x = new int[12];
>>>>
>>>> Yields a pointer to an array. Not to *any* memory location.
>>>
>>> x is a pointer to a scalar not a pointer to an array; the scalar just
>>> happens to be the first element of an array.
>>>

>> You are wrong to say that its not a pointer to the array ref C++
>> standards:
>> "the newexpression yields a pointer to the initial element (if any) of
>> the array."

>
> It is quite simple: there is a difference between "pointer to an array"
> and "pointer to the first element of an array"; you need to engage your
> brain.
>

Err no "a pointer to the first element of an array" means exactly the same
thing as "a pointer to an array".

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-17-2011

"Leigh Johnston" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> On 17/03/2011 22:14, Leigh Johnston wrote:
>> On 17/03/2011 22:10, Paul wrote:
>>>
>>> "Leigh Johnston" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> On 17/03/2011 21:45, Paul wrote:
>>>>>
>>>>> "Leigh Johnston" <(E-Mail Removed)> wrote in message
>>>>> news:(E-Mail Removed) ...
>>>>>> On 17/03/2011 20:51, Juha Nieminen wrote:
>>>>>>> Leigh Johnston<(E-Mail Removed)> wrote:
>>>>>>>> On 17/03/2011 20:28, Paul wrote:
>>>>>>>>> Hi
>>>>>>>>> I have this array:
>>>>>>>>>
>>>>>>>>> int (*array)[4] = new int[4][4];
>>>>>>>>> ++array;
>>>>>>>>> array[-1][3] = 4;
>>>>>>>>>
>>>>>>>>> Is this an array? Or is it not an array?
>>>>>>>>
>>>>>>>> It is not an array; it is a pointer to an array.
>>>>>>>
>>>>>>> In fact, it's just a pointer to some memory location. The language
>>>>>>> makes no difference whether that memory location contains one object
>>>>>>> or several objects at contiguous memory locations. (Well, with one
>>>>>>
>>>>> No it's not the standards states that when the typeid denotes an array
>>>>> type, the newexpression
>>>>> yields a pointer to the initial element of the array.
>>>>>
>>>>> This means that
>>>>> int* x = new int[12];
>>>>>
>>>>> Yields a pointer to an array. Not to *any* memory location.
>>>>
>>>> x is a pointer to a scalar not a pointer to an array; the scalar just
>>>> happens to be the first element of an array.
>>>>
>>> You are wrong to say that its not a pointer to the array ref C++
>>> standards:
>>> "the newexpression yields a pointer to the initial element (if any) of
>>> the array."

>>
>> It is quite simple: there is a difference between "pointer to an array"
>> and "pointer to the first element of an array"; you need to engage your
>> brain.
>>

>
> Maybe the following will help you understand the semantic differences
> involved:
>
> int main()
> {
> int* p1 = new int[42]; // 'p1' is a pointer to the first element of an
> array
> int (*p2)[42] = new int[1][42]; // 'p2' is a pointer to an array
> }
>

This is a clear indication of how screwed up you are. Please read the
following :

"the newexpression 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[i][10] is
int (*)[10]. ]"


The standards define both of these types to be pointers to the array, they
are just different *types* of pointer.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments