in Programming in C edited by
754 views
2 votes
2 votes
char buffer[6]=”hello”;
char *prt1=buffer -1; /* undefined behavior */
char *ptr2 = buffer +5;  /*Ok, pointing to the ‘\0’ inside the array */
char *ptr3 = buffer +6;  /* OK, pointing to just beyond */
char *ptr4 = buffer +7;  /* undefined behavior */

Please clear last two line.. and why 2nd last is not undefined behavior.

in Programming in C edited by
by
754 views

2 Comments

it should show undefined because it is pointing outside of allocated memory location ,rt?
0
0
yes, @Kapil, @ManojK , @Arjun Sir, Please help ...
0
0

2 Answers

5 votes
5 votes
Best answer
char buffer[6]=”hello”;
char *prt1=buffer -1; /* undefined behavior */
char *ptr2 = buffer +5;  /*Ok, pointing to the ‘\0’ inside the array */
char *ptr3 = buffer +6;  /* OK, pointing to just beyond */
char *ptr4 = buffer +7;  /* undefined behavior */

char *ptr3 = buffer + 6 is valid, as we are allowed to legally point to one past the last array element until we dereference it .

Actually, Setting a pointer one past the last element of an array is allowed in C and C++. 

Hence, setting to pointer after '\0' ends in an array is allowed for only one position .

Ex => char buffer[8] = "vijaycs"

so, buffer + 7 returns '\0' which is surely allowed .

and Setting a pointer to  buffer + 8(one past the last array element) is also valid, until it is dereferenced like

buffer[8] or *(buffer + 8), as it is not legal to access the contents of an address outside the allocated memory for an array.

Hence, after this setting a pointer to buffer + 9 and onwards is undefined behaviour .

You can read more here => https://gcc.gnu.org/onlinedocs/libstdc++/manual/iterators.html

selected by
by

6 Comments

@Kapilp

buffer + 8(one past the last array element) is also valid//correct .

But setting a pointer to buffer + 9 and onwards is undefined behaviour .Not clear.Can you explain this?

0
0
@Manoj Sir ,

setting a pointer to buffer + 8 is valid , but derefencing is not valid as it can return some core dump data.

whereas, setting a pointer to buffer + 9,  buffer + 10, buffer + 11, buffer + 12 results in undefined behaviour.
0
0

C standard says

When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integral expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
char *ptr3 = buffer +6;

 We can take the address one beyond the end of an array, but you can't dereference it. For your array of 6 items, buffer+6 would work. However  &buffer[6] really causes undefined behavior or not (and if it does, whether it really should). However, the result of that expression is unspecified (just guaranteed not to be an overflow).

char *ptr3 = buffer +7;

While any other expression going more than one past the array bound is explicitly undefined behavior.

Now going beyond one past of array means array out of bound checking .As C does not have array out of bond checking so as  C standard is concerned, accessing an array outside its bounds has "undefined behavior".

Now correct me if wrong ?

0
0

""""Now going beyond one past of array means array out of bound checking .As C does not have array of bund checking so as  C standard is concerned, accessing an array outside its bounds has "undefined behavior".""""

Setting a pointer for one past the last array element is fine , but, accessing that memory location is undefined behaviour ( sometimes, it is segmentation fault ) . 

how this works?

take one past the last array element memory location and set a pointer "last" to that location.

char* last = buffer + 8;

For(char* str = buffer ; str < last ; str ++)

{.............................}

Hence, accessing/Dereferencing past the last array element results in undefined behaviour .

0
0

Are you taliking about these two ?

#include<stdlib.h>
#include<stdio.h>
main()
{
   char buffer[6]="hello";
   char *ptr3 = buffer +8;
   char *str;
for(str=buffer;str<ptr3;str++)

    printf("%d \n",str);
}

And

#include<stdlib.h>
#include<stdio.h>
main()
{
   char buffer[6]="hello";


   char *ptr3 = buffer +8;
   char *str;
for(str=buffer;str<ptr3;str++)

    printf("%c \n",*str);
}
1
1
Yes, Now , u are accessing the contents of ptr, which is UB or segmentation fault .
0
0
2 votes
2 votes
by

Related questions