'''''''
I did this in O(n^2) and O(1) space
Anonymous
Line 1034: Char 34: runtime error: addition of unsigned offset to 0x603000000070 overflowed to 0x60300000006c (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34
If you look at the error, you can clearly see that your program exhibits Undefined Behavior because it access elements outside the bounds of the vector. nums[left] == nums[triplet[1]] nums[right] == nums[triplet[2]] Here triplet stores the values in the vector. You are then trying to access an element in the position denoted by values which may access outside the bounds of the vector This code here is one of the problems. Not sure if there are additional ones. Use a debugger.
'''''''
also are there any major differences between CLang and GCC compiler such that it can affect this code's output?
Anonymous
but before these conditions, I have left<n-2 and right>i+2 conditions which ensure the values don't go out of bounds
No. Let me give an example. Suppose say the first triplet you find is {-1,-1000,1001}. Now triplet[1] is -1000 and triplet[2] is 1001. Suppose the input nums was 10 elements long. In the code above when you do nums[triplet[1]] you are accessing an element at position -1000 which is outside the bounds of the vector nums
Ludovic 'Archivist'
Ludovic 'Archivist'
also, the usage of indices instead of iterators is a bit iffy
'''''''
Yes, clang tests for undefined behaviour and generally gcc does not
hmm that makes sense, leetcode checked for it and returned an error and my gcc compiler just gave me the output
'''''''
also, the usage of indices instead of iterators is a bit iffy
u mean iterators like I've used in main function?
Ludovic 'Archivist'
i mean you i, left, right and the operator[] you seem to love a bit too much
'''''''
i mean you i, left, right and the operator[] you seem to love a bit too much
ahh lol, will see how to replace them with iterators
Anonymous
my compiler isn't giving me an error, leetcode compiler is and leetcode debugger is not free
You can force gcc to show runtime errors by turning on the sanitizers. The command line option is -fsanitize=undefined
Ludovic 'Archivist'
I would have solved that problem with a range that generates a superpermutation of the vector just out of funsies
Ludovic 'Archivist'
but it would probably take a few years to run
Daiki
I built up a small http server and now I want to how I can send data to the browser through the server
If I have for example a code like this. <?php echo $_GET['id']; ?> How to send the value of id with my C code?
Daiki
I used the winsock2.h library to implement it.
Лазиз
HI! Is it possible to initialize an array of integers with characters? int num[3]; num[0] = '0';
Ludovic 'Archivist'
HI! Is it possible to initialize an array of integers with characters? int num[3]; num[0] = '0';
yes, some compiler may even support multiple characters literals as an extension, allowing to do int a = 'ABCD';
Ludovic 'Archivist'
You will get a number, char is a number already, and multibyte characters are also numbers
Ludovic 'Archivist'
also, I do not recommend thinking of char <=> character, text very rarely works like that and to do true text processing you should learn more about UTF runes, codepoints and graphemes
Light
#include <stdio.h> #include <math.h> int main() { float num = 3.14; int result = floor(num); printf("Floor integer of %.2f = %d", num, result); return 0; } Error: undefined reference to `floor' gcc -std=c99 -Wall -lm -o out test.c Why this error is showing ?
Daulet
Try to use floorf
Or replace math.h to tgmath.h
Daulet
Header <tgmath.h> provides a type-generic macro version of this function.
Light
Maybe problem with argument -std=c99. _Generic macro is added in C11
tried tgmath.h floorf without -std=c99 still same error
Anonymous
Hello
Adarsh
I am using Linux
gcc [programName].c -o programName
Adarsh
Check your way to run the program is it ok !!
Adarsh
./programName
Light
gcc [programName].c -o programName
same error /usr/bin/ld: /tmp/cc4MCc54.o: in function `main': x.c:(.text+0x2d): undefined reference to `floor' collect2: error: ld returned 1 exit status
Adarsh
Output in online compiler
Light
Remove -Wall from command
gcc -std=c99 -Wall -lm -o x x.c gcc -std=c99 -lm -o x x.c gcc -lm -o x x.c gcc -o x x.c not working
Лазиз
i think its a bad practice
Great! Try to change this code on your way #include <unistd.h> void ft_putchar(char c) { write(1, &c, 1); } void ft_commas(void) { ft_putchar(','); ft_putchar(' '); } void ft_print_comb(void) { int num[3]; num[0] = '0'; while (num[0] <= '9') { num[1] = '0'; while (num[1] <= '9') { num[2] = '0'; while (num[2] <= '9') { if (num[0] < num[1] && num[1] < num[2]) { ft_putchar(num[0]); ft_putchar(num[1]); ft_putchar(num[2]); if(!(num[0] == '7' && num[1] == '8' && num[2] == '9')) ft_commas(); } num[2]++; } num[1]++; } num[0]++; } }
Daulet
x.c
Is your code changed or stay same?
Light
Is your code changed or stay same?
yes my code is same as before
Light
#include <stdio.h> #include <math.h> int main() { float num = 3.14; int result = floor(num); printf("Floor integer of %.2f = %d", num, result); return 0; } Error: undefined reference to `floor' gcc -std=c99 -Wall -lm -o out test.c Why this error is showing ?
#include <stdio.h> #include <math.h> int main() { float num = 7.144; int result = floor(num); printf("Result: %d", result); return 0; } Here is simple version Error: gcc -o x x.c /usr/bin/ld: /tmp/ccFq8QjC.o: in function `main': x.c:(.text+0x2d): undefined reference to `floor' collect2: error: ld returned 1 exit status
Daulet
What linux distro you use?
If ubuntu then try install build-essential: sudo apt install build-essential
Light
gcc: error: unrecognized command-line option ‘--lmath’
Light
What linux distro you use?
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Daulet
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Do You install build-essential?
Light
gcc x.c -o x -lmath /usr/bin/ld: cannot find -lmath: No such file or directory collect2: error: ld returned 1 exit status
Light
Do You install build-essential?
yes I did And it is up to date
Light
working
Light
Thanks everyone It is working if I use gcc x.c -o x -lm but it is not working inside Clion IDE
Ludovic 'Archivist'
fan
Hi gays #include <iostream> class Test { public: operator std::string() { return "hello, world"; } operator int() { return 123; } }; int main() { std::string v = Test(); int a = Test(); printf("v = %s\na = %d\n", v.c_str(), a); return 0; } we know c++ is not provide return value overload. but how understand this case ?
Gani
Ludovic 'Archivist' Lagouardette Thanks all of them
Gani
I have never forgotten your helping
Anil
I have watched videos on C now can anyone suggest me how do I practice writing the code
Anonymous
Hello
Anonymous
Can you post the source code of the calculator in C++?
Anonymous
Hi gays #include <iostream> class Test { public: operator std::string() { return "hello, world"; } operator int() { return 123; } }; int main() { std::string v = Test(); int a = Test(); printf("v = %s\na = %d\n", v.c_str(), a); return 0; } we know c++ is not provide return value overload. but how understand this case ?
This is not like overloading based on return values. When you overload based on only return values then there is no way for the compiler to decide which function you intended to call. For ex: if C++ allowed overloads based on return values then you could write two functions like this: int fun(){ return 42;} double fun(){ return 3.0;} fun(); In this case, the compiler can't decide between the two overloads. In your case, you have two conversion operations which are not overloads. This can be used for conversions only in those cases where the compiler can figure out which conversion operation you intended to call. This is usually done through a static_cast or through an implicit/explicit user defined conversion. In your case you would do something like: static_cast<std::string>(test_obj); or int s {test_obj} In both cases, the compiler can figure out which conversion operation you intended to call.
Notaxmar
Hey everyone. Just had a simple question, when specifying system of effect on Windows, its like #include <windows.h>. How can I specify on android only
Anonymous
What is the difference int function(VarObj myObj) vs int function(VarObj & myObj)
Aquatica
What is the difference int function(VarObj myObj) vs int function(VarObj & myObj)
One copies the variable the other passes it as a reference
Anonymous
very helpfull
Captain
What is the difference int function(VarObj myObj) vs int function(VarObj & myObj)
First one makes copy of myObj and whatever changes made to myObj in that function will be done to that copied myObj not the original one, the copy gets destroyed when the function terminates. In second one whatever changes made will reflect in original myObj because it is referencing to original.
Michel
I have a custom type that allocates memory on the heap and I want it to be usen in the CUDA device, but I need to set the values from the host. What would be the bes way to do it?
/
Hi i need help
/
when i do this