Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to an array -Is there an out of bound access?

Reply
Thread Tools

pointer to an array -Is there an out of bound access?

 
 
Kavya
Guest
Posts: n/a
 
      10-28-2006
int main (){

int a[][3]={{1,2,3},{4,5,6}};
int (*ptr)[3]=a;

/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);

++ptr;

/* This should be fine and give 6 as output*/
printf("%d\n",(*ptr)[2]);

return 0;
}

But what if I do something like this

int main (){

int a[][3]={{1,2,3},{4,5,6}};
int (*ptr)[3]=a;

/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);

/* Will this be fine??
printf("%d\n",(*ptr)[5]);
return 0;
}

Is the second printf call in second code fine? or is it out of bound
access?
I tried both the codes with gcc 3.4.5 with -Wall and -pedantic. There
was no warning and got the correct output as 3 and 6 in both codes.

 
Reply With Quote
 
 
 
 
Cong Wang
Guest
Posts: n/a
 
      10-28-2006


On Oct 28, 11:43 am, "Kavya" <(E-Mail Removed)> wrote:
> int main (){
>
> int a[][3]={{1,2,3},{4,5,6}};
> int (*ptr)[3]=a;
>
> /* This should be fine and give 3 as output*/
> printf("%d\n",(*ptr)[2]);
>
> ++ptr;
>
> /* This should be fine and give 6 as output*/
> printf("%d\n",(*ptr)[2]);
>
> return 0;
>
> }But what if I do something like this
>
> int main (){
>
> int a[][3]={{1,2,3},{4,5,6}};
> int (*ptr)[3]=a;
>
> /* This should be fine and give 3 as output*/
> printf("%d\n",(*ptr)[2]);
>
> /* Will this be fine??
> printf("%d\n",(*ptr)[5]);
> return 0;
>
> }Is the second printf call in second code fine? or is it out of bound
> access?
> I tried both the codes with gcc 3.4.5 with -Wall and -pedantic. There
> was no warning and got the correct output as 3 and 6 in both codes.


It's out.
I also use gcc and got the following:

3
-1074239864

.. In fact, 'ptr' is NOT a common int pointer. See the following:

int *p = (int*)a;
printf("%d\n", p[5]);

That's OK, because 'p' is a common int pointer.

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      10-28-2006
Kavya:

> int main (){
>
> int a[][3]={{1,2,3},{4,5,6}};



Equivalent to:

int a[2][3] = {{1,2,3},{4,5,6}};


> int (*ptr)[3]=a;



Equivalent to:

int (*ptr)[3] = &a[0];


> /* This should be fine and give 3 as output*/
> printf("%d\n",(*ptr)[2]);



Equivalent to:

printf("%d\n", a[0][2]);


> ++ptr;



Equivalent to:

ptr = &a[1];


> /* This should be fine and give 6 as output*/
> printf("%d\n",(*ptr)[2]);



Equivalent to:

printf("%d\n", a[1][2]);


> return 0;
> }



That works fine.


> But what if I do something like this
>
> int main (){
>
> int a[][3]={{1,2,3},{4,5,6}};
> int (*ptr)[3]=a;
>
> /* This should be fine and give 3 as output*/
> printf("%d\n",(*ptr)[2]);
>
> /* Will this be fine??
> printf("%d\n",(*ptr)[5]);



Basically you're asking if the Standard necessitates that multi-dimensional
arrays be lain out in a particular way in memory, specifically:

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

I'm pretty sure that things have to be like this, but maybe someone can
give a quote from the Standard proving or disproving the necessity.

--

Frederick Gotham
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-28-2006
Frederick Gotham:

> Basically you're asking if the Standard necessitates that
> multi-dimensional arrays be lain out in a particular way in memory,
> specifically:
>
> a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
>
> I'm pretty sure that things have to be like this, but maybe someone can
> give a quote from the Standard proving or disproving the necessity.



I actually, if I put a tad bit of thought into it, I don't need to consult
the Standard.

We know that:

(1) There cannot be padding between array elements or after.
(2) Each consecutive array element must have ascending addresses.

Also, there's no such thing as multi-dimensional arrays in C, but rather
arrays of arrays. Therefore, looking at the first array:

int (a[2])[3];

We know that the three elements in this array must not have padding between
them, and that their addresses must be ascending. Now, if we look at the
array type, we see that each element is an int[2]. We know that this inner
array must also have no padding between elements, and also that the
addresses must be ascending.

Therefore, the memory layout I showed above is necessitated by the
Standard.

--

Frederick Gotham
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-28-2006
Frederick Gotham said:

<snip>

> Also, there's no such thing as multi-dimensional arrays in C


Utter bilge.


From C89's 3.3.2.1 Array subscripting:

Successive subscript operators designate a member of a multi-dimensional
array object.

From C99's 6.5.2.1 Array subscripting:

Successive subscript operators designate an element of a multidimensional
array object.


If the Standard says C has multi-dimensional arrays, C has multi-dimensional
arrays.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-28-2006
Richard Heathfield:

>> Also, there's no such thing as multi-dimensional arrays in C

>
> Utter bilge.



Yes, you're right.


> From C89's 3.3.2.1 Array subscripting:
>
> Successive subscript operators designate a member of a multi-dimensional
> array object.



Indeed they do.


> From C99's 6.5.2.1 Array subscripting:
>
> Successive subscript operators designate an element of a
> multidimensional array object.



Again, you're right.


> If the Standard says C has multi-dimensional arrays, C has
> multi-dimensional arrays.



Yes you're right. But how exactly do these multi-dimensional arrays work?

Do you think it's right to say that, other than the proton and the
electron, that there's a third kind of particle within the atom? Some would
say yes, the neutron. Others would say no, because a neutron is little more
than an electron and a proton bound together.

So, I say that a multi-dimensional array is merely an array of arrays. And
guess what, that way of thinking works well for me.

If the Standard, or you for that matter, would like to say that there are
multi-dimensional arrays in C, then go ahead, you're right; but as far as
the grammar of the C programming language goes, they work like an array of
arrays.

--

Frederick Gotham
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-28-2006
On Sat, 28 Oct 2006 10:57:12 GMT, Frederick Gotham
<(E-Mail Removed)> wrote:

>Frederick Gotham:
>
>> Basically you're asking if the Standard necessitates that
>> multi-dimensional arrays be lain out in a particular way in memory,
>> specifically:
>>
>> a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
>>
>> I'm pretty sure that things have to be like this, but maybe someone can
>> give a quote from the Standard proving or disproving the necessity.

>
>
>I actually, if I put a tad bit of thought into it, I don't need to consult
>the Standard.
>
>We know that:
>
> (1) There cannot be padding between array elements or after.
> (2) Each consecutive array element must have ascending addresses.
>
>Also, there's no such thing as multi-dimensional arrays in C, but rather
>arrays of arrays. Therefore, looking at the first array:
>
> int (a[2])[3];
>
>We know that the three elements in this array must not have padding between
>them, and that their addresses must be ascending. Now, if we look at the
>array type, we see that each element is an int[2]. We know that this inner
>array must also have no padding between elements, and also that the
>addresses must be ascending.


I agree with your intent but I think you would have been closer to the
point you were trying to make to say the first array has two elements,
each of which is an int[3] and then proceed to discuss the absence of
padding between a[0] and a[1] followed by the combined effects of
aligning a[i] and a[i][0] and the absence of padding between a[i][j]
and a[i][j+1].

>
>Therefore, the memory layout I showed above is necessitated by the
>Standard.


But if the compiler performs range checking on constant subscripts or
inserts range checking into the executable code, it would compliant to
issue a diagnostic and reject the translation unit or terminate the
execution of the program for a serious run time error, respectively.


Remove del for email
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-28-2006
Richard Heathfield <(E-Mail Removed)> writes:
[...]
> From C89's 3.3.2.1 Array subscripting:
>
> Successive subscript operators designate a member of a multi-dimensional
> array object.
>
> From C99's 6.5.2.1 Array subscripting:
>
> Successive subscript operators designate an element of a multidimensional
> array object.
>
>
> If the Standard says C has multi-dimensional arrays, C has
> multi-dimensional arrays.


Yes, but it's really just a matter of terminology. If the C standard
didn't mention multidimensional arrays, or even if it explicitly
stated "The C language does not have multidimensional arrays, but it
does have arrays of arrays", then it would still describe exactly the
same language.

You can declare an array of arrays. The question of whether you can
call that a multidimensional array has a clear answer (yes, because
the standard says so), but it's not a very important question.

Note that some languages do support multidimensional arrays as a
distinct concept; arr1[x][y] indexes into an array of arrays, and
arr2[x,y] indexes into a two-dimensional array. C, of course, doesn't
do this (and the existence of the comma operator makes arr2[x,y] a
trap for the unwary).

Digression follows.

Similarly, we say that C *doesn't* have pass-by-reference because the
standard doesn't use that term. But if the standard referred to
passing a pointer-to-foo as passing the foo by reference, then we'd
have to say that C *does* support pass-by-reference. In fact, C does
support pass-by-reference *as a programming technique*, but not as a
language construct.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-28-2006
Keith Thompson said:

<snip>

> Digression follows.
>
> Similarly, we say that C *doesn't* have pass-by-reference because the
> standard doesn't use that term. But if the standard referred to
> passing a pointer-to-foo as passing the foo by reference, then we'd
> have to say that C *does* support pass-by-reference. In fact, C does
> support pass-by-reference *as a programming technique*, but not as a
> language construct.


As you probably recall, I think it is easier to explain parameter-passing to
a newbie if one sticks strictly to the "pass-by-value" terminology, even
when pointers are being passed. There is no need to make an exception for
pointers, and to do so leads to confusion, as we occasionally see in
questions on this newsgroup.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-28-2006
Richard Heathfield <(E-Mail Removed)> writes:
> Keith Thompson said:
> <snip>
>
>> Digression follows.
>>
>> Similarly, we say that C *doesn't* have pass-by-reference because the
>> standard doesn't use that term. But if the standard referred to
>> passing a pointer-to-foo as passing the foo by reference, then we'd
>> have to say that C *does* support pass-by-reference. In fact, C does
>> support pass-by-reference *as a programming technique*, but not as a
>> language construct.

>
> As you probably recall, I think it is easier to explain parameter-passing to
> a newbie if one sticks strictly to the "pass-by-value" terminology, even
> when pointers are being passed. There is no need to make an exception for
> pointers, and to do so leads to confusion, as we occasionally see in
> questions on this newsgroup.


I think it depends on the newbie. For someone who's learning to
program for the first time, it probably makes sense to stick to the
idea of "pass-by-value", and later introduce the idea that passing
pointers by value can be a useful technique if you want to modify the
pointed-to object.

On the other hand, someone who's new to C, but has experience in some
other language that *does* directly support pass-by-reference, will
likely find it useful to know that passing a pointer is the way to
implement the equivalent of pass-by-reference in C. (When I first
learned C, most of my programming experience was in Pascal.)

I might also argue that pass-by-reference is a useful concept in
computer science in general, independent of any particular language,
and it's useful to see how that concept is mapped onto the constructs
of a particular language.

C doesn't "have" linked lists, binary trees, or pass-by-reference.
You can implement them all using pointers. If I present a chunk of C
code that implements a linked list and say "This is a linked list",
nobody is going to jump up and say "No, C doesn't have linked lists;
that's just a structure with a pointer in it and some functions that
operate on it".

But that's not a perfect analogy, and I can certainly see your point.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
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
Lower bound & Upper bound sunil panda Java 9 10-07-2008 08:32 PM
Passing Bound Object value to another Bound Object Phillip Vong ASP .Net 0 07-27-2006 10:54 PM
How to represent the data in a not bound control via bound control Mario Krsnic ASP .Net 0 06-23-2006 07:38 AM
Getting lower-bound and upper-bound of strings input Rhiner Dan C++ 1 03-27-2005 02:03 AM



Advertisments