Anonymous
Hi, question ! I used std::size_t in for loop to printing my array elements but it doesn't work(nothing prints), what's the reason ? I compiled that with gcc 11.1 and clang 5.0, but it still doesn't work without any error 🤯. int main() { srand(time(0)); std::array<std::size_t,10> arr; for(std::size_t i = 0; i<10; i++) arr[i] = rand()%9; for(std::size_t j = 9; j>-1; j--) std::cout << arr[j] << std::endl; }
This is an example of compiler optimization when it encounters possible Undefined Behavior. On my system (with the default compilation flags), this program doesn't produce any output and the program just runs and stops. This is because the compiler knows that the second for loop will never terminate and optimized it away. The compiler is allowed to do this because under circumstances where Undefined Behavior can be caused, the compiler is free to do anything it wants. The reason why your second for loop causes Undefined Behavior is because of the conditional part in your foor loop: j>-1 This will always be true because a std::size_t variable can never be negative. So your loop runs for ever and in the process the value j takes on values that are outside the valid indexes for the array and thus accessing them results in Undefined Behavior.
Shady
are address boundary error and segmentation fault the same?
Anonymous
are address boundary error and segmentation fault the same?
Address Boundary errors are one type of error that causes a segmentation fault. A segmentation fault may happen because of many reasons like for example out of bounds access, accessing an uinitialized value (traps may be caused), exhaustion of stack space, heap expanding into stack space etc etc. Address boundary errors usually happen when you access memory that is illegal or not valid in your program at the point of access.
el
Hello guys, my robotic team searching for mentor or engineer for can mentor us about C++ for humanoid robots. This mentor get paid, if u interesting, just hit my DM, thankyou guys.
Anonymous
anybody knw about ternory operator
Anonymous
?
Pavel
What do you mean by "call class", do you mean calling operator() on an object? How do you store the objects? by value in the map or as pointers to some base class?
Pavel
Then you can use it right away from this map[1].myFunction() if your class has myFunction function
Pavel
I think you didn't tell the whole story what you're trying to achieve, though
Pavel
You can do that as well https://www.ideone.com/JxLPWW
Pavel
If that this doesn't answer your question, try to give more details of what you're trying to achieve
Pavel
It depends on what you want to achieve with this. The most common way is to have a common parent class that has some interface for the operations that you plan to do. E.g. you want to print message to different systems, you can have a class class Writer { public: virtual void print(int value) = 0; virtual ~System() {} }; and then writer to console you can inplement it like this: class ConsoleWriter : public Writer { public: void print(int value) override { // do some console specific stuff } }; Another way to do that can be to use std::any or std::variant. Maybe there are other ways but it really depends on a task
Armin
We need your contribution: github.com/ArminGh02/sorting-algorithms
پویا رحیم
Use std::array<std::size_t, 10>::reverse_const_iterator to print reverse of the array if am not wrong don't use indexing, is error prone
I appreciate it but i just wanted to use std::size_t and see what will happen. I don't want to print reverse array elements; but thanks 🙏
\Device\NUL
Do people still do that? Is better to use dynamic memory the heap
Idk, but from my perspective it should be avoided
Jojo
Like to c/c++ ot
klimi
#ot ?
Levi
how can i get mingw 32bit version
Sahil
how can i get mingw 32bit version
https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Automated%20Builds/
Anonymous
heyy budyys look i am learning c lang and i am alone nobody close to me for discuss anything about c lang i need some enviroment around us that would be help me alot
Nana
Hi guys
Nana
I need a book that deal with C++ GUI
مــــــــســــــعـــود
I need a book that deal with C++ GUI
https://www.amazon.com/C-GUI-Programming-Qt-4/dp/0131872494
Nana
Okay. Thanks bro
مــــــــســــــعـــود
The book is for c++ with qt framework. You can download it from https://qt.io
~
Write a program where two different queue structures use the same array. The first of the queues you will write will try to use the array from beginning to end, while the second will use it from the end to the beginning. Do not forget that queues may conflict and create a mechanism to prevent this. One queue may use more space in the array than the other queue.
mugish
Summers are back 🌞 In case you have started planning your summer, there’s something you must add to your list! It’s time for the *Grand Summer Internship Fair* and brands like *Reliance, Oyo, Mahindra Holidays, Delhi Capitals* have already arrived with *1,500+ more companies*. Why is it extra hot? You get a *stipend as high as INR 40,000/month*. I’m ready to experience a VIP journey through the GSIF Elite Zone and you should too because it doesn’t get any better. Participate here - https://internshala.com/grand-summer-fair-2022?utm_source=eap_telegram&utm_medium=19839808
مــــــــســــــعـــود
who can do this?
https://stackoverflow.com/questions/58051000/how-to-store-multiple-queue-in-array
Kate
Hello! I am starting to learn C++. I want to run a ready-made example, but it does not work, there is an error: E1696 cannot open source file "holoeye_slmdisplaysdk.hpp" The advices from Google didn't help. I think I have done everything right. Paths to include and library are added to: Project properties/Configuration properties/C/C++ General/Additional include directories Project properties/Configuration properties/Linker/General/Additional library directories Help me please!
江流儿
{ cout << "input [0-100] : "; cin >> x; if (x >= 0 && x <= 100) break; else rewind(stdin); }
江流儿
Can anyone tell me, rewind(stdin); what is it used for?
Kate
Can I see your code files?
Yes, I see them in the corresponding folder
Prince Of Persia
E1696 cannot open source file "holoeye_slmdisplaysdk.hpp"
Kate
Is this file exist?
Yes, it is in the folder include/cpp
Prince Of Persia
Yes, it is in the folder include/cpp
Are you compiling with cmake?
Kate
Are you compiling with cmake?
I don't know how to do this, sorry... I've just clicked Build/Build solution
Kate
In which ide?
In Visual Studio 2022
Prince Of Persia
In Visual Studio 2022
Oh idk much about windows sorry
LeBouy
A C program that reads input string and counts the number of each vowel character and the number of all other characters
Pavel
how do I code it
You either need a buffer for the string (array of chars) big enough to fit the input and then iterate over it using a loop Or you can just read input character-by-character (also in a loop) You need to have two counter variables, and in the loop increment one of them when meeting a vowel and another when meeting not a vowel, then after the loop print these two counter variables
LeBouy
#include <stdio.h> #include <stdlib.h> int main() { char str1[110]= "Trinity"; int i, vowels, other; //check if the character is a vowel if ((str1[i] == 'a' str1[i] == 'b' str1[i] == 'c' str1[i] == 'd' || str1[i] == 'e')) { ++vowels;} } else if (str1[i] >= 'a' && str1[i] <= 'z'){ ++other;} {printf("Vowels: %d\n", vowels); printf("Other: %d\n,", other);} return 0; }
LeBouy
that's how i did it but it keeps saying erros
Pavel
Also try formatting your code, it is very difficult to read
Pavel
#include <stdio.h> #include <stdlib.h> int main() { char str1[110]= "Trinity"; int i, vowels, other; //check if the character is a vowel if ((str1[i] == 'a' str1[i] == 'b' str1[i] == 'c' str1[i] == 'd' || str1[i] == 'e')) { ++vowels;} } else if (str1[i] >= 'a' && str1[i] <= 'z'){ ++other;} {printf("Vowels: %d\n", vowels); printf("Other: %d\n,", other);} return 0; }
so, let's just do step by step first you need to initialize your variabes: int i = 0; int vowels = 0; int other = 0; Otherwise their values are not guaranteed to be zero. second, you need a loop to iterate over your string, you can do something like this while (str[i] != '\0') { // your checks here i++; } and put your if-else inside
Pavel
#include <stdio.h> #include <stdlib.h> int main() { char str1[110]= "Trinity"; int i, vowels, other; //check if the character is a vowel if ((str1[i] == 'a' str1[i] == 'b' str1[i] == 'c' str1[i] == 'd' || str1[i] == 'e')) { ++vowels;} } else if (str1[i] >= 'a' && str1[i] <= 'z'){ ++other;} {printf("Vowels: %d\n", vowels); printf("Other: %d\n,", other);} return 0; }
Your check for vowels is a bit strange, "b", "c" and "d" are not vowels, are they? And some other vowels are skipped. Ah ok I see that these are some test values. If you want to make it more advanced (and you know how to make functions), you can make a function that checks if a character is a vowel and returns a bool, and then use that function in your if check.
Pavel
I don't see more problems, try to fix the code and check the errors the compiler gives to you
%Nikita
Hello, guys. How do you write code - in favor of readability, or in favor of speed? Because in some cases in C it is unreal to write fast and easy to read code. Just for example: The code below is readable, but it creates new array, which will be used only one time. Waste of time and memory: int op[3] = {item1, item2, item3}; func(op); And this code is hard to read, but array is inlined: func((int[3]){item1, item2, item3}); So which way is better?
%Nikita
if you turn on optimalizations, will the compiler optimalize it?
I don’t trust compiler optimizations. It can just remove parts of my code. I don’t know exactly, but I remember that my compiler (gnu c compiler) just deleted part where I change const variable by pointer: const int a = 5; int *p = (int*)&a; *p = 10; Solution is to declare a as const volatile int. It took me about 40 minutes to understand wtf is going on.
Juli
Who can help me
%Nikita
Who can help me
https://dontasktoask.com
Juli
Pls contact me in private
Pavel
And adding volatile to fix the code sounds bad (it's very slow)
LeBouy
C program that creates an 8-element array of type int, sets the elements to the first eight powers of 2, and then prints out the values. Use a for loop to set the values, and for variety, use a do-while loop to display the values?
Anonymous
Ludovic 'Archivist'
*happy hammer noises*
🙋🏻‍♂️
Hello guys