0
Pointer A predincrements and pointer B postincrements in cycle. Why aren't they equival? What's wrong with pointer B?
//Part of code with cycle: do { --*I; ++*A; *B++; cout << " A: " << *A << ", B: " << *B <<endl; } while (*I); //Full code: https://code.sololearn.com/cRyHc43YY9ca/?ref=app
11 Respostas
+ 2
"But result are very different. When A = 2, B can be 0 or 35677 or something else, and next execution A = 3, B =0."
The reason for that becomes clear when you come to grip with the fact of the uncertainty of implementation (compiler) reaction against presented undefined behavior which is caused by blindly moving pointers ahead into the regions (next 4 bytes in each iteration) not being assigned to the process. In this situation, everything is possible. The program might crash, or a segmentation fault might be triggered, or the program would happily generate incorrect results.
Now you can probably figure out how those numbers come up "randomly",
Example: If B is a pointer declares as `int *B = new int;` and initialized as `*B = 0` at address `0x1720c20` (before iteration), then for 3 iterations we'll have the following changes each time the execution flow reaches to `*B++`,
Itr #1: the value of B = undefined, address = 0x1720c24
Itr #2: the value of B = undefined, address = 0x1720c28
Itr #3: the value of B = undefined, address = 0x1720c2c
Also, please note that each dynamically allocated storage with `new` keyword must be accompanied by its respective `delete` keyword in order to fully handed the utilized memory to the operating system, otherwise the program causes memory leakage.
+ 1
++*B;
+ 1
Nico Ruder ~ swim ~ Now I got it. Thank for your help!
+ 1
no problem
+ 1
"delete I, A, B"
Although the above construct is valid, yet if you have a look at the way the comma operator[1] treats both sides of itself and most importantly the precedence of comma and `delete` operators [2], you come to realize that what you are doing is actually deallocating "only" the pointer `I`. That is,
(delete I) , (A, B)
First, the left-hand-side (LHS) of `, ` (inside the left parenthesis) being evaluated and the result (if any) is discarded. Then, after checking for any side effect of LHS the RHS of `, ` get through the same process. So, you want to make sure to separately delete each as
delete I;
delete A;
delete B;
[1] https://en.cppreference.com/w/c/language/operator_other#Comma_operator
[2] https://en.cppreference.com/w/cpp/language/operator_precedence
0
the pluses before B i would say
0
because ++var increaments first and than goes on and var++ increments first after the next execution
0
because ++var increaments first and than goes on and var++ increments first after the next execution
0
But result are very different. When A = 2, B can be 0 or 35677 or something else, and next execution A = 3, B =0.
0
~ swim ~ Just tryed it with int array, works generally correctly.
0
To Seek Glory in Battle is Glorious
Thank you for the detailed answer, I fixed the mistake, and have another question: is next part of code correct?
"delete I, A, B;
I = A = B = 0;"