MTG
can u elaborate
Vishal
I mean when I have: int a=4; a++; Then cpu will add two 4 bytes numbers (I'm assuming int to be 4 bytes) i.e 4 and 1... And when I have: int *b =&a; b++; Then cpu will still be adding two 4 bytes numbers(I'm assuming 32 bit architecture): i.e. address of a and 4. For the cpu it doesn't matter what those numbers represent...it just has to perform addition... That's why I'm not able to understand why it's UB if I don't access those invalid addresses.
MTG
the whole calculation is done once
Vishal
Ohk.. if it's so for such systems where pointer arithmetic is handled differently then normal arithmetic then it makes sense
MTG
can you show me where they have mentioned intermediate values should also be within range
MTG
Can you tell me one thing?
MTG
When you get an undefined behavior?
MTG
A) when you do calculation
MTG
B) when you store address
MTG
C) or when you access the location
MTG
Not until you access
MTG
Going by your point —— int *p = 10000; The above statement will result in an error
MTG
But no it will not
Vishal
Btw is this also UB: int *a =0xab14aa66 //random address
MTG
Btw is this also UB: int *a =0xab14aa66 //random address
I m telling this guy the same thing he is not getting convinced
Vishal
I'm asking
Vishal
He's giving the standard reference
MTG
Get to know the correct answer by some guy who knows and then tell who is being idiotic
Vishal
Standard has the final say in what is valid and what is UB
Vishal
Although I agree that it's weird
MTG
My point is simple until and unless you calculate the full expression you can't say whether it is undefined or not
MTG
I m gonna show it wait for 1 min
MTG
#include <stdio.h> int main() { char s[] = "hello"; printf("%s", s + 1000 - 997); return 0; }
MTG
Take this program
MTG
.LC0: .string "%s" main: push rbp mov rbp, rsp sub rsp, 16 mov DWORD PTR [rbp-6], 1819043176 mov WORD PTR [rbp-2], 111 lea rax, [rbp-6] add rax, 3 mov rsi, rax mov edi, OFFSET FLAT:.LC0 mov eax, 0 call printf mov eax, 0 leave ret
MTG
this is the assembly code for it
MTG
Go through and tell me
MTG
Compiler : x86-64 gcc 10.2
MTG
according to you gcc is not according to standards?
MTG
and this is not my machine It is compiler explorer
MTG
Quiz me
MTG
the answer is there in question itself
MTG
it is undefined
MTG
it is undefined behavior
MTG
https://godbolt.org/
MTG
atleast in some machine it is should be a undefined behavior to be classified as undefined behavior right?
MTG
Can you tell me how printf prints a string?
MTG
I have read and i know
Vishal
Can you tell me how printf prints a string?
Bro you can argue endlessly on this..it might not make sense to you (to me as well)...but if the standard mandates it to be UB..then it is *UB*
MTG
no point in arguing more if you find any machine in which that statement is defined behavior then ping me
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]); }
Here, in questions four options were there from which i have to mark correct one a) geeks b)orgeeks c)rgeeks d)forgeeks Correct answer was geeks. Undefined behavior was not mentioned..
MTG
yes'
MTG
we can argue with talented people not with extremely talented
MTG
show me in standard where it says intermediate values should in the bounds
MTG
again 'intermediate'
MTG
"intermediate" or "temporary" values
MTG
where it says intermediate?
MTG
god knows how you get UB without accessing the memory location and just by adding some number to address !
Vishal
where it says intermediate?
So do you agree that the following is UB(even if I don't access it): int main(){ int* a = new int[100]; a+=1000; }
MTG
again it is not undefined behavior
Vishal
again it is not undefined behavior
But it's not a case of temporary value
MTG
if you say that as an undefined behavior end() iterator will never exist in C++
MTG
MTG
By Dennis ritchie
MTG
thats is for traversing the array
MTG
you need and end limiter to know where to stop
MTG
fine I will stop now You can report that question in geekforgeeks
MTG
and let them answer your query
MTG
then you can prepare using the standard without gfg :)
MTG
the standard clearly states when you access
Vishal
the standard clearly states when you access
Bro...please read point 4.3...and let us know what you think it means
MTG
fine char s[] = "hello"; char *p = s + 10;
MTG
so according to you this program itself is undefined?
MTG
the prrogram itself?
Vishal
so according to you this program itself is undefined?
What do you think after reading point 4.3
MTG
4.3 states that if the pointer is far away from the bound it is undefined but when you subtract it if you come back to your bounds which the program has access It is not undefined, you can use it
Vishal
fine char s[] = "hello"; char *p = s + 10;
Let's come to the subtraction part later... Here you didn't perform subtraction
Vishal
So do you agree that it's UB
MTG
Let us do one thing : post this on stackoverflow and let some great minds answer
MTG
Agree?
Vishal
fine char s[] = "hello"; char *p = s + 10;
I mean do you agree that this is IB
Vishal
UB
MTG
I mean do you agree that this is IB
no not if you don't access
Vishal
Here you didn't perform subtraction
Vishal
But you did read 4.3 didn't you
MTG
No let us post new one which the same example of geeksforgeeks
MTG
Agree?
Vishal
Madhu has already posted the question... It's same I think
MTG
no no other people will answer
MTG
no worry I will post