由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这样会不会造成memory leak?
相关主题
一个interview问题,关于内存泄漏请教boost::any compile错误。
C memory leak problem help问个C++中重复删除指针的问题
VC++ 中的 memory leak problemone question about overloading operator delete
哪位在Linux上用C++碰到过memory fragmentation吗?Dynamic buffer management question
[合集] Memory leak的问题怎样能把go写的稍微漂亮一点?
python一问A weird segmentation fault!
array allocation in c几个问题
在 windows下的C++开发平台是不是 Dev-C++?一个函数指针的问题
相关话题的讨论汇总
话题: results话题: int话题: length话题: delete
进入Programming版参与讨论
1 (共1页)
x******a
发帖数: 6336
1
int *allFactorials(int n)
{
int *results;
int length= (n==0?1:n);
int tmp=sizeof(int)*length;
results=new int [tmp];
if(!results)
return NULL;
DoAllFactorials(n, results, 0);
return results;
}
如果是,应该怎么改?谢谢。
g***i
发帖数: 4272
2
想干嘛,为啥要返回个指针
x******a
发帖数: 6336
3
在 programming interiview exposed上面抄的一段。。。
page 103
改了一句:
results =(int *) malloc(tmp);
会不会有memory leaks?谢谢

【在 g***i 的大作中提到】
: 想干嘛,为啥要返回个指针
y*******g
发帖数: 6599
4
caller 可以call free

【在 x******a 的大作中提到】
: 在 programming interiview exposed上面抄的一段。。。
: page 103
: 改了一句:
: results =(int *) malloc(tmp);
: 会不会有memory leaks?谢谢

d****n
发帖数: 1637
5
DoallFactorials(n,results,0) should be
DoallFactorials(length, results, 0)
In all the cases, length of results is not exactly same as "n", from "int
length= (n==0?1:n);"
d****n
发帖数: 1637
6
either leak or not depends on later GC works.(delete, free)
you cann't say leaks by just when malloc.

【在 x******a 的大作中提到】
: 在 programming interiview exposed上面抄的一段。。。
: page 103
: 改了一句:
: results =(int *) malloc(tmp);
: 会不会有memory leaks?谢谢

r****t
发帖数: 10904
7


【在 x******a 的大作中提到】
: int *allFactorials(int n)
: {
: int *results;
: int length= (n==0?1:n);
: int tmp=sizeof(int)*length;
: results=new int [tmp];
: if(!results)
: return NULL;
: DoAllFactorials(n, results, 0);
: return results;

w***c
发帖数: 245
8
it depends on how the caller releases, use delete[] or just delete. the
results is allocated by new[] so when the caller releases it the delete[]
should be called.

【在 x******a 的大作中提到】
: int *allFactorials(int n)
: {
: int *results;
: int length= (n==0?1:n);
: int tmp=sizeof(int)*length;
: results=new int [tmp];
: if(!results)
: return NULL;
: DoAllFactorials(n, results, 0);
: return results;

y***d
发帖数: 2330
9
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initializa

【在 x******a 的大作中提到】
: int *allFactorials(int n)
: {
: int *results;
: int length= (n==0?1:n);
: int tmp=sizeof(int)*length;
: results=new int [tmp];
: if(!results)
: return NULL;
: DoAllFactorials(n, results, 0);
: return results;

i**h
发帖数: 424
10
delete[] is same as delete if the allocated is of a primitive type (i.e. not
a class).

【在 w***c 的大作中提到】
: it depends on how the caller releases, use delete[] or just delete. the
: results is allocated by new[] so when the caller releases it the delete[]
: should be called.

相关主题
python一问请教boost::any compile错误。
array allocation in c问个C++中重复删除指针的问题
在 windows下的C++开发平台是不是 Dev-C++?one question about overloading operator delete
进入Programming版参与讨论
t****t
发帖数: 6806
11
strictly speaking this is not true... although usually you can get through.

not

【在 i**h 的大作中提到】
: delete[] is same as delete if the allocated is of a primitive type (i.e. not
: a class).

x******a
发帖数: 6336
12
谢谢楼上诸位,请问到底会不会?怎么test有没有?
y***d
发帖数: 2330
13
just do it 10^9 times in your program, without the actual computation.

【在 x******a 的大作中提到】
: 谢谢楼上诸位,请问到底会不会?怎么test有没有?
a**9
发帖数: 86
14
either not return pointer type or use auto_ptr
also, new will not return "NULL" for most compilers....
d****n
发帖数: 1637
15
+1,
there is one scenario could cause a leak I think.
if DoAllFactorials() function alter the content of pointer "result", which
means
results being released or lost during the DoallFactorials call,.
then it is a leak.
My suggest is to declare this function paramters as const type
Void DoAllFactorials(int n, int const * results, int aux)

【在 x******a 的大作中提到】
: int *allFactorials(int n)
: {
: int *results;
: int length= (n==0?1:n);
: int tmp=sizeof(int)*length;
: results=new int [tmp];
: if(!results)
: return NULL;
: DoAllFactorials(n, results, 0);
: return results;

w***c
发帖数: 245
16
that depends on what .lib/.obj is linked during building. if it is
nothrownew.obj or .lib, we can just check NULL after allocation. if not, oh
boy, exception handling for this case is annoying...

【在 a**9 的大作中提到】
: either not return pointer type or use auto_ptr
: also, new will not return "NULL" for most compilers....

1 (共1页)
进入Programming版参与讨论
相关主题
一个函数指针的问题[合集] Memory leak的问题
another c++ interview questionpython一问
再问一个free()的问题array allocation in c
弱问c++里有没有NULL这个keyword?在 windows下的C++开发平台是不是 Dev-C++?
一个interview问题,关于内存泄漏请教boost::any compile错误。
C memory leak problem help问个C++中重复删除指针的问题
VC++ 中的 memory leak problemone question about overloading operator delete
哪位在Linux上用C++碰到过memory fragmentation吗?Dynamic buffer management question
相关话题的讨论汇总
话题: results话题: int话题: length话题: delete