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 | |
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.
|
|
|
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....
|