I have following test example:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result of the code above is:
init 0x7ffed27c6450 0x56480bc1eeb0 5
value 0x7ffed27c6470 0x56480bc1eeb0 5
done 0x7ffed27c6450 0 0
Looks great:
Now, move to:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo2(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result is:
init 0x7ffccc93a5c0 0x56124b3a8eb0 5
rvalue_ref 0x7ffccc93a5c0 0x56124b3a8eb0 5
done 0x7ffccc93a5c0 0x56124b3a8eb0 5
My problem is:
For the 1st case, it is perfectly called by "move semantics", and as you see, the ownership of the vector has been transfered to the function parameter. Finally, at "done", the data is null to verify the the vector at main() no longer owns the vector.
Now to explicitly claim the parameter is "rvalue reference", as case 2. As you see, actually it is like "call by (l)reference".
How can I figure out it?
Related
I try to make uncrustify to go from:
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
to :
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
But so far I failed miserably! Is this even possible, and if so, can I get any hints?
No, there is no such angle align option in Uncrustify as of now.
Only
# Whether to align lines that start with '<<' with previous '<<'.
#
# Default: true
align_left_shift = false # true/false
which for some reason is defaulted be always used
I am trying to get the valid memory address of CHAR A4 and b5 but when I try to reach that address using Hex Editor it's not reading the address that I have already got in my console output after compiling.
Hex Editor is validating the address as invalid address.
My Code:
#include <iostream>
using namespace std;
main ()
{
{ //INT
cout << "INT" << '\n';
int a = 2, b = 3;
cout << "Result: " << "for " << "int a " << "= " << a << '\n';
cout << "Result: " << "for " << "int a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//SHORT
cout << "SHORT" << '\n';
short a = 2, b = 3;
cout << "Result: " << "for " << "short a " << "= " << a << '\n';
cout << "Result: " << "for " << "short a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//FLOAT
cout << "FLOAT" << '\n';
float a = 2, b = 3.1;
cout << "Result: " << "for " << "float a " << "= " << a << '\n';
cout << "Result: " << "for " << "float a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//DOUBLE
cout << "DOUBLE" << '\n';
double a = 20, b = 30.1;
cout << "Result: " << "for " << "double a " << "= " << a << '\n';
cout << "Result: " << "for " << "double a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//CHAR
cout << "CHAR" << '\n';
char A4 = 'A' , b5 = 'B' ;
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << '\n';
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << " " << "at " << "address " << &A4 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << " " << "at " << "address " << &b5 << '\n';
cout << "-----------------------------------------" << '\n';
}
}
Check out the list of overloads for operator<< for streams. The one for char const* assumes a zero-terminated string at that address. What you want is the overload for void const*. For other types of pointees, that conversion is done implicitly by the compiler, for char you need to make it explicitly yourself:
cout << static_cast<void const*>(&b5) << endl;
char A4 = 'A';
cout << &A4;
Is printing char*, when you print a char* the standard library tries to print a null terminated character string. As you only have a single character there is no null terminator so junk gets printed until the standard library happens to find a null byte, this is undefined behaviour.
To print a pointer rather than a string you need to cast to a different pointer type, for example:
char A4 = 'A';
cout << static_cast<void*>(&A4);
I am trying to display struct members of a list in OMNeT++, all members are displayed correctly unless the member which is of type const char*. I am confused because after three push_back in the list, when I display. All the members of the last pushed element is displayed correctly even the one of type const char*. But for the two first pushed elements, the member of type cont char* display nothing, garbage or "DETAIL (Ipv4)Drones.host[3].ipv4.ip".
Ipv4Address srcAddress = recMtlsd->getSrcAddress();
Ipv4Address dstAddress = recMtlsd->getDstAddress();
const char* position = recMtlsd->getPosition();
simtime_t time = recMtlsd->getTime();
int srcID = recMtlsd->getId();
EV_DEBUG << "Source : " << srcAddress << endl;
EV_DEBUG << "Destination : " << dstAddress << endl;
EV_DEBUG << "Position : " << position << endl;
EV_DEBUG << "Time : " << time << endl;
EV_DEBUG << "Source ID: " << srcID << endl;
// All precedent displays are working correctly
/*typedef struct Mtlsd{
Ipv4Address originatorAddr, destinationAddr;
const char *position;
int originatorId;
simtime_t time;
}MTLSD;*/
MTLSD recitem;
recitem.originatorAddr = srcAddress;
recitem.originatorId = srcID;
recitem.destinationAddr = dstAddress;
recitem.position = position;
recitem.time = time;
EV_DEBUG << "Source : " << recitem.originatorAddr << endl;
EV_DEBUG << "Destination : " << recitem.dstinationAddr << endl;
EV_DEBUG << "Position : " << recitem.position << endl;
EV_DEBUG << "Time : " << recitem.time << endl;
EV_DEBUG << "Source ID: " << recitem.srcID << endl;
// All precedent displays are working correctly
/*typedef struct Mtlsd_data{
list<MTLSD> q;
int ID;
}MTLSD_DATA;*/
list<MTLSD_DATA> mtlsd_file;
auto node = find_if(mtlsd_file.begin(), mtlsd_file.end(), [=] (MTLSD_DATA const& i){return (i.ID == srcID);});
bool found = (node != mtlsd_file.end());
if (!found)
{
MTLSD_DATA recdata;
recdata.ID = srcID;
recdata.q.push_back(recitem);
mtlsd_file.push_back(recdata);
EV_DEBUG << "For node " << srcID ;
for(auto claim=mtlsd_file.back().q.begin(); claim!=mtlsd_file.back().q.end();++claim)
{
EV_DEBUG << "(" << string(claim->position) << ", " << claim->time << ");" << endl;
}
// The precedent display works correctly
}
else
{
EV_DEBUG << "I already have data about the node " << node->ID << endl;
if (node->q.size() == 3)
{
EV_DEBUG << "I already have three time-location claim in the queue" << endl;
EV_DEBUG << "Here they are: ";
EV_DEBUG << "For node " << (*node).ID << endl;
for(auto fileclaim=(*node).q.begin(); fileclaim!=(*node).q.end();++fileclaim)
EV_DEBUG << "(" << string((*fileclaim).position) << ", " << (*fileclaim).time << ");" << endl;
EV_DEBUG << "I will delete the old one (" << node->q.front().position << ", " << node->q.front().time << ")" << endl;
node->q.pop_front();
}
node->q.push_back(recitem);
EV_DEBUG << "I have pushed this new one : (" << string(node->q.back().position) << ", " << node->q.back().time << ")" << endl;
}
EV_DEBUG << "Here they are all time-location claims in the queue : ";
for(auto fileclaims=node->q.begin(); fileclaims!=node->q.end();++fileclaims)
{
EV_DEBUG << "(" << string(fileclaims->position) << ", " << fileclaims->time << ");" << endl;
}
// The last element is displayed correctly, but those before not.
.
.
.
So in c++11 the Chrono Library provides, duration_cast:
Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished
And c++17's floor:
Returns the greatest duration t representable in ToDuration that is less or equal to d
So for all x will the result of these 2 calls be equal:
chrono::duration_cast<chrono::seconds>(x)
chrono::floor<chrono::seconds>(x)
As far as I can tell, same as the difference between static_cast and std::floor: Negatives are rounded down instead of truncated toward zero.
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
int main() {
std::cout << "duration_cast:" << std::endl;
std::cout << "1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(1400ms).count() << std::endl;
std::cout << "1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(1500ms).count() << std::endl;
std::cout << "1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(1600ms).count() << std::endl;
std::cout << "-1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(-1400ms).count() << std::endl;
std::cout << "-1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(-1500ms).count() << std::endl;
std::cout << "-1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(-1600ms).count() << std::endl;
std::cout << "floor:" << std::endl;
std::cout << "1.4s: " << std::chrono::floor<std::chrono::seconds>(1400ms).count() << std::endl;
std::cout << "1.5s: " << std::chrono::floor<std::chrono::seconds>(1500ms).count() << std::endl;
std::cout << "1.6s: " << std::chrono::floor<std::chrono::seconds>(1600ms).count() << std::endl;
std::cout << "-1.4s: " << std::chrono::floor<std::chrono::seconds>(-1400ms).count() << std::endl;
std::cout << "-1.5s: " << std::chrono::floor<std::chrono::seconds>(-1500ms).count() << std::endl;
std::cout << "-1.6s: " << std::chrono::floor<std::chrono::seconds>(-1600ms).count() << std::endl;
return 0;
}
.
duration_cast:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -1
-1.5s: -1
-1.6s: -1
floor:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -2
-1.5s: -2
-1.6s: -2
https://wandbox.org/permlink/SsmpRz6RkvbL6Sru
I have a problem with a school homework. I have to write a few function, and I have problem with the ShiftPointerVal which argument is a pointer to int. This is the most important in that homework, I mean that argument. I have that issue: error: lvalue required as left operand of assignment. Target of this function is to make a pointer bigger by one. I don't know how to write this function so the pointer to int can be an argument.
void ShiftPointerVal (int& pointer)
{
&pointer = &pointer + 1;
}
void ShiftPointerRef (int *&pointer)
{
pointer = pointer + 1;
}
void ShiftPointerPointer (int **pointer)
{
*pointer = *pointer +1;
}
int main ()
{
int number = 5;
int* p_number = &number;
std::cout << "\n----- 4 -----\n";
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
ShiftPointerVal (p_number);
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
std::cout << "\n----- 5 -----\n";
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
ShiftPointerRef (*&p_number);
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
std::cout << "\n----- 6 -----\n";
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
ShiftPointerPointer (&p_number);
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
}