我在我的一个小项目上探索poll()函数,我注意到这个片段崩溃了:
ErrorCode XNotifier_Linux::updatePoll()
{
ErrorCode ret = Success;
struct pollfd descriptors = { m_fd, IN_MODIFY, 0 };
const int nbDescriptors = poll(&descriptors, m_fd+1, 10*1000);
if (descriptors.events & (POLLIN | POLLPRI))
ret = DataIsPresent;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
Valgrind在这里非常有帮助,因为它指出了ufdsunitialized 中的poll字段:
==833== Syscall param poll(ufds.fd) points to uninitialised byte(s)
==833== at 0x569CB28: poll (in /lib64/libc-2.14.1.so)
==833== by 0x400F7A: xnot::XNotifier_Linux::updatePoll() (linux.cpp:72)
==833== by 0x400D4B: xnot::XNotifier_Linux::update() (linux.cpp:28)
==833== by 0x400FF4: main (linux.cpp:90)
==833== Address 0x7fefffbb8 is on thread 1's stack
Run Code Online (Sandbox Code Playgroud)
正如descriptors在堆栈上创建的那样,我理解当函数返回时,指针 …
我在Linux薄荷12.
我想运行一个程序usr/share/application/firefox,然后在任何地方传递一个字符串.我还没有找到适用于Linux的解决方案,但从目前为止我所看到的,Windows有很多理论.
size_t ExecuteProcess(std::wstring FullPathToExe, std::wstring Parameters, size_t SecondsToWait)
{
size_t iMyCounter = 0, iReturnVal = 0, iPos = 0;
DWORD dwExitCode = 0;
std::wstring sTempStr = L"";
/* - NOTE - You should check here to see if the exe even exists */
/* Add a space to the beginning of the Parameters */
if (Parameters.size() != 0)
{
if (Parameters[0] != L' ')
{
Parameters.insert(0,L" ");
}
}
/* The first parameter needs to be the …Run Code Online (Sandbox Code Playgroud) 我刚编译了这个简单的片段:
#include <iostream>
#include <string>
std::string foo()
{
return std::string("bar");
}
int main()
{
std::string test = foo();
std::cout << test << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用-O2优化,只发现正在创建两个std :: string对象.当我转储二进制文件时,objdump显示~basic_string被调用两次.
0000000000400900 <main>:
400900: 53 push %rbx
400901: 48 83 ec 10 sub $0x10,%rsp
400905: 48 89 e7 mov %rsp,%rdi
400908: e8 73 01 00 00 callq 400a80 <foo()>
40090d: 48 89 e6 mov %rsp,%rsi
400910: bf 80 10 60 00 mov $0x601080,%edi
400915: e8 a6 ff ff …Run Code Online (Sandbox Code Playgroud) 我正在创建一个自定义 ostream 类,它在以下代码段中简要介绍。我想能用std::endl但是编译器不让我用。我不明白为什么。
#include <iostream>
struct Bar
{
};
template <typename T>
struct Foo
{
};
template <typename T, typename U>
Foo<T>& operator<<(Foo<T>& _foo, U&&)
{
return _foo;
}
int main()
{
Foo<Bar> f;
f << "aa" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
gcc 4.7.1 给我的错误是:
main.cpp:21:21: 错误: 'operator<< ((* & f), (*"aa")) << std::endl' main.cpp:21 中的 'operator<<' 不匹配: 21:注意:候选是:main.cpp:13:9:注意:模板Foo& operator<<(Foo&, U&&) main.cpp:13:9:注意:模板参数推导/替换失败:main.cpp:21: 21:注意:
无法推导出模板参数“U”
为什么不能推导出参数U?这不应该typeof(std::endl)吗?
考虑这个功能:
void foo(int * p)
{
// something
}
Run Code Online (Sandbox Code Playgroud)
编译器是否可以假设没有其他线程会修改p指向的值?或者它是否必须表现得好像可以随时修改此值?
void bar(volatile int * p)
{
}
Run Code Online (Sandbox Code Playgroud)
如果没有,volatile关键字是否有帮助?GOTW#69指出,volatile编译器通常会忽略关键字.
编辑:显然对"编译器假定"这一短语存在一些误解.让我澄清一下:
*p在第一次读取时将值放入寄存器,然后使用它直到p超出范围.此时,它应该写入该*p内存地址的值.*p读取时,编译器都应该从内存中获取它的值,因为有一些其他线程可能会修改它.每次*p更改时,编译器都应将其写入内存,以便其他线程可以读取它我有一个std::vector包含很多元素.由于向量很大,我将指针存储到另一个向量中的某些特定元素,以便能够更快地访问它们.但是随着向量的增长,有时它的内部容器会被重新分配,导致我的所有指针都变得无效.
有没有办法知道这种情况何时发生?这样我就可以更新其他列表中的指针.
我的老师刚刚将此作为即将到来的C++期中考试的几个程序示例之一.代码是:
//What does this program print?
#include <iostream>
using namespace std;
#define foo1( a ) a * a
int j = 6;
inline int
foo2( int a )
{
return a * a;
}
void
goo1( int& x )
{
x = 3;
}
void
goo2( int x )
{
x = 3;
}
int main()
{
int i, j = 12;
cout << "foo1 = " << foo1( 3 + 2 ) << "\n";
cout << "foo2 = " …Run Code Online (Sandbox Code Playgroud) 典型的C库看起来像这样:
foo_t * foo_open();
int foo_query( foo_t * );
int foo_close( foo_t * );
Run Code Online (Sandbox Code Playgroud)
我可以看到两种方法将它们包装成sleak RAII结构.我可以创建一个类并包装每个C函数:
class foo
{
public:
foo(): m_impl( foo_open() ) { }
~foo() noexcept { foo_close( m_impl ); }
int query() { return foo_query( m_impl ) };
};
Run Code Online (Sandbox Code Playgroud)
或者我可以使用带有自定义析构函数的智能指针:
class foo_destructor
{
public:
void operator()( foo_t * const obj ) noexcept
{
foo_close( obj );
}
};
typedef std::unique_ptr< foo_t, foo_destructor > foo_ptr;
Run Code Online (Sandbox Code Playgroud)
并直接使用C接口.
int main()
{
foo_ptr my_foo( foo_open() );
foo_query( my_foo.get() );
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在使用第二种解决方案,但主要是因为我懒得编写所有成员函数.是否有理由认为一种方法应该优先于另一种方法?
考虑这个例子:
#include <string>
#include <chrono>
#include <atomic>
#include <thread>
#include <iostream>
std::string some_variable;
void writer_thread()
{
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
some_variable = "done";
}
int main()
{
{
std::thread w( &writer_thread );
w.join();
}
std::cout << some_variable;
}
Run Code Online (Sandbox Code Playgroud)
我是否有必要添加同步机制以确保some_variable正确读取main()?
换句话说:连接或破坏std::thread对象是否意味着刷新与其局部变量相关联的存储器?
c++ multithreading synchronization c++-standard-library c++11