由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这段code有啥问题?
相关主题
How to overload global new operator?c++ pointers are iterators, why?
这个问题怎么答?一个让我比较困惑的问题 c++ inheritence
why use static function here?[C++] 入门级问题 increment and decrement operators
one question about overloading operator deletewhy copy assignment operator returns non-const type?
operator overloading (C++)one more interview question
请问关于overloading <<C++ Q90 - Q92 (转载)
问题的根源找到了问个指针array 的简单问题
Go不支持operator overload大家觉得C++复杂在哪里?
相关话题的讨论汇总
话题: int话题: new话题: gg话题: pointer话题: return
进入Programming版参与讨论
1 (共1页)
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 的大作中提到】
: 不应该这么做
相关主题
请问关于overloading <<c++ pointers are iterators, why?
问题的根源找到了一个让我比较困惑的问题 c++ inheritence
Go不支持operator overload[C++] 入门级问题 increment and decrement operators
进入Programming版参与讨论
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返回指针是什么意思?

相关主题
why copy assignment operator returns non-const type?问个指针array 的简单问题
one more interview question大家觉得C++复杂在哪里?
C++ Q90 - Q92 (转载)说到cpp11 和 围棋的对比
进入Programming版参与讨论
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得作用我当然知道 问题是什么叫通过这个来返回一个指针?
1 (共1页)
进入Programming版参与讨论
相关主题
大家觉得C++复杂在哪里?operator overloading (C++)
说到cpp11 和 围棋的对比请问关于overloading <<
问个overloading new operator的问题问题的根源找到了
一个inheritance 的问题Go不支持operator overload
How to overload global new operator?c++ pointers are iterators, why?
这个问题怎么答?一个让我比较困惑的问题 c++ inheritence
why use static function here?[C++] 入门级问题 increment and decrement operators
one question about overloading operator deletewhy copy assignment operator returns non-const type?
相关话题的讨论汇总
话题: int话题: new话题: gg话题: pointer话题: return