我一直在使用Valgrind来查找我的代码中的内存泄漏,虽然没有发现内存泄漏,但是报告了一些错误,它们全部来自单个函数/类方法:
==17043== ERROR SUMMARY: 10100 errors from 3 contexts (suppressed: 0 from 0)
==17043==
==17043== 100 errors in context 1 of 3:
==17043== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==17043== at 0x5441DA2: send (send.c:28)
==17043== by 0x404C2D: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043== by 0x404F1C: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043== by 0x401F2A: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043== Address 0x7feffff61 is on thread 1's stack
==17043== Uninitialised value was created by a stack allocation
==17043== …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用FetchContent
CMake 帮助程序自动构建所需的依赖项,以防它们未通过安装/可用find_package
,但在如何处理导出构建目标方面遇到了困难。
我自己的项目例如需要TinyXML2作为依赖项:
## TinyXML2
find_package(tinyxml2 QUIET)
if (${tinyxml2_FOUND})
message(STATUS "tinyxml2 FOUND!(${tinyxml2_LIBRARIES})")
echo_all_cmake_variable_values()
else()
message(STATUS "tinyxml2 NOT FOUND, fetching from source!")
FetchContent_Declare(tinyxml2
GIT_REPOSITORY https://github.com/leethomason/TinyXML2
GIT_TAG 9.0.0
)
FetchContent_MakeAvailable(tinyxml2)
set(tinyxml2_LIBRARIES tinyxml2)
endif()
Run Code Online (Sandbox Code Playgroud)
然后用于链接项目目标:
# ...
target_link_libraries(${PROJECT_NAME} PUBLIC ${Boost_LIBRARIES}
pthread
${tinyxml2_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)
然后导出链接的目标:
# ...
export(EXPORT ${PROJECT_NAME}Targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::)
Run Code Online (Sandbox Code Playgroud)
问题是,从源获取时,导出步骤失败:
CMake Error in src/myproject/CMakeLists.txt:
export called with target "myproject" which requires target "tinyxml2" that
is not in any export set.
Run Code Online (Sandbox Code Playgroud)
有没有办法也自动导出tinyxml2
为必需的依赖项?
是否可以创建通过"虚拟"串行端口发送数据的"虚拟"串行设备?我需要开发一些代码来与Arduino进行交互,但是不需要它.可以用socat或写入dev/ttyXXX
文件的一些代码来完成吗?
编辑:我正在运行Arch Linux
我在我的代码上使用了Eigen的MatrixXd矩阵,在某个时刻我需要一个3D矩阵.由于Eigen不具有三维矩阵类型,因为它仅针对线性代数进行了优化,而是创建了MatrixXd类型的指针数组:
Eigen::MatrixXd *CVM =new Eigen::MatrixXd[100];
for (int i = 0; i < 100; i++){
CVM[i]= Eigen::MatrixXd::Zero(5,5);
}
Run Code Online (Sandbox Code Playgroud)
但是,稍后我需要访问此数组的值,为此我做的事情如下:
for (int k = 0; k < 100; k++){
Eigen::MatrixXd* b=&CVM[k];
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
b->coeff(i,j)=47;
}
}
}
Run Code Online (Sandbox Code Playgroud)
作为b
一个指针,而不是它MatrixXd
自己,b(i,j)
显然不会工作,所以我使用该coeff()
方法,但是,我收到以下错误:
error: assignment of read-only location ‘b->Eigen::Matrix<double, -1, -1>::<anonymous>.Eigen::PlainObjectBase<Derived>::coeff<Eigen::Matrix<double, -1, -1> >(((Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1> >::Index)i), ((Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1> >::Index)j))’
Run Code Online (Sandbox Code Playgroud)
编辑:输出添加
cout …
Run Code Online (Sandbox Code Playgroud) 我的代码需要一个 3D 矩阵/数组结构,现在我的矩阵和向量都依赖于 Eigen。
现在我正在使用new
以下方法创建 3D 结构:
MatrixXd* cube= new MatrixXd[60];
for (int i; i<60; i++) cube[i]=MatrixXd(60,60);
Run Code Online (Sandbox Code Playgroud)
和访问值:
double val;
MatrixXd pos;
for (int i; i<60; i++){
pos=cube[i];
for (int j; j<60; j++){
for (int k; k<60; k++){
val=pos(j,k);
//...
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,现在这部分代码的速度非常慢,这让我相信这可能不是最有效的方法。有没有其他选择?
我试图将一个整数连接到一个已知的字符串,我发现有几种方法可以做到这一点,其中两个是:
int num=13;
string str = "Text" + static_cast<ostringstream*>( &(ostringstream() << num) )->str();
Run Code Online (Sandbox Code Playgroud)
或者我也可以使用boost
库' lexical_cast
:
int num=13;
string str= "Text" + boost::lexical_cast<std::string>(num);
Run Code Online (Sandbox Code Playgroud)
是否boost::lexical_cast
以任何方式使用更高效,因为我已经知道转换类型(int
to string
)?或者static_cast
同样有效,而不必依赖外部库?
我正在编写一些使用boost::asio
类读取和写入串行设备的代码.但是,当在程序之间发送多个字符串时,我注意到在接收程序中,数据在写入串口时按顺序读取,而不是从其他程序发送数据 - 如果我开始读取数秒之后的数据,我不会得到我现在发送的值,而是之前发送的值.我假设这是由我如何设置我的boost::asio::serial_port
:
int main(int argc, char const *argv[]){
int baud=atoi(argv[1]);
std::string pty=argv[2];
printf("Virtual device: %s\n",pty.data());
printf("Baud rate: %d\n",baud);
boost::asio::io_service io;
boost::asio::serial_port port(io, pty);
port.set_option(boost::asio::serial_port_base::baud_rate(baud));
// counter that writes to serial port in 1s intervals
int val=0;
while (1){
std::string data=std::to_string(val);
data+='\n';
std::cout << data;
write(port,boost::asio::buffer(data.c_str(),data.size()));
sleep(1);
val++;
data.clear();
}
port.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有一种方法可以在将新值发送到串行端口时强制将过去的数据丢弃(我假设应该在代码的write()部分完成)?
使用'='为类成员设置一些值并提供其他参数的正确语法是什么?例如矢量中的位置:
MyClass<float> mt;
mt(2,4) = 3.5;
Run Code Online (Sandbox Code Playgroud)
我试过了:
template <class _type>
_type myClass<_type>::operator()(int r,int c) {
return data[r*nCols+c];
};
template <class _type>
myClass<_type>::operator= (int r, int c, _type val) {
data(r,c) = val;
};
Run Code Online (Sandbox Code Playgroud)
但编译器告诉我,我可以使用1参数覆盖'='运算符.
A
对于标量和容器类型的参数,我正在重载具有不同输入类型的模板化类的构造函数:
template<typename T>
class A {
public:
A();
A(T&& _val) { printf("non-template constructor\n");} ;
template<typename iT> A(const iT& _cont) { printf("template constructor\n");};
};
int main(int argc, char const *argv[]) {
A<float> foo1(0.9); //template constructor
A<float> foo2((float)0.9); //no-template constructor
A<float> foo3(std::vector<int>(5,8)); //template constructor
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,有没有办法在隐式可转换类型上调用非模板构造函数,例如传递double
给构造函数A<float>()
?
我一直在研究如何使用可变数量的参数声明函数或类成员,并遇到了可变参数函数,但是我想知道是否有某种方法可以访问传递给函数的参数数量,而不必通过正如大多数文档所介绍的那样,它直接作为第一个参数。我也知道我可以使用可变参数模板 或std::initializer_list
,但由于我希望传递相同类型的多个参数,这些参数似乎过于通用和/或语法复杂。
#include <cstdarg>
bool func(int args...) {
va_list list;
va_start(list, args);
int val = args;
while(val >=0) {
std::cout << val << std::endl;
val = va_arg(list, int);
}
return true;
}
bool other_func(int c, int args...) {
va_list list;
va_start(list, args);
int val = args;
for (int i = 0; i<c; i++) {
std::cout << val << std::endl;
val = va_arg(list, int);
}
return true;
}
int main(int argc, char const …
Run Code Online (Sandbox Code Playgroud) c++ ×9
boost ×2
class ×2
eigen ×2
matrix ×2
pointers ×2
serial-port ×2
templates ×2
arduino ×1
arrays ×1
boost-asio ×1
build ×1
c++11 ×1
casting ×1
cmake ×1
constructor ×1
dependencies ×1
operators ×1
overriding ×1
socat ×1
sockets ×1
string ×1
tty ×1
valgrind ×1
variadic ×1