concept question

J

jiing

int main(void){
char* p;
char q[10];
char temp='a'; //This line is outside

for(int i=0;i <10;i++){
p = &temp;
p++;
q='a';
}

for(int i=0;i<10;i++){
cout<<*p++<<"\t";
cout<<q<<endl;
}
}

int main(void){
char* p;
char q[10];


for(int i=0;i <10;i++){
char temp='a'; //This line is inside
p = &temp;
p++;
q='a';
}

for(int i=0;i<10;i++){
cout<<*p++<<"\t";
cout<<q<<endl;
}
}

why their result are not the same?
Can someone explain it for me.
Thanks a lot.

-jiing-
 
C

CrayzeeWulf

jiing said:
for(int i=0;i <10;i++){
char temp='a'; //This line is inside
p = &temp;
p++;
why their result are not the same?
Can someone explain it for me.

Both versions have undefined behavior. You are assigning p to &temp and then
incrementing p. This will result in p pointing to an undefined address.
What are you trying to do ?

Thanks,
 
C

CrayzeeWulf

jiing said:
for(int i=0;i <10;i++){
char temp='a'; //This line is inside
p = &temp;
p++;
why their result are not the same?
Can someone explain it for me.

Both versions have undefined behavior. You are assigning p to &temp and then
incrementing p. This will result in p pointing to an undefined address.
What are you trying to do ?

Thanks,
 
C

CrayzeeWulf

jiing said:
for(int i=0;i <10;i++){
char temp='a'; //This line is inside
p = &temp;
p++;
why their result are not the same?
Can someone explain it for me.

Both versions have undefined behavior. You are assigning p to &temp and then
incrementing p. This will result in p pointing to an undefined address.
What are you trying to do ?

Thanks,
 
A

Artie Gold

jiing said:
int main(void){
char* p;
char q[10];
char temp='a'; //This line is outside

for(int i=0;i <10;i++){
p = &temp;
p++;
q='a';
}

for(int i=0;i<10;i++){
cout<<*p++<<"\t";
cout<<q<<endl;
}
}


The above is correct.
int main(void){
char* p;
char q[10];


for(int i=0;i <10;i++){
char temp='a'; //This line is inside
p = &temp;
The variable `p' is assigned the address of the variable `temp'...
p++;
q='a';

....but here, `temp' goes out of scope...
....so here, `p' points to something that no longer exists, invoking
undefined behavior.
for(int i=0;i<10;i++){
cout<<*p++<<"\t";
cout<<q<<endl;
}
}

why their result are not the same?
Can someone explain it for me.
Thanks a lot.

Try a web search for `dangling reference' if this isn't yet clear.

HTH,
--ag
 
K

Krishanu Debnath

CrayzeeWulf said:
jiing wrote:




Both versions have undefined behavior. You are assigning p to &temp and then
incrementing p. This will result in p pointing to an undefined address.

Wrong. 'p = &temp; p++;' does not invoke undefined behavior. Because
after postfix increment, it points to one past the last element of the
'array object', which is allowed by standard.

OP actually invoking UB in the following for(both) loop:

for(int i=0;i<10;i++){
cout<<*p++<<"\t";
cout<<q<<endl;
}


Krishanu
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Krishanu said:
Wrong. 'p = &temp; p++;' does not invoke undefined behavior. Because
after postfix increment, it points to one past the last element of the
'array object', which is allowed by standard.

What array? temp is a char.
 
K

Krishanu Debnath

Julián Albo said:
Krishanu Debnath wrote:




What array? temp is a char.

Did you notice the quote while reading 'array object'? Following is the
relavent text from standard.

Sec 5.7 Additive operators [expr.add]

Para 4

"For the purposes of these operators, a pointer to a nonarray object
behaves the same as a pointer to the first element of an array of length
one with the type of the object as its element type."

Para 5

"..... 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."

Krishanu
 
R

Rolf Magnus

Krishanu said:
Sec 5.7 Additive operators [expr.add]

Para 4

"For the purposes of these operators, a pointer to a nonarray object
behaves the same as a pointer to the first element of an array of length
one with the type of the object as its element type."

Wasn't there also something in the standard that said that an object can be
treated as an array of char with sizeof(object) elements?
 
C

CrayzeeWulf

Krishanu said:
Wrong. 'p = &temp; p++;' does not invoke undefined behavior. Because
after postfix increment, it points to one past the last element of the
'array object', which is allowed by standard.
You are correct. Just incrementing the pointer does not result in undefined
behavior. The problem is with dereferencing it after it is incremented past
&temp. Thanks for the clarification.
 
D

Default User

Rolf said:
Krishanu said:
Sec 5.7 Additive operators [expr.add]

Para 4

"For the purposes of these operators, a pointer to a nonarray object
behaves the same as a pointer to the first element of an array of length
one with the type of the object as its element type."

Wasn't there also something in the standard that said that an object can be
treated as an array of char with sizeof(object) elements?

I believe these are the sorts of things you are refering to.

[basic.types] 3.9 Types

2 For any object (other than a base-class subobject) of POD type T,
whether or not the object holds a valid value of type T, the underlying
bytes (1.7) making up the object can be copied into an array of char or
unsigned char.36) If the content of the array of char or unsigned char
is copied back into the object, the object shall subsequently hold its
original value.

4 The object representation of an object of type T is the sequence of N
unsigned char objects taken up by the object of type T, where N equals
sizeof(T). The value representation of an object is the set of bits
that hold the value of type T. For POD types, the value representation
is a set of bits in the object representation that determines a value,
which is one discrete element of an implementation-defined set of
values.37)





Brian
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top