Anonymous
I think yes. It is located in the header file.
Creating it in the header file is wrong. The solution given by @itsmanjeet won't work for that case. There is no way to do this in C which doesn't have default initializers or constructors. So you must instead provide an init factory function which should be called every time you want a struct object to be initialized. This is the only recommended way
itsmanjeet
Did you check out haste?
Just checked that, look like it will work, thanks
Anonymous
Is it some sort of wrapper for structure? Can you suggest some article or what I should be looking for?
https://stackoverflow.com/questions/44574856/initialize-c-struct-through-a-function/44575797 The accepted answer has an example.
Anonymous
for(i=0;i<size-1;i++) { if(i<position) { temp[i]=a[i]; } if(i>=position) { temp[i]=a[i+1]; } }
You are copying all elements in array 'a' except the element at index 'position' to array 'temp'.
Deleted Account
If I set the fd of the socket to non-blocking mode, and then I use sendmsg to send fd to another process, do I still have to deal with the non-blocking unsent complete error(EAGAN?)
isaac
How do I write a C program that formats information entered by the user; Enter intem number:50 Enter unit price:1000
Dr
You actually read the input by specifying format like %d, %f or %s etc
Anonymous
If I set the fd of the socket to non-blocking mode, and then I use sendmsg to send fd to another process, do I still have to deal with the non-blocking unsent complete error(EAGAN?)
Yes you definitely have to. The shared socket between the processes uses the same underlying kernel structures. Assuming proper synchronisation is in place, you will have to deal with an error that a non blocking send would be an error if it would block which it would in your case. I don't quite understand exactly what it is that you are trying to do. If you want two processes to share a socket descriptor, you will have to send the descriptor through another UNIX socket piped pair.
Ammar
If I set the fd of the socket to non-blocking mode, and then I use sendmsg to send fd to another process, do I still have to deal with the non-blocking unsent complete error(EAGAN?)
You wrote: > I use sendmsg to send fd to another process Can you rephrase that part? I don't understand the part "send fd to another process". I suppose: - process A calls sendmsg(). - process B calls recvmsg(). Is that what you mean?
Anonymous
Hello, thx for allow me to get in, have a nice weekend!
Laopigo
Can i do something like this? char *s; if (something) { static char buffer[64]; // do something with buffer s = buffer; } // do something with s
ᎯᏏᎴυᎱᎱᎯᏂ𓅓.
Write a program in codeblocks to find the product of two floating point numbers and to display it. The inputs should be entered by the user.
Kunal
Int number; If((number & 1 )==0) Someone can explain me this line ?
Ammar
At a glance, the code looks sane to me. Can you show your debugging detail? Also, did you call send() properly from the other socket side?
Ammar
Int number; If((number & 1 )==0) Someone can explain me this line ?
It peforms bitwise "and" number with 1 and compare it with 0. This is actually a common way to take the LSB. IOW, if the LSB is zero, it means the number is even, otherwise it is odd.
Ammar
Alright, something went wrong. I retract my old sentence. char data = recv(client_sock, client_message, sizeof(client_message), 0); is not the right thing to do. recv() returns a ssize_t, not a char. Also the f << data is basically printing the return value of recv(). Not the buffer that the recv() writes to memory. You need to use f << client_message instead.
Ni
class A { public: A(A && rhs) noexcept; A& operator=(A && rhs) noexcept; A(int i): val(i) { std::cout « "hello" « "\n"; } private: int val = 0; }; A::A(A&& rhs) noexcept: val(rhs.val) { std::cout << "Move constructor." << "\n"; } A& A::operator=(A && rhs) noexcept { val = rhs.val; std::cout << "Move-assignmemnt constructor." << "\n"; return *this; } A foo() { A ret(5); // expression 1 return ret; } int main() { A a = foo(); return 0; } Guys, I need some help here: The above snippet gives hello on output, which was given by expression 1, so I wonder: 1. how did `a` in main function got initialized? It does not use move or move-assignment consturctor, and since copy and copy-assignment constructor is deleted, they are not used either
Jacky
u can use the move-assignment explicitly
Ni
u can use the move-assignment explicitly
Yep, I know I could do that, but I'm wonder why my initialization of a does not use any copy-control constuctors
Jacky
compiler would optimize the copy semantics
Jacky
r u use the clang compiler?
Anonymous
Write a program in codeblocks to find the product of two floating point numbers and to display it. The inputs should be entered by the user.
void main(void) { float x, y; printf("imput the numbers: \n "); scanf("%f",&x); setbuf(stdin, null); scanf("%f",&y); setbuf("stdin,,null"); printf("the result is: %f" , x*y); system("pause"); }
Jacky
u can define the copy constructor and assignment, check if can be used
我是大娃
why always use 'foo' and 'bar' as examples?
Jacky
can u close the compiler optimization?
Jacky
https://www.google.com/search?q=g%2B%2B+optimization+level
Ni
https://www.google.com/search?q=g%2B%2B+optimization+level
tried compile with g++ -O0 snippet.cpp, everything same as previous
Jacky
maybe -std=c++11?😂
Anonymous
yep
Jacky
default maybe the c++2x level😂
Milwaukee
Good people lm having trouble with my code l need it to out put a 3x2 matrix lm not sure were exactly am I getting it wrong
Milwaukee
#include <iostream> using namespace std; int main () { int m,n; int a[3][2]; cout<<"Enter the number of rows"<<endl; cin>>m; cout<<"Enter the number of columns"<<endl; cin>>n; cout<<"Enter the Elements in Table"<<endl; for ( int i = 0; i < m; i++ ) { for ( int j = 0; j < n; j++ ) { cin >>a[i][j]; } } for ( int i = 0; i < m; i++ ) for ( int j = 0; j < n; j++ ) { cout << "a[" << i << "][" << j << "]: "; cout << a[i][j]<< endl; } return 0; }
Jacky
the maximum of m is 3 and n is 2, maybe the input m or n is out of range?
Jacky
i tried…
Jacky
only the default constructor is called…
Jacky
https://stackoverflow.com/questions/5918312/what-rules-to-follow-for-a-function-to-use-nrv-optimization/5918369#5918369
Jacky
what is difference with the NRV?
Anonymous
what is difference with the NRV?
RVO is a special form of copy elision. It is usually used to refer to the copy elision optimization performed by the compiler when you return a named local variable/temporary from a function and use it to initialize another variable of the same type. But before C++17, you still required an accessible copy or move constructor. Post C++17, this optimization is mandatorily required and is hence not considered an optimization. So even if a class does not have an accessible copy or move constructor, the compiler can perform the copy elision. Thus copy elision is the proper way to refer to this rather than as an optimization which may or may not be performed by the compiler.
Jacky
i use the -std=c++11 only call Default Constructor, btw
Anonymous
i use the -std=c++11 only call Default Constructor, btw
Just try the OP's code after explicitly deleting (using = delete) the copy constructor and move constructor in both C++11 and C++17
Jacky
if delete the copy and move semantics, compile is error😂i guess it use the NRV
Anonymous
if delete the copy and move semantics, compile is error😂i guess it use the NRV
No. It will be a compiler error only when you use c++11. If you use std=c++17, it will compile fine. Go through the cppreference link in my first reply to the OP to understand why it happens.
Jacky
c++17 is error, btw
Jacky
env is wsl for ubuntu20LTS
Anonymous
c++17 is error, btw
Am not sure what you tried. Here, try this code in both C++11 and C++17 class A{ public: A(const A&) = delete; A(A&&) = delete; A(int a) : i(a){} private: int i = 0; }; A foo(){ return A(5); } int main(){ A obj = foo(); } This code would compile fine in C++17 but would give you an error in C++11 about inaccessible copy/move constructor
Shayan
Create a function that checks the Array overflow condition before insertion. [An overflow error occurs when one attempts to append the array using an index value greater than the array size]
Jacky
copy and move assignment all delete, btw😂
Anonymous
copy and move assignment all delete, btw😂
What are you on about? Do you even read anything said here? It will compile in C++17.
Muyi
that shouldn't be happening
Shlok
Anybody have code of Infix to prefix expression GUI code....!?
Jacky
compile in c++17 of course
Anonymous
hello folks, what vim plugins are you using ?
Anonymous
hello folks, what vim plugins are you using ?
a.vim, you complete me, fugitive and nerd tree
C++
hi guys, i was learning >> operator overloading in c++, but i found a weird thing that this line of code would compile in c++11 to c++17 standard but not c++20
C++
this is weird cuz i thought those standards are backward compatible
C++
wait i cant post media content
C++
#include <iostream> #include <cstring> class Example { private: char* str; public: // no args constructor Example () : str {nullptr} { str = new char[1]; *str = '\0'; } // Overloaded constructor Example (const char* s) : str {nullptr} { if (s == nullptr) { str = new char[1]; *str = '\0'; } else { str = new char[std::strlen(s) + 1]; std::strcpy(str, s); } } // Copy constructor Example (const Example& source) : str {nullptr} { str = new char[std::strlen(source.str) + 1]; std::strcpy(str, source.str); } // Copy assignment Example& operator = (const Example& rhs) { std::cout << "Using Copy assignment for " << rhs.str << std::endl; if (this == &rhs) { return *this; } delete [] this->str; // clear the pointer str = new char[std::strlen(rhs.str) + 1]; // allocate the memory on heap thats the same length as rhs string std::strcpy (this->str, rhs.str); // doing copy assignment return *this; // return the obj by reference } // Destructor ~Example(){ if (str == nullptr){ // std::cout << "Destructor for nullptr" << std::endl; } else { // std::cout << "Destructor for " << str << std::endl; } delete [] str; } friend std::ostream& operator<<(std::ostream& os, const Example& rhs){ os << rhs.str; return os; } friend std::istream& operator>>(std::istream& is, Example& rhs){ char* buff = new char[1000]; is >> buff; rhs = Example {buff}; delete [] buff; return is; } }; int main (void) { using namespace std; Example strep; cout << "Enter the third stooge's first name: "; cin >> strep; cout << "You entered: " << strep << endl; return 0; }
C++
ok so here's the code that i found the problem
C++
try compile that under C++20, it wont work, but for c++11, c++14 and c++17, they all work
Ni
void push_back(std::string&& str) { // 1 data->push_back(std::move(str)); } void push_back(std::string&& str) { // 2 data->push_back(str); } Guys, I've tried to implement a vector like class and I've got some problem implementing the move version push_back: * Are the above two member function identical? * Since the str argument is already a rvalue reference, so I soppose it's redundant to use std::move to convert str, and function 2 is enough, am I right?
Anonymous
Can you guys please give me some c++ project ideas for college?
Anonymous
#include <iostream> #include <cstring> class Example { private: char* str; public: // no args constructor Example () : str {nullptr} { str = new char[1]; *str = '\0'; } // Overloaded constructor Example (const char* s) : str {nullptr} { if (s == nullptr) { str = new char[1]; *str = '\0'; } else { str = new char[std::strlen(s) + 1]; std::strcpy(str, s); } } // Copy constructor Example (const Example& source) : str {nullptr} { str = new char[std::strlen(source.str) + 1]; std::strcpy(str, source.str); } // Copy assignment Example& operator = (const Example& rhs) { std::cout << "Using Copy assignment for " << rhs.str << std::endl; if (this == &rhs) { return *this; } delete [] this->str; // clear the pointer str = new char[std::strlen(rhs.str) + 1]; // allocate the memory on heap thats the same length as rhs string std::strcpy (this->str, rhs.str); // doing copy assignment return *this; // return the obj by reference } // Destructor ~Example(){ if (str == nullptr){ // std::cout << "Destructor for nullptr" << std::endl; } else { // std::cout << "Destructor for " << str << std::endl; } delete [] str; } friend std::ostream& operator<<(std::ostream& os, const Example& rhs){ os << rhs.str; return os; } friend std::istream& operator>>(std::istream& is, Example& rhs){ char* buff = new char[1000]; is >> buff; rhs = Example {buff}; delete [] buff; return is; } }; int main (void) { using namespace std; Example strep; cout << "Enter the third stooge's first name: "; cin >> strep; cout << "You entered: " << strep << endl; return 0; }
https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2 There were changes made in the C++20 standard to prevent reading into a char* pointer. This was to prevent buffer overflow attacks (the same reason why gets was removed from the standard. Backwards compatibility doesnt mean retaining all features that are seldom used and considered unsafe.
Anonymous
void push_back(std::string&& str) { // 1 data->push_back(std::move(str)); } void push_back(std::string&& str) { // 2 data->push_back(str); } Guys, I've tried to implement a vector like class and I've got some problem implementing the move version push_back: * Are the above two member function identical? * Since the str argument is already a rvalue reference, so I soppose it's redundant to use std::move to convert str, and function 2 is enough, am I right?
1 is correct. 2 is wrong. This is because, in 1, though str is an rvalue reference, inside the function's scope it is a named value and hence str as such is an lvalue. So you have to call std::move again to cast it to an rvalue reference. For ex in function1 you will notice the error if you try doing something like: void push_back(std::string&& str) { // 1 std::string&& s = str; //will fail to compile as str is an lvalue data->push_back(std::move(str)); }
Anonymous
Hi there How do vector::at function works
Ammar
yes
Ok, here is the answer... You only need to handle EAGAIN from the socket that is marked as non-blocking. The socket in another process does not need to care about EAGAIN *if it is not marked* as non-blocking.
Anonymous
what have imgui have develoop