我不久前写了一个程序(Mac OS X,C++,SDL,FMOD),它表现得相当不错.但最近我想扩展其功能并为其添加更多代码.现在,当我运行它并尝试测试新功能时,程序会与SIGABRT崩溃.
查看调试器,在函数堆栈上我看到:
据我所知,"__ stack_chk_fail"表示堆栈溢出.但这不是最奇怪的事情.在这个函数"odtworz"中,我有一些像这样的代码:
...
koniec = 0;
while ( koniec == 0 ) {
...
if (mode == 1) {
...
}
else if (mode == 2) {
...
}
else if (mode == 3) {
piesniOrkiestrowe[0] = '\0';
while ( piesniOrkiestrowe[0] == '\0' ) {
losowaPiesn();
char * piesnOrkiestrowa = szukajPiesniOrkiestrowej();
if ( piesnOrkiestrowa != NULL )
strcpy(piesniOrkiestrowe, piesnOrkiestrowa);
}
char nowyPiesnPlik[25];
sprintf(nowyPiesnPlik, "%sorch/%s", PIESNI_DIR.c_str(), piesniOrkiestrowe); …Run Code Online (Sandbox Code Playgroud) 看看这两个代码.
下面的代码工作正常.
void someFunction () {
// Some unimportant stuff
}
MainM::MainM(QObject *parent) :
QObject(parent)
{
std::thread oUpdate (someFunction);
}
Run Code Online (Sandbox Code Playgroud)
此代码抛出错误:
void MainM::someFunction () { //as a class member
}
MainM::MainM(QObject *parent) :
QObject(parent)
{
std::thread oUpdate (someFunction);
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>)'
std::thread oUpdate (someFunction);
^
Run Code Online (Sandbox Code Playgroud) 考虑class D从a class B和a的sb实例派生std::shared_ptr<B>.一旦我确认了dynamic_cast<D*>(sb.get())可能,我想std::shared_ptr<D>从某人那里创建一个合适的.换句话说,我想dynami_cast<>在shared_ptr之间实现一种.我怎么能以干净的方式做到这一点?一种可能的解决方案是使B 从(铸造)指针派生std::enable_shared_from_this并使用.但这需要改变B类的定义.是否有更好的想法?有什么吗?shared_from_this()Dboost
请考虑以下示例代码:
#include <future>
#include <array>
#include <cassert>
typedef std::array<int, 5> foo_t;
foo_t* bar(foo_t& foo) {
return &foo;
}
int main() {
foo_t foo;
auto a = std::async(bar, foo);
auto b = std::async(bar, foo);
assert(a.get() == b.get());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC 4.6.3编译时没有任何投诉.但是,这在运行时失败:
test: test.cpp:15: int main(): Assertion `a.get() == b.get()' failed.
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
但是,GCC 4.8.2拒绝编译该文件:
In file included from /usr/local/include/c++/4.8.2/future:38:0,
from test.cpp:1:
/usr/local/include/c++/4.8.2/functional: In instantiation of 'struct std::_Bind_simple<std::array<int, 5ul>* (*(std::array<int, 5ul>))(std::array<int, 5ul>&)>':
/usr/local/include/c++/4.8.2/future:1525:70: required from 'std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,动态链接都可以,但是当我尝试使用静态链接进行编译时,我有以下错误.
我的应用程序使用boost线程,asio
错误:
/tmp/ccMj2fHI.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x237): undefined reference to `boost::system::get_system_category()'
test.cpp:(.text+0x243): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x24f): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x25b): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x267): undefined reference to `boost::system::get_system_category()'
/tmp/ccnCWj1O.o: In function `__static_initialization_and_destruction_0(int, int)':
AccountSe.cpp:(.text+0x507): undefined reference to `boost::system::get_system_category()'
AccountSe.cpp:(.text+0x513): undefined reference to `boost::system::get_generic_category()'
AccountSe.cpp:(.text+0x51f): undefined reference to `boost::system::get_generic_category()'
AccountSe.cpp:(.text+0x52b): undefined reference to `boost::system::get_generic_category()'
AccountSe.cpp:(.text+0x537): undefined reference to `boost::system::get_system_category()'
Run Code Online (Sandbox Code Playgroud)
所有源文件都有类似的错误.
编译命令行:
g ++ -L/usr/lib/-lboost_system -lboost_thread -o newserver -static /usr/lib/libboost_thread.a /usr/lib/libboost_system.a stdafx.cpp test.cpp AccountSe.cpp ... -lpthread -std …
我有两种类型:A和B.'聪明'指向B和B'聪明'指向A.在主要范围内,我有一个指向A的智能指针.
class A;
class B;
typedef shared_ptr<A> pA;
typedef shared_ptr<B> pB;
class B {
public:
B() {}
pA a;
virtual ~B() {cout << "delete b" << endl;}
};
class A {
public:
A() : b(new B()) {}
pB b;
virtual ~A() {cout << "delete a" << endl;}
};
int main(int argc, char **argv)
{
{
pA pa(new A());
pa->b->a = pa;
}
cout << "here" << endl;
}
Run Code Online (Sandbox Code Playgroud)
我希望在范围的末尾删除这两个对象.没有任何对象被删除,因为A有两个指向自身的指针(一个在b中,一个在主勺中).
这是一个简单的例子.实际上我有两种以上的类型和更多的指针.可以想象一个大的对象结构指向彼此浮动在RAM中,只有一个指针从主范围指向这个结构.我希望在释放此指针后删除此结构.
我需要解析一个自定义协议的框架,它可以包含各种大小的整数(uint8_t,uint16_t,uint32_t等等)和字符串的前缀长度(uint16_t).
我想编写一个模板函数来从字节向量中读取这些值,以使语法更具可读性.这是我的功能:
template< typename type >
type read( const std::vector< byte > & bytes, uint index )
{
if( index + sizeof( type ) > bytes.size() )
{
throw exception( "read() - Out of range." );
}
type val;
memcpy( & val, & bytes[ index ], sizeof( type ) );
return val;
}
template< std::string >
std::string read( const std::vector< byte > & bytes, uint index ) // ERROR HERE
{
if( index + sizeof( type ) > bytes.size() ) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用c ++ std::ifstream类将二进制文件准备到一组变量中.
以下示例有效:
std::ifstream inFile;
inFile.open("example.bin");
uint8_t temp8;
uint16_t temp16;
inFile >> temp8;
inFile >> temp8;
Run Code Online (Sandbox Code Playgroud)
但是,如果我用一行替换最后两行
inFile >> temp16;
Run Code Online (Sandbox Code Playgroud)
什么都没有读,inFile.fail()返回true.
任何人都可以解释,为什么我不能读入16位变量?
当我试图保持一个关于#includes 的干净头文件时,我发现通过让我的所有成员指向需要包含其他头文件的类/对象,我可以做得更好,因为这样我可以使用前向声明而不是#includes。
但我开始怀疑这是否是一个好的经验法则。
因此,例如采用以下类 - 选项 1:
class QAudioDeviceInfo;
class QAudioInput;
class QIODevice;
class QAudioRx
{
public:
QAudioRx();
private:
QAudioDeviceInfo *mp_audioDeviceInfo;
QAudioInput *mp_audioInput;
QIODevice *mp_inputDevice;
};
Run Code Online (Sandbox Code Playgroud)
但是,这可以像这样重写 - 选项 2:
#include "QAudioDeviceInfo.h"
#include "QAudioInput.h"
#include "QIODevice.h"
class QAudioRx
{
public:
QAudioRx();
private:
QAudioDeviceInfo m_audioDeviceInfo;
QAudioInput m_audioInput;
QIODevice m_inputDevice;
};
Run Code Online (Sandbox Code Playgroud)
选项 1,通常是我的偏好,因为它包含一个更好的清洁标题。但是,选项 2 不需要动态分配/取消分配资源。
也许这可以被视为一个基于主观/意见的问题(我知道意见警察会对此无所适从),所以让我们避免“我喜欢”和“我喜欢”并坚持事实/关键点。
所以我的问题是,一般来说,最佳实践是什么(如果有的话),为什么?或者如果没有一种最佳实践(通常是这种情况),什么时候使用每一种最佳实践?
编辑
抱歉,这实际上是用于私有变量 - 而不是公共变量。
问题是关于GRIB解析器(链接到GRIB文件https://github.com/Gifciak/GRIB),当我执行我的代码时(尽管代码块或通过控制台在linux上g++ main.cpp -pedantic),我遇到了错误,分段错误,但它没有并非总是会发生。
例如,当我编译10次时,将有8次错误,而2次则将一切正常,这将为我提供控制台输出和信息。
正如我研究过的那样,问题在于std::copy,因为它可能正在尝试一个不再存在的复制迭代器。
有人可以解释为什么会发生吗?为什么它不总是崩溃或成功?
#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>
using ByteVec = std::vector<uint8_t>;
template<typename T, size_t size = sizeof(T)>
auto getReverseEndianValue(const auto & iter) {
union {
T result;
char tmp[size];
} buffer;
auto reverseIter = std::make_reverse_iterator(std::next(iter, size));
std::copy(reverseIter, std::next(reverseIter, size), buffer.tmp);
return buffer.result;
}
enum Edition {
Edition_Unknown = -1,
Edition_GRIB1 = 1,
};
namespace section {
class IS {
public:
uint32_t magicFlag;
uint32_t size;
Edition …Run Code Online (Sandbox Code Playgroud)