Till
Dzung
All life is undefined behavior
`int *testint()
{
int a = 99;
int *b = &a;
return b;
}
int main()
{
int *a = testint();
std::cout << *a << std::endl;
}` This code gives UB right?
Dzung
i've tested with -fsanitize=undefined , it prints 1
Till
Dzung
without giving any error
Dzung
i used g++
Dzung
and with also -Wall -Werror -pedantic-errors
Vladimir
Dzung
@suisei_v it print 99 normally, with fsanitize=undefined, it prints 1
Vladimir
Vladimir
Sometimes you get 99, sometimes not. It's random
Vladimir
From this point it's UB
Dzung
Yes, but if i changed the code little bit to
`
int *testint()
{
int a = 99;
return &a;
}
`
Dzung
i'll get address of stack memory associated with local variable 'a'
Vladimir
Compiler saves you from self-shooting
Dzung
technically, they aren't different, why on the first code, compiler can't point that error out?
Vladimir
Because you use another variable to store address, so compiler thinks you know what are you doing
Dzung
since both return pointer to the deallocated memory address
Dzung
oh, ok
Vladimir
You can write
int *foo() { int *b = 84230482; return (b); }
Vladimir
Compiler will say nothing
Anonymous
I'm not sure, but modern compilers should warn you.
Anonymous
Lemme check.
Vladimir
You just return a value.
UB is about dereference it.
But when you try to return address of local var, compiler will warn you
Anonymous
Interesting.
Anonymous
GCC does warn you, just not how I expected.
Anonymous
test.c: In function ‘foo’:
test.c:2:11: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
int *b = 84230482;
Anonymous
so yeah, it doesn't.
Vladimir
It's about casting, not returning
Anonymous
yes.
Anonymous
One has to be wary of pointers.
Vladimir
True
Lelouch
83. Program to find area of triangle using Heron's formula.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double a, b, c, s, area;
clrscr();
printf("\nEnter the sides of triangle : \n");
scanf("%lf%lf%lf", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("\nArea of triangle using Heron's Formula : %.2lf", area);
getch();
}
Lelouch
Guys want to know
Lelouch
Meaning of lf
Anonymous
yes?
Lelouch
And double used
Dzung
Dzung
c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Vladimir
lf = let printf know you send double
Anonymous
https://en.wikipedia.org/wiki/Printf_format_string
Vladimir
technically lf = f, but a long time ago f means float lf means double
Vladimir
so now lf = f
Lelouch
Oh
Lelouch
Anonymous
Anonymous
But it should return a similar error
Anonymous
Clang-8 on Ubuntu throws this error for me.
Anonymous
test.c:2:7: warning: incompatible integer to pointer conversion initializing
'int *' with an expression of type 'int' [-Wint-conversion]
int *b = 84230482;
^ ~~~~~~~~
1 warning generated.
Anonymous
Anyways.
Solomon
Pls how can one check if a directory is empty or not
Anonymous
Vladimir
Lelouch
If i use float instead of double and f instead of lf answer is same but if i use int it shows 0 input as well as output
Anonymous
use int where? You use %d in the format string?
Dzung
from my first code? do you use any particular flag ?
Anonymous
Oh, not your code.
Anonymous
The code Vladimir sent.
Anonymous
Sorry to have misunderstood you.
Dzung
@Remavas i've tested with multiple online compiler, there is no warning too
Anonymous
Don't mine me, I misunderstood you.
Anonymous
I was talking about Vladimir's code.
Lauri
What's wrong with my msg
Anonymous
Any video lectures on c++
Anonymous
There are some lectures by Bjarne Stroustrup himself.
Anonymous
Anonymous
Lelouch
Pc game?
Ибраги́м
https://botondballo.wordpress.com/2019/07/26/trip-report-c-standards-meeting-in-cologne-july-2019/#lfts
Nani
Ok
.
/great
.
/get findprojects