如果成员函数尝试执行操作会发生什么delete this;,例如在以下类的构造函数中?
class A
{
public:
A(int);
~A();
int *pi;
}
A::A(int i)
{
delete this;
pi = new int(i);
}
A::~A()
{
delete pi;
}
Run Code Online (Sandbox Code Playgroud) 考虑到成本,这些情况是否相同?
// case 1
int a = 5;
// case 2
int a (5);
// case 3
int a;
a = 5
Run Code Online (Sandbox Code Playgroud) 在C++中设计堆栈时,当堆栈为空时,pop()方法(或front()方法)应该返回什么?以下哪种设计更好?
好的,我看到我的问题不是那么清楚,让我试着改写它:
有一些数据结构可以基于链表,如堆栈,队列来实现,并且每个数据结构都有一个返回前端元素(或尾部)的方法.
我想知道,当数据为空时,是否有关于设计这种方法的原则指南.
而我对更好的定义是"易于正确使用且难以正确使用".
通过使用valgrind进行检查,我看到在终止程序后没有释放5块内存,但它们仍然可以访问.我需要被它打扰吗?
它是如何发生的?
zhanwu@gelata:~/sandbox$ valgrind ./a.out
==2430== Memcheck, a memory error detector
==2430== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2430== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==2430== Command: ./a.out
==2430==
Hello world!
Thread1 returns 1
Thread2 returns 10
Thread3 returns 10
==2430==
==2430== HEAP SUMMARY:
==2430== in use at exit: 1,590 bytes in 5 blocks
==2430== total heap usage: 14 allocs, 9 frees, 2,442 bytes allocated
==2430==
==2430== LEAK SUMMARY:
==2430== definitely …Run Code Online (Sandbox Code Playgroud) 在Scott Meyers的Effective C++中,第18项使接口易于正确使用且难以正确使用,他提到了null shared_ptr:
std::tr1::shared_ptr<Investment> pInv(static_cast<Investment*>(0), getRidOfInvestment)
Run Code Online (Sandbox Code Playgroud)
和一个时尚分配操作
pInv = ... //make retVal point to the correct object
Run Code Online (Sandbox Code Playgroud)
在这种情况下,可能需要创建一个null shared_ptr并稍后进行分配?为什么不在有资源(原始指针)时创建shared_ptr?
由于Scott Meyers没有在前面的例子中显示完整的赋值,我认为shared_ptr的assign运算符是重载的,可以这样做:
pInv = new Investment; // pInv will take charge of the pointer
// but meanwhile keep the delete function it already had
Run Code Online (Sandbox Code Playgroud)
但我尝试使用boost的实现它不会这样工作.那么null shared_ptr是什么意思?
我几乎可以肯定我在这里遗漏了一些东西,有人帮我解决了.
PS.更多关于shared_ptr的初始化和赋值
#include <boost/shared_ptr.hpp>
int main(int argc, char *argv[])
{
boost::shared_ptr<int> ptr1(new int);
boost::shared_ptr<int> ptr2;
ptr2.reset(new int);
boost::shared_ptr<int> ptr3 = new int;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个例子不能由g ++(Ubuntu/Linaro …
我今天安装了Python 2.7,使用:
./configure --prefix=/home/zhanwu/local --enable-shared --enable-profiling --with-pydebug
make install
Run Code Online (Sandbox Code Playgroud)
然后我在每个函数调用后继续在屏幕上得到类似"[37745 refs]"的内容:
[zhanwu@cluster ~]$ ~/local/bin/python
Python 2.7.1 (r271:86832, Jun 16 2011, 17:45:05)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
[37745 refs]
>>> print 'test'
test
[37745 refs]
>>> sys.exit()
[18048 refs]
[zhanwu@cluster ~]$
Run Code Online (Sandbox Code Playgroud)
这些数字是什么意思?这里有什么问题,我可以摆脱它们吗?
uname -a结果:
[zhanwu@cluster ~]$ uname -a
Linux cluster.xxx.xxx.xxx 2.6.18-128.1.14.el5 #1 SMP Wed Jun 17 06:38:05 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud) 我是一名新测试人员,在阅读遗留代码时,我有以下两个类:
public class TestCommon : Component
{
public void Initialize()
{
var serviceContainer = (IServiceContainer)this.GetService(typeof(TestFramework));
serviceContainer.AddService(typeof(TestCommon), this);
}
}
public class TestFramework : ISite, IServiceContainer
{
readonly Hashtable services = new Hashtable();
public TestFramework()
{
this.AddService(this);
var bedrockModuleInstance = (TestCommon)Activator.CreateInstance(typeof(TestCommon));
((TestCommon)bedrockModuleInstance).Site = this;
((TestCommon)bedrockModuleInstance).Initialize();
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么在TestCommon类的Initialize方法中,可以调用GetService并以某种方式返回TestFramework的'GetService被调用?我尝试通过阅读有关容器,组件和站点的MSDN来理解它,但无法理解Site的想法.
更新: 阅读GetService的实现,发现组件的GetService实际上返回其网站的GetService,回答了我的问题.
protected virtual object GetService(Type service) {
ISite s = site;
return((s== null) ? null : s.GetService(service));
}
Run Code Online (Sandbox Code Playgroud) 使用谷歌应用引擎:
# more code ahead not shown
application = webapp.WSGIApplication([('/', Home)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
如果两个不同的用户在两台不同的机器上请求网页,那么将调用服务器的两个单独实例?
或者只是服务器的一个实例一直在运行以处理所有请求?
如果一个用户在同一个浏览器中打开网页两次怎么样?
编辑:
根据下面的答案,一个实例可以轮流处理来自不同用户的请求.然后考虑以下几部分代码,取自Google给出的示例:
class User(db.Model):
email = db.EmailProperty()
nickname = db.StringProperty()
Run Code Online (Sandbox Code Playgroud)
1,这里的电子邮件和昵称被定义为类变量?2,同一服务器实例处理的所有请求共享相同的变量,因而错误地相互干扰?(比如,一个人的电子邮件出现在另一个人的页面中)
PS.我知道我应该阅读手册和文档更多,我正在这样做,但是经验丰富的程序员的答案将真正帮助我更快地理解,更多通过,谢谢
使用矢量化替换for循环可以显着提高Matlab程序的速度.是因为矢量化代码是并行运行的吗?
矢量化对于使用NumPy或uBLAS的程序也有益吗?
我有一个尺寸为(100*100)的标签矩阵,存储为numpy数组,我想用pyglet显示矩阵.
我最初的想法是使用这个矩阵使用函数pyglet.image.ImageData()形成一个新的pyglet图像.它需要imagedata的缓冲区作为输入,但是我不知道如何从numpy数组中获得正确的格式化缓冲区.
任何人都有任何想法?
PS.我目前的解决方案
3d_label = numpy.empty([100,100,3])
3d_label[:,:,0] = label * 255 # value range of label is [0,1]
3d_label[:,:,1] = label * 255
3d_label[:,:,2] = label * 255
image_data = ctypes.string_at(id(3d_label.tostring())+20, 100*100*3)
image = pyglet.image.ImageData(100, 100, 'RGB', image_data, -100*3)
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法从3 [100*100]矩阵构造一个[100*100*3]矩阵与numpy?