我是shared_ptr的新手,我正试图弄清楚.reset()函数的确切功能.
#include <memory>
#include <stdio>
using namespace std;
class SomeClass{};
int main()
{
shared_ptr<SomeClass> sp (nullptr);
//do some stuff, sp now has 10 co-owners
cout << sp.use_count << endl;
sp.reset();
cout << sp.use_count << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
会输出
10
0
Run Code Online (Sandbox Code Playgroud)
因为我使用了reset函数是从内存中删除了所有实例吗?因为,我刚刚用sp消除了任何可能的内存泄漏?显然,这是一个我快速编写的玩具示例,对不起,如果有任何错误.
跟进情况:
shared_ptr<SomeClass> returnThis() {
shared_ptr<SomeClass> someObject(new SomeClass(/*default constructor for example*/) );
return someObject;
}
Run Code Online (Sandbox Code Playgroud)
在主要的地方:
shared_ptr<SomeClass> mainObject;
mainObject = returnThis();
Run Code Online (Sandbox Code Playgroud)
mainObject的使用次数是否为2,因为someObject是在函数中创建但从未清除过的?或者它是一个,并在返回值时自动完成清理?
我正在编写代码来比较标准C中的两个输入文件,使用Xcode IDE.我一直收到这个错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0).我已经对此做了一些阅读,并认为这是一个内存问题,但无论我尝试什么,我似乎无法修复它(我也尝试使用malloc动态制作结构并列出底部的代码).这很奇怪,因为它会写入所有数据,然后在最后发出错误.文件格式是这样的:start(int).. stop(int)id(+或 - )现在我不关心其余部分的东西我刚刚在一个文件上测试这个只有+ id,所以" - "方面不是问题的一部分.无论如何我已经很累了,已经盯着这几个小时了,所以请原谅我,如果它没有'
typedef struct
{
int start;
int stop;
char *strandID;
} location;
int main(int argc, const char * argv[])
{
if (argc != 4)
{
fprintf(stderr,
"Usage is ./a.out windowfile.txt genefile.txt outputFileName");
exit(-1);
}
//const vars
const char *windowInput = argv[1];
const char *geneInput = argv[2];
const char *outputfile = argv[3];
const int windowHeader = 9;
const int geneHeader = 3;
//get size of structures -- I have debugged and these …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用工具链Crosstool-NG
.我使用Crosstool-NG 1.22.0
的OS X 10.11.4
.
我安装使用homebrew
,所以我不确定为什么我缺少依赖项.当我尝试构建时,这是我的输出:
$ ct-ng build
[INFO ] Performing some trivial sanity checks
[INFO ] Build started 20160504.172530
[INFO ] Building environment variables
[ERROR] Missing: 'x86_64-apple-darwin15.4.0-gcj' or 'x86_64-apple-darwin15.4.0-gcj' or 'gcj' : either needed!
[ERROR]
[ERROR] >>
[ERROR] >> Build failed in step '(top-level)'
[ERROR] >>
[ERROR] >> Error happened in: CT_Abort[scripts/functions@329]
[ERROR] >> called from: CT_TestAndAbort[scripts/functions@351]
[ERROR] >> called from: main[scripts/crosstool-NG.sh@458]
[ERROR] >>
[ERROR] >> For more info on this …
Run Code Online (Sandbox Code Playgroud) 我有一个以下格式的文件:
1: some_basic_info_in_this_line
2: LOTS_OF_INFO_IN_THIS_LINE_HUNDREDS_OF_CHARS
3: some_basic_info_in_this_line
4: LOTS_OF_INFO_IN_THIS_LINE_HUNDREDS_OF_CHARS
...
Run Code Online (Sandbox Code Playgroud)
这种格式重复数万次,文件最多可达50 GiB +.我需要一种有效的方法来处理这种格式的第2行.我愿意使用C,C++ 11 STL或者提升.我已经查看了有关SO上的文件流的各种其他问题,但我觉得我的情况是独特的,因为文件很大,只需要每四行中有一行.
内存映射文件似乎是我读过的最有效的,但是映射50+ GB文件会占用大多数计算机RAM(你可以假设这个应用程序将被"普通"用户使用 - 比如4-8 GiB RAM).此外,我只需要一次处理其中一行.以下是我目前正在做的事情(是的,我知道这不是有效的,这就是为什么我要重新设计它):
std::string GL::getRead(ifstream& input)
{
std::string str;
std::string toss;
if (input.good())
{
getline(input, toss);
getline(input, str);
getline(input, toss);
getline(input, toss);
}
return str;
}
Run Code Online (Sandbox Code Playgroud)
将mmap打破成块可以解决我的情况吗?反正我是否只能利用4行中的1行?谢谢您的帮助.
我在运行10.9.1的相当新的mbp上安装了Kepler.最初这个工作,但现在它不会打开.单击停靠的图标快捷方式将启动eclipse,但小的加载栏稍微超过"加载工作台",然后整个应用程序关闭.在终端中运行会给我这个错误:
Job found still running after platform shutdown. Jobs should be canceled by the plugin that scheduled them during shutdown: org.eclipse.ui.internal.Workbench$55
Run Code Online (Sandbox Code Playgroud)
我读过其他人在小牛的eclipse上遇到Java问题,但我不确定这是我的情况,特别是因为它以前是在运行.请帮忙!
我正在尝试创建一个简单的shell脚本来列出第一个输入6次,一行,然后报告第二个输入的大小.这是我的脚本:
#!/bin/sh
# script1.sh
#
#
# $1=filename $2=number
i=0
while [$i -lt 7] #line 11
do
i=$(($i + 1))
echo $1
done
printf "\n"
if [$2 -gt 1000] #line 19
then
echo 'This is a big number!'
else
echo 'This is a small number.'
fi
Run Code Online (Sandbox Code Playgroud)
以下是我在尝试使用时收到的错误:
./script1.sh test 131234 ./script1.sh:line 11:[0:命令未找到
./script1.sh:line 19:[131234:命令未找到这是一个小数字.
我认为它部分有效,但有关命令-lt和-gt的信息导致错误.在Linux和终端(OS X)上运行会提供相同的错误.
我总是通过引用传递函数原型中的dereferencing,然后在传入参数时引用变量.我最近看到的似乎也是一种通过引用传递的方式,但它的工作方式略有不同.它引用原型中的参数,但在将值输入函数时不期望任何类型的(de)引用.使用我的方法时,必须使用" - >"运算符来访问成员函数,但使用其他方法时,可以使用".".运营商.您能否解释一下这两种方法之间的区别,以及实际中是否有更常用的方法.我在下面有一些示例代码来更好地解释我的意思:
#include <iostream>
#include <string>
using namespace std;
void something(string *byRef, string &dontKnow);
int main(int argc, const char * argv[])
{
string test1 = "test string 1";
string test2 = "second test";
something(&test1, test2);
return 0;
}
void something(string *byRef, string &dontKnow) {
cout << "test1 address = " << byRef << "\nsize = " << byRef->size() << endl;//size function accessed by "->"
cout << "test2 address = " << &dontKnow << "\nsize = " << dontKnow.size() << …
Run Code Online (Sandbox Code Playgroud) 我正在我的系统上安装boost 1.68.0.我想验证使用默认系统编译器(在本例中为GCC 4.4.7)进行安装不会影响利用C++ 14或更高版本的任何增强功能.我的项目使用比GCC 4.4.7更新的编译器 - 我是否通过不使用相同的编译器来限制我的boost功能集?
目前我只使用仅限标题的库,但我想确保一切正常,如果我开始使用更多.
我正在尝试使用终端中的g ++在os x上编译一些c ++代码.但是,我不断收到错误,我不确定它的含义.我有3个文件,main.cpp; comp_fns.cpp和comp_fns.h.Window和Gene是两个不同的类.这是错误:
g++ -Wall main.cpp comp_fns.cpp
duplicate symbol Window::setValues(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in:
/var/folders/jf/3y93rsfd1n55q2qd75y0w0r00000gn/T//cc51aFZg.o
/var/folders/jf/3y93rsfd1n55q2qd75y0w0r00000gn/T//cc2KNfcB.o
duplicate symbol Gene::setValues(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in:
/var/folders/jf/3y93rsfd1n55q2qd75y0w0r00000gn/T//cc51aFZg.o
/var/folders/jf/3y93rsfd1n55q2qd75y0w0r00000gn/T//cc2KNfcB.o
ld: 2 duplicate symbols for architecture x86_64
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,如果需要我可以发布代码.编辑:我没有#include一个cpp文件到另一个.这是我的头文件,我认为问题可能出在那里.这是我的第一个cpp程序,所以可能会有一些明显的错误.我最初用C语言编写它并将其改为c ++,因此我可以学习如何做到这两点.
#ifndef __Compare_Data_C____comp_fns__
#define __Compare_Data_C____comp_fns__
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
class Window {
public:
int …
Run Code Online (Sandbox Code Playgroud)