|
t****t 发帖数: 6806 | 2 A::~A() { for (auto i: children) delete i; } |
|
b***i 发帖数: 3043 | 3 children->parent=a 是什么意思? |
|
l********a 发帖数: 1154 | 4
就是如果构造函数传入第一个参数是当前构造对象的父指针
A::A(A* a,int)
{
this->parent = a;
...
} |
|
l********a 发帖数: 1154 | 5
auto会报错,
我写成类似这样的,还是会出错
for (int i=0;i<(int)(this->children_.size());++i)
{
A *temp = this->children_[i];
delete temp;
} |
|
b***i 发帖数: 3043 | 6 but Children is not A, it's vector |
|
|
t****t 发帖数: 6806 | 8 this is c++11, but you got the essence. in any case, you have other bug in
your code. |
|
|
|
|
j*****g 发帖数: 16 | 12 简单测试一下楼主的class, 发现这句不能compile:
this->children->parent = a;
main.cpp: In constructor ‘A::A(A*, int)’:
main.cpp:25:
error: base operand of ‘->’ has non-pointer type ‘std::vector
allocator >’
这是为什么呢?多谢! |
|
t****t 发帖数: 6806 | 13 他显然只是东拼西凑了一小段给你看一下, 你只能看个意思, copy paste是不成的 |
|
|
t****t 发帖数: 6806 | 15 你不用来来回回重复说明你的设计, 这个设计很简单, 大家都看明白了
大家问的是你混乱的语法问题, 你either没贴对or没贴全 |
|
|
b***i 发帖数: 3043 | 17 这就对了,你写好一个snippet,然后我们放到codepad.org上运行,就可以继续讨论了
另外,你那个A::~A里面的swap是谁跟谁?什么目的? |
|
t****t 发帖数: 6806 | 18 that is most probably copied from effective STL or some other book to "
really" release the vector memory. but it's actually not needed. |
|
t****t 发帖数: 6806 | 19 you read it wrong -- it said when you clear() the vector, or pop_back(), or
erase(), it probably won't release the memory.
but when you delete the vector, it will and it must.
read book carefully. |
|
b***i 发帖数: 3043 | 20 那你就作出一个能出错的snippet,放到codepad上出错,再贴出来代码,好吗?
另外,你怎么不用delete,而是直接~? |
|
|
|
l********a 发帖数: 1154 | 23 多谢2位,可能我代码哪里显式调用deconstructor了 |
|
t****t 发帖数: 6806 | 24 ...跟你说话真够费劲儿的. 都已经给你指出行号了你还"可能". |
|
p***o 发帖数: 1252 | 25 来自主题: Programming版 - map析构
dll
出错原因是第二个有人说:insert在dll外分配内存, clear在dll内释放。
解决方法是第一个有人说:不能垮dll传递stl对象。
这跟是不是new出来的毫无关系,因为stl对象内部需要分配/释放内存。 |
|
b***i 发帖数: 3043 | 26 来自主题: Programming版 - map析构 什么是 ~pair?
写个snippeT |
|
b***i 发帖数: 3043 | 27 来自主题: Programming版 - map析构 if you use map ..... * param=new ...., then you use delete param in
destructor of testGA. if you use auto, then no need to write anything. The
destructor of map will be called.
Show us more code before you make a snippet: including:
testGA yourvariable, what did you do to this variable
anythign done with params inside testGA,
where did you do insert, where is node
what did you do in destructor of testGA.
make |
|
p***o 发帖数: 1252 | 28 来自主题: Programming版 - map析构 This is a well-known problem for C++/dll and there are many solutions.
Here are some.
1 Use dynamic linking for C/C++ library, e.g. msvcrt.dll, instead of
static linking. You don't need to change your code but it probably
won't work across compilers (even different versions).
2 Use C interface across binary modules and keep new/delete malloc/free
in the same binary module. An additional benefit is it can also be
used for other languages.
3 Use an allocator that can work across binary mod... 阅读全帖 |
|
t****t 发帖数: 6806 | 29 来自主题: Programming版 - map析构 我以为dll都是dynamic linking的呢, 我真是out了呀.
好象unix上.so没这问题... |
|
l********a 发帖数: 1154 | 30 来自主题: Programming版 - map析构
thank you so much for your comments. I am sure that you pointed the key
problem of working around with stl variables crossing dlls. I 've found
those solutions you mentioned above on internet.
Unfortunately, I am the only one to take care of such an outdated c library
and port it into c++. I am not sure if the first one (using dynamic linking
for c/c++ library) could solve my problem easily (I doubt it). The last
solution to use an allocator is quite difficult for me at this moment. So,
the bes... 阅读全帖 |
|
p***o 发帖数: 1252 | 31 来自主题: Programming版 - map析构 VC里一直都是这样啊,再加上Debug/Release和Single/Multi-Threaded(现在没Single
了),
发布一个DLL可以有七八个binary ... |
|
p***o 发帖数: 1252 | 32 来自主题: Programming版 - map析构 I would say the library could be outdated, but you can still use
its inferface. That probably will save you a lot of trouble.
library
linking |
|
p*a 发帖数: 592 | 33 来自主题: Programming版 - map析构 编译选项用的/MT还是/MD? |
|
l********a 发帖数: 1154 | 34 来自主题: Programming版 - map析构
/MDd,多线程调试dll |
|
l********a 发帖数: 1154 | 35 来自主题: Programming版 - map析构
今天准备一个函数一个函数的注释掉试验了.
我也觉得我不存在跨dll使用stl的问题,因为所有的自定义类都在同一个dll内部
或许是文件读写和内存分配的时候出的问题.
多谢几位,有问题再来请教 |
|
b***i 发帖数: 3043 | 36 来自主题: Programming版 - map析构 主程序和dll的选项要一致 |
|
l********a 发帖数: 1154 | 37 来自主题: Programming版 - map析构
这里的测试程序是我写的
实际项目中,那个exe是一个很大的程序,我也不确定它是用什么选项编译的
我这里只能试验了吧 |
|
b***i 发帖数: 3043 | 38 来自主题: Programming版 - map析构 不是你一起编译的?
你用什么编译器?那个是什么编译器? |
|
l********a 发帖数: 1154 | 39 来自主题: Programming版 - map析构
是cad软件二次开发,他们怎么编译的我还真不知道,我只负责写个dll供它程序调用
功能执行良好,然后弹出一个heap错误后就程序退出了
应该是哪部分内存分配的问题,c++断续用过,第一次上一个完整的大项目,出了问题到处
试验.
我是vs2008,那个软件不清楚怎么编译的 |
|
b***i 发帖数: 3043 | 40 来自主题: Programming版 - map析构 如果你认为你自己的代码没有问题,不妨加入一些调试信息,看看主程序生成testGA的
实例和释放的时候有没有问题。
dll |
|
c**y 发帖数: 172 | 41 同意
card.h 里只定义这个类
card.cpp 里定义它的析构和解析函数用来初始化和释放动态内存 |
|
|
b*******s 发帖数: 5216 | 43 不用传对象指针,一个函数指针就可以了
另外使用前先检查指针有效性,否则在析构器里面扔出异常要被骂的 |
|
c*****e 发帖数: 3226 | 44 这篇帖子的目的虽然不是为了引发圣战, 但是鄙人已经遇 见到了, 故而添加这个标签,
告诉大家,参与讨论需要谨 慎, 切忌人身攻击, 切记切记!
我不是用黑Python 装逼的, 任何有此想法的人请绕道. 要 装逼有比黑Python 方便的
多的多的办法. 所以要黑Python而不是那些比Python还要烂的语言, 是 因为他们都是
小众语言, 还不足矣需要黑他们
但是Python 这个跳梁小丑越来越嚣张了, 必须揭示一下 其罪恶的一面, 以正视听.
欢迎大家正常的拍砖吐嘈. 这个说来比较久远了, 可能部 分人已经看过了:)
以下为正文, 摘自Q群的讲座. 原始链接 https://avlog.avplayer.org/3597082/
python%E6%98%AF%E4%B8% AA%E7%83%82%E8%AF%AD%E8%A8%80.html
PS: 好奇Q群聊天记录怎么来的? 聊天记录由该群一干人等开发的avbot (Q群云秘书http://qqbot.avplayer.org ) 生成.
--------------------------------------------... 阅读全帖 |
|
|
t****t 发帖数: 6806 | 46 你这里混淆了两个概念(在你bytes.com的link里有人已经提到了): 堆内存的分配/释放
, 即new/delete, 和进程内存的分配/释放, 即brk, 是两个不同的概念. 用ps/top看到
的是第二种, 但STL能控制的基本上是第一种.
先说STL的容器. clear当然不能保证释放, 但是析构函数一定会保证. 所以你如果不能
确定swap()能不能work, 不妨重新分配容器. 性能应该比swap高, 但是你反正是试一下
, 不用太担心.
再说OS. 在linux上, 缺省的glibc的分配模式是, 对于小内存块, 针对不同的尺寸, 有
不同的内存池. 对于大内存块, 是用mmap直接从系统要的. 对于内存池的delete, 就还
给内存池了, OS仍然看不到. 对于mmap的delete, 直接还给OS. 从一方面, 这可以解释
为什么vector是能"完全"释放, 因为vector的内存一定是连续的, 你开一个大vector肯
定是用mmap直接拿的, 删除之后就没了. 对于unordered_set, 内存不连续, 是渐渐分配
的, 很多删除之后还给内存池了, 所... 阅读全帖 |
|
N******K 发帖数: 10202 | 47 我举个例子
矩阵加法 每个都是1024x0124大小
A=B+C+D
具体这样 temp1=B+C; temp2=temp1+D; A=temp2;
这里面很多数据共享的问题
比如 简单的 A=B;
A是一个矩阵 A=B 是不是要把所有元素都复制一遍? 显然不用
A.data=B.data; 就行了
A.data 可以是 shared_part
如果要对A只做读操作 A不用重新分配一块内存
如果要对A做写操作 那就测试一下A.data有没有别人在用 还有人用的话 那就复制一块
A重新分配一块内存 复制A.data指向的数据
当然 你要搞多线程 多个线程写一个矩阵 会出问题 你要加锁
锁。lock()
测试一下A.data有没有别人在用 还有人用的话 A重新分配一块内存
锁。unlock()
不过 数值计算一般不会这么搞
如果B没用了 析构函数里面 {data.reset()} 就可以了 不会把数据真给删掉了 |
|
D***n 发帖数: 6804 | 48 基本上我认为这些特性都是C++的糟粕:
1)模板:模板的本意是把type和control flow分开,多个type可以共享一个control
flow。但是它引发的问题远比带来的方便程度要多。最后大多数模板都搞成一个杂种:
被塞入各种特化。
我的建议:STL一类成熟的可以用,但是能不用就不用。
2)基本操作符重载:当一个正常人看到A=B居然引发了另外一个线程SIGABT的时候,你
会想到这么一个赋值语句居然触发了某个析构函数把内存给清空了的事情吗?面对现实
吧,这个东西引起的麻烦远远超过其便利性。
至少至少,重载的操作符应该和正常符号分开,比如 = 改成 |= 一类。
3)重载:重载是个好主意,问题在:1)库里面这些重载的函数会被替换成别的函数名
称用于链接,这个替换协议不仅不是标准的一部份,而且还巨复杂。2)只能重载基本
类型。基本上就是一个残废功能。
我的方法:与其让系统自动起名字,不如我自己给他起,比如 init_IntInt, init_Int
, int
4)const
当我读到 Thinking in C++ ,作者开始介绍const的时候,我就开始讨厌C++了,因为
c... 阅读全帖 |
|
z****e 发帖数: 54598 | 49 作为论据,我找到了中文的翻译,但是那个youtub链接失效了
Go语言之父谈Go:大道至简
时间:2012-07-05 15:40 作者:王然
导读:这篇文章是Google首席工程师、Go语言之父Rob Pike自己整理的6月21日在旧金
山给Go SF的演讲稿。Rob提到:Go语言本是以C为原型,以C++为目标设计,但最终却大
相径庭。值得一提的是,这3门语言都曾当选TIOBE年度语言。
几个礼拜之前我被问到:“对于Go语言,最令你惊讶的是什么?”当时我就明确地给出
了答案:“虽然我希望C++程序员能够使用Go作为替代拼,但实际上大部分Go程序员都
是从Python和Ruby转过来的,其中却少有C++程序员。”
我、Ken以及Robert都曾是C++程序员,在我们编写软件时觉得应该设计一门更适合解决
这个问题的编程语言。奇怪的是,其他程序员似乎却不关心。
今天我将说说是什么让我们决定创造Go语言的,及其出乎意料的结果。这里我谈的更多
的会是Go而不是C++,所以即使你不懂C++也没关系。
主旨可以简单地总结为:你更同意Less is more还是Less is less?
这里有一个... 阅读全帖 |
|
b***i 发帖数: 3043 | 50 另外,我是把io_service.run()放在线程A里的,而run()在等待的时候确实能够进行服
务器的服务,但是,当我需要结束的时候不知道如何通知这个线程A停止。结果Windows
下出现对话框,Abort, Retry, ...
应该是这个thread A析构的时候出了错。 |
|