This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 6 years ago.
int i = 7, j = 3;
int *a = &i, *b = &j;
cout << (*a = *b) << ", " << *(*(&a));
Can someone please explain why is the output 3, 7?
Your code can be simplified:
int i = 7, j = 3;
cout << (i = j) << ' ' << i;
Here the variable i is accessed and changed in the same statement. Since the order of evaluation of different parts of the same statement is not specified in the C++ standard, compiler may calculate them in any order, and the result may be different on different compilers (or even different versions of the same compiler, or different runs of the same compiler on the same source code, or even different runs of the same compiled program).
Don't write code that changes and accesses something in one statement.
Related
This question already has answers here:
Why do you have to specify a type for pointers?
(3 answers)
Pointer Arithmetic [closed]
(7 answers)
Closed 13 days ago.
So, I've been learning C++ recently and I have been trying to grapple with pointers and that sort of memory management. This is a simple program I wrote that prints out an array to the cout:
#include <iostream>
int main() {
int x[10] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34};
for(int i = 0; i < 10; i++) {
std::cout << *(x + i) << ", ";
}
}
And this works fine- however I'm a bit confused why line 7 (std::cout << *(x + i) << ", ";) isn't, instead, std::cout << *(x + i * sizeof(int)) << ", ";. Since "i" only increments by one, and the array is a list of multi-bit integers (traditionally 32 bit), shouldn't I have to increment it by the size of one of these integers to get the next int value?
Apologies if I'm not properly understanding how this all works, but any response is appreciated! Thank you!
This question already has answers here:
How does the range-based for work for plain arrays?
(6 answers)
C++11 range based loop: How does it really work
(3 answers)
Closed 2 years ago.
I'll preface this question by saying I am knowledgeable on C, and I'm learning C++.
Suppose I've got an std::string object and I want to go thru all of its characters to verify it (for example, only 4 letters allowed).
I have seen for (type var : array) being used in some code around a week ago, but I couldn't remember how it was used. I'm wondering.. exactly how does this work, and why does it work? Logically it shouldn't.
AFAIK, arrays don't really "know" what their length is. So suppose I have this code:
int arr[] = { 1, 2, 3 };
for (int item : arr) {
std::cout << item << std::endl;
}
How the hell does the for loop know when to stop? Arrays are simply put allocated memory (stack, in this case) that start from a specific address and go on. There's no "stored length" (AFAIK) for arrays. This is why it is a programmer's responsibility to store the length of the array, and do for (int i = 0; i < [length]; i++) but this somehow works?
Would love some explanation. Specific use case for my code:
std::string test = "Hello World!";
for (char letter : test) {
if (letter == 'l') {
std::cout << "'l' exists!" << std::endl;
} // result should be 3 prints.
}
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
The following code gives a weird output. I have tried it on multiple compilers and ended up with the same answer. It will process the statement right to left but print the output left to right however c++ statements are evaulated left to right in general. Can someone explain why this happens when we overload the cout statement.
Output:
15
10
5
However the Output if processed from left to right should be:
8
10
12
#include<iostream>
using namespace std;
int main(){
int a = 5, b = 3, c = 2;
cout<< (a = b + a) << endl << (b = c + a) << endl << (c = b + c);
return 0;
}
This will result in unspecified behavior. The order in which the operations execute isn't specified (since there are no sequence points between them), so the three assignments
a = b + a
b = c + a
c = b + c
can occur in any order. Therefore the output from cout is also unspecified .
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
Why "n*n" results as 4 at the first instant of the loop? to me it should be 1*1. instead it comes as 2*2.
Please give me a simple answer as i'm still a beginner :)
#include <iostream>
using namespace std;
int main(){
int n =1 , *p;
p = &n;
char aString[] = {"student"};
for (int i = 0; i<5; i++)
cout<< "i = "<< i << "n*n = "<<n*n<< "n++ = "<< n++<< " *p "<<endl;
system ("pause");
return 0;
}
http://ideone.com/nWugmm
The evaluation order of elements in an expression is unspecified, except some very particular cases, such as the && and || etc.
writing:
cout<< "i = "<< i << "n*n = "<<n*n<< "n++ = "<< n++<< " *p "<<endl;
you suppose an order and in particulr that n++ is the last evaluated.
To solve this problem you could split the exression in two parts:
cout<< "i = "<< i << "n*n = "<<n*n<< "n++ = "<< n<< " *p "<<endl;
n++;
Order of evaluation is not specified, it's not left to right as you may think and it's not right to left.
Split the code like Daniele suggested if your code relies on order.
And compile your code with high warning level, the compiler can help you spot this.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
pre and post increment operations on a variable give different output on TC and gcc [duplicate]
(5 answers)
Closed 9 years ago.
int a = 0;
cout << a << a+1 << a << 1+a << a;
// output is 01010 , ok fine i understand it! :-)
cout << endl;
cout << a << ++a << a << a++ << a;
// output is 22202 ,
// Plzzz help me how compiler interprets my this statement
// i cant understand :-(
int x = 1;
cout << ++x + ++x;
// output is 6
// how ??
Please if anyone could explain it to me how these outputs are coming :-)
Thanks in Advance!
It depends on the type of x. If x is a built-in type (e.g., int) then you have undefined behavior because you're modifying x twice without an intervening sequence point1.
If x is a user-defined type, then you have unspecified behavior.
1. Technically C++11 has change the terminology so "sequence point" is no longer used, bu the effect is the same.