关于指针的问题 [英] A question about pointer

查看:82
本文介绍了关于指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要我编写c ++代码,我就会担心

指针。系统越多,指针就越危险

我认为。

我必须通过指针,并且有办法

确保任何指针指向的每个对象都将被删除并且只删除一次?

As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?

推荐答案

Bo Yang写道:
Bo Yang wrote:

只要我编写c ++代码,我就会担心

指针。系统越多,指针就越危险

我认为。

我必须通过指针,并且有办法

确保删除任何指针所指向的每个对象

,并且只删除一次?
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?



编号自由是责任,但为了减少泄密的风险,你可以避免使用原始指针和数组来支持RAII技巧:

从不分配资源(例如,内存等)而不将其分配给

一个会自动释放它的对象。一个启用

这类技术的类是std :: tr1 :: shared_ptr,另一个是std :: vector

(参见 http://parashift.com/c++-faq-lite/co...html# faq-34.1)


干杯! --M

No. With freedom comes responsibility, but to reduce the risk of leaks,
you can avoid using raw pointers and arrays in favor RAII techniques:
never allocate a resource (e.g., memory, etc.) without assigning it to
an object that will automatically release it. One class that enables
such techniques is std::tr1::shared_ptr, and another is std::vector
(cf. http://parashift.com/c++-faq-lite/co...html#faq-34.1).

Cheers! --M




Bo Yang写道:

Bo Yang wrote:

只要我编写c ++代码,我就会担心

指针。系统越多,指针就越危险

我认为。

我必须通过指针,并且有办法

确保删除任何指针所指向的每个对象

,并且只删除一次?
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?



您的目标是直接编写整个程序而不使用指针

。甚至不是一个。

请注意,如果需要多态,引用就可以了。

它比你想象的容易。因为没有裸指针意味着没有错误而且没有

阿司匹林。

用适当的智能指针替换任何分配并使用STL

容器(支持复制语义)。

用引用替换指针,优选 - const引用,其中

合适。


#include< iostream>

#include< ostream>

#include< vector>

#include< iterator>

#include< boost / shared_ptr.hpp>


template< typename T>

A级{

T t;

public:

// ctor

A(const T& val = T()):t(val)

{

std :: cout<< A()\ n;;

}

//复制ctor

A(const A& copy)

{

std :: cout<< 复制A \ n;

t = copy.t;

}

// d~tor

~A(){std :: cout<< "〜A()\\\
英寸; }

//朋友op<<

朋友std :: ostream&

operator<<(std :: ostream& os, const A& r_a)

{

return os<< r_a.t;

}

};


int main(){

{

std :: vector< A< double vd(5,11.1); // 5个元素

std :: copy(vd.begin(),

vd.end(),

std :: ostream_iterator< A< double(std :: cout," \ n"));

}


std :: cout<< 使用shred_ptr分配... \ n;;

boost :: shared_ptr< A< std :: string sp_a(new A< std :: string>(" a

short string"));

std :: cout<< * sp_a<< std :: endl;


返回0;

}


/ *

A()< -temp

副本A

副本A

副本A

副本A

复制A

~A()< - 临时破坏

11.1

11.1

11.1

11.1

11.1

~A()< - 5个自动析构函数

~A()

~A()

~A()

~A()

使用shred_ptr分配......

A()

a短串

~A()< - 自动

* /


如果您对策略有任何疑问,请更改

std :: vector< A< double vd(5,11.1); // 5个元素



std :: vector< A< double vd(1000000); //一百万


为了便于比较:相当于一个const引用是指向const值的

const指针。

int n;

const r_n&(n);

const p_n * const(& n);


使用指针是不够的,使用非常量指针可能是非常危险的,这些应该是不可变的。

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.
Replace any allocation with the appropriate smart pointer and use STL
containers (which support copy semantics).
Replace pointers with references, prefereably - const references where
appropriate.

#include <iostream>
#include <ostream>
#include <vector>
#include <iterator>
#include <boost/shared_ptr.hpp>

template< typename T >
class A {
T t;
public:
// ctor
A(const T& val = T()) : t(val)
{
std::cout << "A()\n";
}
// copy ctor
A(const A& copy)
{
std::cout << "copy A\n";
t = copy.t;
}
// d~tor
~A() { std::cout << "~A()\n"; }
// friend op<<
friend std::ostream&
operator<<(std::ostream& os, const A& r_a)
{
return os << r_a.t;
}
};

int main() {
{
std::vector< A< double vd(5, 11.1); // 5 elements
std::copy( vd.begin(),
vd.end(),
std::ostream_iterator< A< double (std::cout, "\n") );
}

std::cout << "allocating using shred_ptr...\n";
boost::shared_ptr< A< std::string sp_a(new A< std::string >("a
short string"));
std::cout << *sp_a << std::endl;

return 0;
}

/*
A() < -temp
copy A
copy A
copy A
copy A
copy A
~A() <- temp destruction
11.1
11.1
11.1
11.1
11.1
~A() <- 5 automatic destructors
~A()
~A()
~A()
~A()
allocating using shred_ptr...
A()
a short string
~A() <- automatic
*/

And if you have any doubt about the strategy, change
std::vector< A< double vd(5, 11.1); // 5 elements
to
std::vector< A< double vd(1000000); // one million

For the sake of comparison: the equivalent of a const reference is a
const pointer to a const value.
int n;
const r_n&(n);
const p_n* const(&n);

Using pointers is iffy enough, using non-const pointers can be
downright dangerous where these should be immutable.




Bo Yang skrev:

Bo Yang skrev:

只要我编写c ++代码,我就会担心

指针。系统越多,指针就越危险

我认为。

我必须通过指针,并且有办法

确保删除任何指针所指向的每个对象

,并且只删除一次?
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?



为什么你必须到处传递指针?一个合适的C ++程序很少需要原始指针,内存泄漏/无效指针访问不是正确编写的代码中的严重问题。

如果您愿意,可以安装垃圾收集器。在这种情况下,你不需要维护内存资源。但请记住,其他

资源仍然必须保持。我从来没有需要使用

垃圾收集器作为智能指针,例如boost提供的那些,

但是其他类型的应用程序可能还有其他需求。 br />

/ Peter

Why must you pass pointers everywhere? A proper C++ program rarely
needs raw pointers, and memory leaks/invalid pointer access is not a
serious problem in correctly written code.
If you so desire, you can install a garbage collector. In that case you
do not have to maintain memory ressources. But remember that other
resources still must be maintained. I never have had a need to use a
garbage collector, as smart pointers such as those provided by boost,
but I other types of applications might have other needs.

/Peter


这篇关于关于指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆