Vishal
No let us post new one which the same example of geeksforgeeks
How will your question be different then this: https://stackoverflow.com/questions/10473573/why-is-out-of-bounds-pointer-arithmetic-undefined-behaviour#10473591
MTG
But it is your doubt and i m clear one that
MTG
but its fine i will post
Vishal
Yeah
Vishal
Don't forget that
Cengizhan
It is also UB, how do you know that stackoverflow question will not be answered by Madhu? :)
Cengizhan
If you do not believe or convince yet from others, we are not here to make you convince.
Cengizhan
If you say that it is not UB, ok do it on your own thats all.
MTG
https://stackoverflow.com/questions/63147390/is-this-a-undefined-behavior-or-not?noredirect=1#comment111665427_63147390
MTG
You can track as much as you want
Vishal
You should have asked the other example as well
Vishal
Because you didn't agree on that as well
Vishal
I think you should include that
MTG
Read the answer also
MTG
yes
MTG
sure
MTG
Looks like another guy answered
MTG
🙂
MTG
That's fine 👍 Sorry
MTG
Sorry about gender not about the argument
Anonymous
One of my senior too said that question is not UB As per him g : stores base address g[6] = *(g+6) it is 'o' 111 g[8]= 103 Expression is base address + 8 so answer is 1.. What should i consider?
MTG
g is pointer to char
MTG
yes
MTG
still we are
MTG
If I am wrong I will accept for sure
Anonymous
/get
Vishal
If I am wrong I will accept for sure
I don't know what someone has to do in order to prove to you whether something is UB or not...when the standard itself is not enough...one can only wonder
MTG
I m not getting where the standard says intermediate calculation values can't go out of bound
MTG
We get undefined behavior if we access the pointer
Vishal
You didn't agree to the other example being UB as well!!
MTG
and the pointer access takes place once the result is passed to the printf function
MTG
You didn't agree to the other example being UB as well!!
Bcz in that example we have not accessed the address
MTG
we just computed it
Vishal
Bcz in that example we have not accessed the address
And where exactly did point 4.3 mention that it's UB only when the pointer is accessed
Anonymous
I am new in C programming Currently on Pointers.. Can anybody please explain how below program output is geeks main( ) { char g[ ] = "geeksforgeeks"; printf("%s", g + g[6] - g[8]); }
So, to my conclusion.... In this type of question i should start solving from right to left as there (g + g[6] - g[8] ) is no grouping .. If grouped as ((g + g[6] ) - g[8] ) then it is UB as it start pointingto g + 111 address If grouped like (g + (g[6] - g[8] )); then it is valid Right????
Vishal
4.3 has clearly said that the addition itself is UB
Anonymous
Thank you very much!!
Anonymous
hi guys
Vishal
One interesting thing that his question has brought out is that even 'o' - 'g' isn't guaranteed to be 8 always
Vishal
So it's undefined anyways
Anonymous
please sand link of c# drage and drop software
MTG
here we are referring to ascii values right?
Vishal
Ohk...I missed that
Vishal
I guess if Bjarne Stroustroup will explain to him in person then he'll accept
Anonymous
One more doubt i have... Question : predict output #include<string.h> #include<stdio.h> int main() { char a = 30, b = 5; char *p = &a, *q = &b; printf("%d", p - q); } a) 1 b) Run time error c) Compile Time error d) Garbage value Answer : a Explanation given was difference between any two variables of same data type are always one But i think explanation given is wrong as, as per my understanding pointer substraction gives how many such type data can be stored within those addresses.. Is it always one or my understanding is correct?
Vishal
Afair it's not guaranteed that individual stack variables will be next to each other
Vishal
Isn't that why it's UB?
Anonymous
So for such questions i should say answer is UB
Anonymous
Okay... Understood!
Vishal
👍
Anonymous
Doing things on pointers like comparing their value is undefined behavior
Anonymous
Yeah... from now i will...
Anonymous
&a > &b for instance is undefined
Anonymous
@lightness_races_in_orbit do you see any UB here?
Anonymous
https://github.com/facebook/rocksdb/blob/d9d190742c0a8a3d5ff0b0867383bd38c1d0f5f0/db/db_impl/db_impl_secondary.cc#L115
Cengizhan
@lightness_races_in_orbit do you see any UB here?
No, because && operator is called short-circuit and every time first-if statement will be checked.
Cengizhan
There is no UB.
Scriptern
/get programming books
olli
I m not getting where the standard says intermediate calculation values can't go out of bound
then learn to read the standard, if you can't fall back to the SO answer provided above. The standard document talks about an "expression" which defines "the syntax, order of evaluation, and meaning of expressions. An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects." [expr] Even to compute a temporary you make use of the additive expression and so the rules apply, and while it's most likely to work fine on most architectures you encounter, your compiler is allowed to emit all the code it wants, potentially even harmful one. So I would argue not to invoke UB.
Anonymous
/getfreeprogrammingbook
Anonymous
/getfreeprogrammingbook
Anonymous
For some reason if the last 2 conditions are swapped the UB disappears
Ajay
One interesting thing that his question has brought out is that even 'o' - 'g' isn't guaranteed to be 8 always
yes, this was new to me. One of the comment says that the letters may not be sequential and may have some gaps inbetween the characters.
Ajay
BTw, if 'o'-'g' isn't guaranteed to be 8 then using this in programs is UB. Isn't it?
Ajay
If the answer for my above question is yes, then encoding provided by the state govt. Isn't it like if you use the govt. provided encoding then it will always be UB?
Ajay
ok I get that it will surpass the valid bounds. So if it surpasses the bounds in some X character encoding and not in some other character encoding Y, then it's UB in X and not in Y?
Ajay
ok fair enough, makes sense.
Anonymous
@lightness_races_in_orbit ot, but are you https://stackoverflow.com/users/560648/lightness-races-in-orbit?
labyrinth
a quick question, if I run a hello world program, is the parent process of this process the init process with pid_t == 1?
labyrinth
i was asked in an interview about how to check if a program has successfully run and it is too quick that it finishes before you can check with top or htop
Anonymous
a quick question, if I run a hello world program, is the parent process of this process the init process with pid_t == 1?
unless you ask the init daemon to launch your process, that won't be the case