Dr
Two clients, yes. No server in this architecture
klimi
Luca
yes.
Talula
Isn’t this like WebSocket or MQTT?
Dr
But MQTT need broker right!?👀
Peace
A pointer could be of void type but reference variables cannot . Why ?
Pavel
A pointer could be of void type but reference variables cannot . Why ?
Interesting question, references were added several years later, maybe the thinking was to reduce the set of operations available for them only to safe ones
Pavel
But I'm not an expert here
Max
Hi
Ludovic 'Archivist'
A pointer could be of void type but reference variables cannot . Why ?
For the exact same reason they cannot be null, a reference is meant to be valid by design, meaning that anything that creates or manipulates a reference except pointers is to be type safe and not generate a null reference
Talula
But MQTT need broker right!?👀
Yep... you're right... My mistake... forgot.
Anonymous
For the exact same reason they cannot be null, a reference is meant to be valid by design, meaning that anything that creates or manipulates a reference except pointers is to be type safe and not generate a null reference
Actually references were not added to the language because they are type safe. There are umpteen number of ways by which you can create a dangling reference. If you wanted them to be type safe, the committee would have done something akin to Rust's lifetime support. The fact that references cannot be initialized to be null references is because they can't be changed once initialized. So null references are pretty much useless just like an uninitialized const pointer References were added to the standard for an entirely different reason.
Strife
#include<iostream> #include<conio.h> #include<math.h> using namespace std; //------F(3.6) int F(int m,int n){ if(m==1 || n==0 || m==n) return 1; else return F(m-1,n)+F(m-1,n-1); } int a(){ int m=3; int n=6; cout<<"F(3-1,6)+F(3-1,6-1)="<<F(m-1,n)+F(m-1,n-1); return 0; } int b(){ int n=3; int m=6; cout<<"F(m-1,n)+F(m-1,n-1)="<<F(m-1,n)+F(m-1,n-1); return 0; } main(){ cout<<"F(3-1,6)+F(3-1,6-1)= "<<'\n'<<"answer \\\\ "<<a(); cout<<'\n'<<'\n'; cout<<"F(6-1,3)+F(6-1,3-1)= "<<'\n'<<"answer \\\\ "<<b(); return 0; }
Strife
a() must be 4 and b() =20 But it shows me other answers
Strife
i change return my answer change if return 1 a() =41
Talula
yes
Because you're returning 0 and it is displaying that.
Peace
It is giving 40 and 200
I think it will return 4 & 20.
Talula
#include<iostream> #include<conio.h> #include<math.h> using namespace std; //------F(3.6) int F(int m,int n){ if(m==1 || n==0 || m==n) return 1; else return F(m-1,n)+F(m-1,n-1); } void a(){ int m=3; int n=6; cout<<"F(3-1,6)+F(3-1,6-1)="<<F(m-1,n)+F(m-1,n-1); } void b(){ int n=3; int m=6; cout<<"F(m-1,n)+F(m-1,n-1)="<<F(m-1,n)+F(m-1,n-1); } int main(){ cout<<"F(3-1,6)+F(3-1,6-1)= "<<'\n'<<"answer \\\\ "; a(); cout<<'\n'<<'\n'; cout<<"F(6-1,3)+F(6-1,3-1)= "<<'\n'<<"answer \\\\ "; b(); return 0; }
Peace
why compiler is not dereferencing void pointer? int p=10; void *ptr; ptr=&p; cout << *ptr << " "<< ptr; return 0;
Peace
Thank you too
welcome.. 🥳
Peace
As what type should it dereference it?
void pointers are allowed in C++. Then what is the use of that void pointer if we cannot dereference it.
Pavel
void pointers are allowed in C++. Then what is the use of that void pointer if we cannot dereference it.
You cannot dereference it because it doesn't have the type information. Try to answer my question? How the compiler should pick the type to dereference? Maybe its an int, maybe float, maybe std::string, how could it know? To use it you need to cast it to a pointer of a specific type
Pavel
I mean we have std::any
Peace
Also, why do you even need void* for in C++?
then why it is allowed if eventually void will get casted into another datatype.
Pavel
then why it is allowed if eventually void will get casted into another datatype.
If you want to pass some data and you don't know the type of it. So you can have an event system and want to be able to attach data of any type to the event. So you can allocate it, store as void*, some info about type, and probably pointer to a function that will clear the data (call delete). But that's an old (C) way of doing that, you would use std::any in C++ for that.
Pavel
C++ is supporting a lot of C features, and a lot of very specific features, it doesn't mean you need to use them all
Anonymous
then why it is allowed if eventually void will get casted into another datatype.
void* is useful in C++ when you need to treat memory as just memory. It is used in custom allocators where people need to write their own memory allocators for efficiency reasons suited to their own use cases. The part of this allocator which allocates memory doesn't know the types of objects that is going to be stored there. So they have to reference this memory using void* pointers. If you look at the standard library allocators code, you would find multiple void* references
Anshul
in set stl, when i am doing this how does the find function works
Anshul
set<Student,Student_cmp> s; int n; cin>>n; for(int i=0;i<n;i++) { string name,rollNo; int age; cin>>age>>name>>rollNo; Student temp(name,age,rollNo); s.insert(temp); } for(auto x:s) { cout<<x.name<<" "<<x.age<<" "<<x.rollNo<<endl; } Student to_find("Anshul",20,"2001"); if(s.find(to_find)!=s.end()) { cout<<s.find(to_find)->rollNo; } else { cout<<"not present"; }
Anshul
class Student { public: string name; int age; string rollNo; Student(string n,int a,string roll) { name=n; age=a; rollNo=roll; } Student(){} }; class Student_cmp { public: bool operator()(Student s1,Student s2) { return s1.rollNo<s2.rollNo; } };
Anonymous
in set stl, when i am doing this how does the find function works
Remember that the comparator you pass to set treats keys as equivalent if neither of them return true when compare is called on them.
Anshul
Remember that the comparator you pass to set treats keys as equivalent if neither of them return true when compare is called on them.
what if i want to know the element which is exactly same. i.e. i want to define my own == operator
Anonymous
what if i want to know the element which is exactly same. i.e. i want to define my own == operator
In your case, you take only the roll number into consideration. So if there are two students with the same roll number but different names, the set will treat them as equal and only one of them will be inserted into the tree. If your comparison operator is designed well and it takes all attributes of importance into consideration then you will have no issues. In your example if no students have the same roll number (typically that would be the case), then your comparison is fine. It should be able to locate the element whose roll number is the same as the roll number of the student you are looking for.
Anshul
i want that i sort students according to their roll no. but consider two students to be same if their names are same
Dark
Please send mingw compiler link
Dima
lol
Dima
maybe pdf?
Dark
Please send mingw compiler link
The file on sourceforgenet is not working
Anonymous
the comparator class helps the set to compare two Student objects right? according to this i create this class i.e. how i want my set to be sorted. but i can't understand how can i know 2 elements are equal or not
That is what I told you. For two students R1 and R2, if cmp(R1, R2) and cmp(R2,R1) both return false then R1 and R2 are considered equal If you want comparisons to take names into account then you should write such a comparison yourself. The same comparison will be used to store the elements in the set as well.
Anonymous
The file on sourceforgenet is not working
Did you try https://www.mingw-w64.org
Anonymous
for find() function, there are 2 checks made internally cmp(r1,r2) and cmp(r2,r1) ?
Yes. It has an effect similar to if ((!cmp(R1,R2) && (!cmp(R2,R1))
Anonymous
And why would you compare twice?
Because sets use a comparison operator which implements a partial order and not a total order
Dark
Did you try https://www.mingw-w64.org
yes i tried it didnt worked windows file is not available only linux repo are there
Anonymous
Yeah that is fine. But don't expect immediate feedback.
Amu
i have a question regarding a small part of my concurrency code
Amu
* enqueueStops -- * * Enqueue "num" stop requests (i.e. one per worker thread) into * the task queue associated with this request generator. * * Hint: Use the stop_handler function declared in * RequestHandlers.h in conjunction with the task queue to * create the stop requests. * * Results: * Does not return a value. * * ------------------------------------------------------------------ */ void RequestGenerator:: enqueueStops(int num) { // stop_handler(num) // TODO: Your code here. }
Amu
what does it mean to enqueue num of stop requests if anyone understands
Anonymous
what does it mean to enqueue num of stop requests if anyone understands
We can't comment much without understanding the full context. But I guess it means that there is a mechanism by which threads can indicate that they want a running job to be stopped using a stop handler. They probably expect you to be able to enqueue a stop request from each worker thread (therefore num stop requests in all) and thus stop 'num' jobs currently in progress.
Amu
The code for the stop handler is below:/* * ------------------------------------------------------------------ * stop_handler -- * * The thread should exit. * * Results: * None. * * ------------------------------------------------------------------ */ void stop_handler(void* args) { // TODO: Your code here. }
Amu
I understand that but what do you think is the right info to add to the stop handler? perhaps pthread exit?
Amu
and also what could the argument possibly be
Amu
“ Enqueue arg->maxTasks requests to the supplier queue, then * stop all supplier threads by enqueuing arg->numSuppliers * stop requests.”
Anonymous
I understand that but what do you think is the right info to add to the stop handler? perhaps pthread exit?
Yes. But it should be done gracefully i.e currently running jobs should be processed and no new requests should be accepted
Anonymous
and also what could the argument possibly be
The argument is already specified. It is a number which indicates the thread id possibly.
Amu
I see, thanks for the info. i’ll probably be back if i have any more questions
Amu
static void* supplierGenerator(void* arg) { class Simulation { public: TaskQueue supplierTasks; TaskQueue customerTasks; EStore store; int maxTasks; int numSuppliers; int numCustomers; explicit Simulation(bool useFineMode) : store(useFineMode) { } };
Amu
I have a class called simulation and supplier generator takes arg which is a pointer to the shared simulation object, however the shared simulation object does not have a constructor. How do create this shared simulation object in my supplier generator function? (remember arg is a pointer to the shared simulation object and I can’t use new)
Peace
What does it mean by flushing the stream when using cout<< endl; ?
Prince Of Persia
Prince Of Persia
please explain..
cout << end Is equal to cout << '\n' << flush;
Peace
cout << end Is equal to cout << '\n' << flush;
that means compiler first flushes the previous cout output stored in buffer and then cout further outputs if any and then again clears the buffer when program ends.