u***i 发帖数: 489 | 1 int *a;
a =Gg();
int * Gg()
{
int s[8];
return s;
} |
h*******e 发帖数: 225 | 2 compilation error
【在 u***i 的大作中提到】 : int *a; : a =Gg(); : int * Gg() : { : int s[8]; : return s; : }
|
d****p 发帖数: 685 | 3 A classic c++ error: return pointer of local variable.
【在 u***i 的大作中提到】 : int *a; : a =Gg(); : int * Gg() : { : int s[8]; : return s; : }
|
u***i 发帖数: 489 | 4 那如何要返回local variable的address, 该怎么写呢?
【在 d****p 的大作中提到】 : A classic c++ error: return pointer of local variable.
|
z****e 发帖数: 2024 | 5 new。
【在 u***i 的大作中提到】 : 那如何要返回local variable的address, 该怎么写呢?
|
h*****0 发帖数: 4889 | 6 不应该这么做
【在 u***i 的大作中提到】 : 那如何要返回local variable的address, 该怎么写呢?
|
d****p 发帖数: 685 | 7 I will reject you immediately if you answer this way in interview :-)
new returns address in heap in 99% case.
【在 z****e 的大作中提到】 : new。
|
f*****Q 发帖数: 1912 | 8 不准,打手板。
【在 u***i 的大作中提到】 : 那如何要返回local variable的address, 该怎么写呢?
|
z****e 发帖数: 2024 | 9 恩?你fail了我,也得给我个理由吧。不然我去HR投诉你。
99%case什么意思?
函数里边的new,如果返回了裸pointer,应该只是个ownership 问题。
lz不是要这种函数里边生成一个对象和它的指针,然后函数返回这个指针么?
这个不就是用new么?
【在 d****p 的大作中提到】 : I will reject you immediately if you answer this way in interview :-) : new returns address in heap in 99% case.
|
u***i 发帖数: 489 | 10 如果要求 函数格式是Int * fun(xx), 然后返回一个指向array的指针, 该怎么写?
【在 h*****0 的大作中提到】 : 不应该这么做
|
|
|
z****e 发帖数: 2024 | 11 用new啊。如果你那个argument本身不是指针,或者指针的引用的话。
int* f(int size){
int* p=new int[size];
return p;
}
然后记得外边用:
int* p=f(10);
delete [] p;
写?
【在 u***i 的大作中提到】 : 如果要求 函数格式是Int * fun(xx), 然后返回一个指向array的指针, 该怎么写?
|
u***i 发帖数: 489 | 12 要是纯C语法的话, 这样写如何?
int * Gg(int *p)
{
int s[8];
p = s;
return p;
}
【在 z****e 的大作中提到】 : 用new啊。如果你那个argument本身不是指针,或者指针的引用的话。 : int* f(int size){ : int* p=new int[size]; : return p; : } : 然后记得外边用: : int* p=f(10); : delete [] p; : : 写?
|
z****e 发帖数: 2024 | 13 这不都说了是错的吗?
因为s是stack上边的。
要放heap上才行。和C/C++没关系。
必须C的话,就malloc+free吧。
函数里边的stack上边的东西,是永远带不出来的。
【在 u***i 的大作中提到】 : 要是纯C语法的话, 这样写如何? : int * Gg(int *p) : { : int s[8]; : p = s; : return p; : }
|
z****e 发帖数: 2024 | 14 哥们,不是我说话狠,你必须回炉重造了。
【在 u***i 的大作中提到】 : 要是纯C语法的话, 这样写如何? : int * Gg(int *p) : { : int s[8]; : p = s; : return p; : }
|
u***i 发帖数: 489 | 15 呵呵。。确实把C给忘得差不多了。。
【在 z****e 的大作中提到】 : 哥们,不是我说话狠,你必须回炉重造了。
|
d****p 发帖数: 685 | 16 LP asked how to return a pointer to local variable.
Your answer is using new.
If you know the issue,
... you only need to say what he wanted is wrong.
Now I know you know it. But your way to answer this question will fail you
in any interview :-)
the left 1% case is
1. return pointer to local static variable
2. return pointer via placement new or overloaded new operator
【在 z****e 的大作中提到】 : 恩?你fail了我,也得给我个理由吧。不然我去HR投诉你。 : 99%case什么意思? : 函数里边的new,如果返回了裸pointer,应该只是个ownership 问题。 : lz不是要这种函数里边生成一个对象和它的指针,然后函数返回这个指针么? : 这个不就是用new么?
|
z****e 发帖数: 2024 | 17 static? 那不是亲爱的singleton, factory....
难怪我找不找工作啊。原来都所答非所问了。
让我向东,我非向西,让我打狗,我偏叫鸡。
55555555555555555555555555555555555555555.
【在 d****p 的大作中提到】 : LP asked how to return a pointer to local variable. : Your answer is using new. : If you know the issue, : ... you only need to say what he wanted is wrong. : Now I know you know it. But your way to answer this question will fail you : in any interview :-) : the left 1% case is : 1. return pointer to local static variable : 2. return pointer via placement new or overloaded new operator
|
N***m 发帖数: 4460 | 18 叫鸡?哈哈
【在 z****e 的大作中提到】 : static? 那不是亲爱的singleton, factory.... : 难怪我找不找工作啊。原来都所答非所问了。 : 让我向东,我非向西,让我打狗,我偏叫鸡。 : 55555555555555555555555555555555555555555.
|
h****8 发帖数: 599 | 19 请教一下
2. return pointer via placement new or overloaded new operator
通过placement new返回指针是什么意思?
【在 d****p 的大作中提到】 : LP asked how to return a pointer to local variable. : Your answer is using new. : If you know the issue, : ... you only need to say what he wanted is wrong. : Now I know you know it. But your way to answer this question will fail you : in any interview :-) : the left 1% case is : 1. return pointer to local static variable : 2. return pointer via placement new or overloaded new operator
|
z****e 发帖数: 2024 | 20 牛人decamp 说错了,new operator是没有overload的概念的。
只能overload "operator new".
【在 h****8 的大作中提到】 : 请教一下 : 2. return pointer via placement new or overloaded new operator : 通过placement new返回指针是什么意思?
|
|
|
h****8 发帖数: 599 | 21 那么他说的return a pointer via placement new到底是什么意思呢
【在 z****e 的大作中提到】 : 牛人decamp 说错了,new operator是没有overload的概念的。 : 只能overload "operator new".
|
z****e 发帖数: 2024 | 22 就是在指定的内存上边具体化对象。
【在 h****8 的大作中提到】 : 那么他说的return a pointer via placement new到底是什么意思呢
|
h****8 发帖数: 599 | 23 这是placement new得作用我当然知道 问题是什么叫通过这个来返回一个指针?
【在 z****e 的大作中提到】 : 就是在指定的内存上边具体化对象。
|
t****t 发帖数: 6806 | 24 means you get the pointer returned by placement new (which is not allocated
on heap).
【在 h****8 的大作中提到】 : 这是placement new得作用我当然知道 问题是什么叫通过这个来返回一个指针?
|