H*M 发帖数: 1268 | 1 studying STL now and have a question.
it seems to me that:
1. in STL, if a function is expecting a functor, it is usually ok to use fun
ction pointer; even if it is not ok, you still can use ptr_fun to get an obj
ect;
2. if it is expecting a function pointer, you can not use a functor, like qs
ort.
From what I read, most STL functions are expecting functors. Who can sumariz
e which functions are expecting function pointers only(e.g. qsort)? Or where
can I find such info.? many thanks. |
|
z****e 发帖数: 2024 | 2 你说得那些我一句话就搞定了啊:
。。。usually you lose the pointer arithmetic(这个就包括你说的那些个运算符
重载了)。其他都是pointer arithmetic啊。
STL里边的iterator分类也不一定就必须是所有iterator的分类啊。
iterator不也是一种design pattern 么?
而且iterator 就是一种smart pointer 啊,这个也没错啊。
其实我也没答好,本来想多说一些,却说得有点过了,该详细说说的地方没有展开。
但是我没有什么原则性的错误啊,这些理解都是对的啊。
你说是吧。 |
|
X****r 发帖数: 3557 | 3 除非所有的iterator都是smart pointer,你说iterator是smart pointer
就是错误的。某些iterator是smart pointer不改变这一点。
现在还
有标准的争论么? 我这里大致的用一下,就原则性错误了? |
|
t****t 发帖数: 6806 | 4 这个是哪本书说的啊。要说iterator是generalized pointer就对了。反正你面试要这么
说肯定没分。
通常现在大家接受的说法是,smart pointer不需要手工delete的pointer. iterator基
本上没这功能。C++里shared_ptr算是一个。auto_ptr勉强沾边。 |
|
s********k 发帖数: 6180 | 5 Is it risk to use void pointer in the following case:
typedef struct
{
void *next;
UINT16 xx;
UINT16 yy;
byte zz;
} a;
mostly, I think
struct a
{
struct a *next;
UINT16 xx;
UINT16 yy;
byte zz;
};
could be better, but is there any risk to use void pointer? what we should
pay attention in order to carefully manipulate the pointer in this case?
thanks |
|
t****t 发帖数: 6806 | 6 yes, that *a is a typo. and removing the twist, yes, compiler most probably
will give the result you expect. (but the program remains undefined.)
but no, your understanding is wrong. the target of an arbitrary cast pointer
is not undefined. but you can not alias it. the rule of thumb here is, do
not cast lvalue [and access it], but you can cast rvalue. casting a pointer
to another type is ok, but accessing through that pointer is usually not (
unless it is compatible).
you
you
bit |
|
r****t 发帖数: 10904 | 7 在本版前面看到师傅们说过,不过当时就有疑问:
为啥 pointers are iterators?
只是因为 iterator objects support pointers arithmetic 就这么说么?(比如
sequential containers 可以用一对 pointer 来初始化,这个是不是用的 overload
ctor 来实现的?)总觉得这样逻辑有点不通,倒像是 duck typing 的逻辑了。
还是有啥别的意思在里面? |
|
G****u 发帖数: 51 | 8 char (*str)[] ="abc" <==> char **str="abc" ??
I dont know how you get this. But gcc actually gives warning for
char **str="abc". str is a pointer #1 which points to pointer #2 which
points string "abc"? but where is pointer #2? |
|
t****t 发帖数: 6806 | 9 i didn't say fun2 is wrong, i said it's poorly written. dunno what you are
arguing.
in my previous post, i said as long as it's a pointer it should be ok
(albeit bad style). if it points to something in stack, it won't be
a pointer, it will be an array. an array is never a pointer.
wrong,
,
something
is |
|
B********r 发帖数: 397 | 10
would
In fun1, str is array name of "ABC". When you return an array name whose
content was destroyed after returning, you can't get a right result.
In fun2, str is a pointer which points to an array "ABC". Since "ABC" is
constant and stays in the memory and what you return is the actual address,
you can find "ABC" in main(). (*str)[] just different from *str[] as a
pointer, instead of a pointer array. |
|
m*******l 发帖数: 12782 | 11 no pointer is the best pointer |
|
d****i 发帖数: 4809 | 12 Are you kidding? C++ program without pointers will not be called C++ at all.
It is the bare metal thing that makes pointers so much fun and so much
fascinating. |
|
a***n 发帖数: 538 | 13 boost的shared pointer很慢,前一段时间profile一个程序,shared pointer的计数器
用了5%的cpu。 |
|
t*****r 发帖数: 142 | 14 也许我太out了,smart pointer都不太喜欢用,各位大牛怎么这样讨厌pointer呢?我
觉得太多的地方需要用pointer了。 |
|
c******t 发帖数: 133 | 15 菜鸟弱问:各种容器的sort函数都要两个iterator作为参数,能不能用pointer呢?我
自己理解的是如果内存连续就应该可以用pointer,比如vector,内存不连续就不行,
比如list,可是试了一下,vector也不能用pointer作为参数来排序,为什么?
iterator有哪些特殊的功能?谢谢了! |
|
|
f****4 发帖数: 1359 | 17 “所有的资源都在同一对花括号内获取和释放” != RAII
“所有的资源都在同一对花括号内获取和释放”在异常发生的时候还是不安全的
个人感觉smart pointer好处就是加强了对拷贝支持
对于一个容器动不动就管理10万+的raw pointers,换了smart pointer是不是会有性能
影响,影响多大?
回国一个月,这里居然没再口水java vs c++,真难得啊。。。 |
|
N******K 发帖数: 10202 | 18 现在有 std的smart pointer 以及 第三方库(比如QT)自带的smart pointer
p=Qtfunction(); p是一个 QT内部的smart pointer 指向一个类实例A
如果把这个换成 std shared_ptr 怎么搞? 把A复制一遍? |
|
b*******s 发帖数: 5216 | 19 4.3 Function-to-pointer conversion [conv.func]
1 An lvalue of function type T can be converted to a prvalue of type “
pointer to T.” The result is a pointer tothe function.57
I think it is what happend. |
|
b*******s 发帖数: 5216 | 20 你这个例子是误用
你对arr取地址得到的是指向int[3]对象的指针
和int *是不能implicitly转换的,当然也有workaround
不适用以下规则 (p = arr适用)
这个规则是array obj到指针的转换
4.2 Array-to-pointer conversion [conv.array]
1 An expression of type “array of N T”, “array of runtime bound of T”,
or “array of unknown bound of T” can
be converted to a prvalue of type “pointer to T”. The result is a pointer
to the ?rst element of the array. |
|
b*******s 发帖数: 5216 | 21 4.3 Function-to-pointer conversion [conv.func]
1 An lvalue of function type T can be converted to a prvalue of type “
pointer to T.” The result is a pointer tothe function.57
I think it is what happend. |
|
b*******s 发帖数: 5216 | 22 你这个例子是误用
你对arr取地址得到的是指向int[3]对象的指针
和int *是不能implicitly转换的,当然也有workaround
不适用以下规则 (p = arr适用)
这个规则是array obj到指针的转换
4.2 Array-to-pointer conversion [conv.array]
1 An expression of type “array of N T”, “array of runtime bound of T”,
or “array of unknown bound of T” can
be converted to a prvalue of type “pointer to T”. The result is a pointer
to the ?rst element of the array. |
|
e******r 发帖数: 220 | 23 在科学计算里, 我们一般什么情况用 pointer to pointer(for example double*
*) 传递值?
什么情况下用 pointer (for example double *)来传递值?
|
|
w*******y 发帖数: 60932 | 24 Link:
http://www.meritline.com/led-laser-pointer-key-chain-torch-974---p-35285.aspx
Coupon code: MLCK188974090280AL1
# Brand new 2-LED + laser pointer keychain flashlight.
# Powerful red laser pointer which can be seen over a mile.
# Made with 11000 mcd bright LEDs, brighter than normal LEDs.
# 10000 hours of LED life-time.
# Mini and light weight, great for emergency.
# Aluminium body and durable.
# With clip. You can use it as keychain or clip it at your backpack.
# Battery:AG13 LR44 357 SR |
|
|
|
w*******y 发帖数: 60932 | 27 Red Beam Laser Pointer, <5mW 3 in 1 Laser Pointer, Keychain & LED Flashli (
600-403-001)
Regular Price: $2.99
Final Price: $1.00
+ Free Shipping
Coupon Code: MLCK525RNL1
Link:
http://iway.org/9631501
|
|
m********a 发帖数: 12601 | 28 agree,最烦那个pointer在一个要点的地方晃来晃去眼睛都花了。
好的报告就像讲故事一样,PPT是辅助作用。比如你看TED的报告,有谁用个pointer在
屏幕上晃了 |
|
l*****g 发帖数: 685 | 29 reinterpret_cast和直接用hard cast都应该可以啊
(至于得到的 pointer address是不是有效,那是另一回事)
如果反过来,把pointer convert 成 int, 那得考虑是32位还是64位的系统。如果是64
位的,那convert to int 会有loss |
|
t******g 发帖数: 252 | 30 I don't understand how the second line in level one work. Can someone please
explain?
I think p is allocated as a pointer to pointer. But where it's pointed to is
not allocated. Why is that not a problem? |
|
s******n 发帖数: 3946 | 31 来说说我的思路,reverse list不难,但是这个问题是有环怎么办:比如a -> b -> c
->a,假设一开始从a开始reverse整个环,然后访问link list的next又遇到了b,这时
候就不能再reverse一次了。
思路:先reverse有环的,再reverse没环的
round 1,找出所有环,把环中最后一个指向special "NodeEnd",所以a -> b -> c ->
a变成 a->b->c->nodeEnd。
round 2,从LL头开始遍历所有Node,假设Node开始的random pointer路径包含nodeEnd
,则重新构成一个逆向的环。
round 3,从LL头开始遍历所有Node,假设Node开始的random pointer路径包含NULL,
则reverse这个list |
|
P********l 发帖数: 452 | 32 If we know the direction of the link, that is, pointing to a visited
node or a coming node, this problem is trivial.
To know the direction, we can follow the 'next' field of the nodes from
the node pointed by the 'random' pointer. If the current node was found,
it is pointing backward. Otherwise, forward.
You can use the 'random' pointer field to improve the performance.
some
simplify
more
than |
|
s********r 发帖数: 403 | 33 你直接看楼主的原贴,他是不是直接去 call 了非静态函数成员
void Function_pointer::update( int n, f pointer )
{
pointer(n);
}
所以大家都认为需要符合c++ 标准,把 next 定义成静态函数。
至于那个 trick, 就是绕过编译检查,把函数指针指向了一个 address,
并通过已经实例化的对象进行调用。
我已经说过编译器无法阻止这种 trick, 并不保证调用正确,要具体case具体看。
你刚才说的,只是把我前面讲的重复了一遍 |
|
t****t 发帖数: 6806 | 34 显然你对pointer to member function的认识还停留在C 语言pointer to function的
阶段. 你楼上讲的是C++定义好的语义, 没有任何问题, 保证调用正确, 有类型检查,
也没有什么trick. |
|
f****4 发帖数: 1359 | 35 typedef int (*sf)(int n);
sf fps = &myFunc.next;
fps(111);
error: ISO C++ forbids taking the address of a bound member function to form
a pointer to member function. Say ‘&Function_pointer::next’
error: cannot convert ‘int (Function_pointer::*)(int)’ to ‘int (*)(int)’
in initialization
typedef int (*sf)(int n);
sf fps = (sf)&myFunc.next;
fps(111);
error: ISO C++ forbids taking the address of a bound member function to form
a pointer to member function. Say ‘&Function_pointer::next’
warning: co... 阅读全帖 |
|
s***e 发帖数: 403 | 36 /**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
map corresponding;
corresponding[nullptr] = nullptr;
RandomListNode* nhead = new RandomListNode(0);
RandomListNode* last = ... 阅读全帖 |
|
t*******y 发帖数: 10477 | 37 【 以下文字转载自 GunsAndGears 讨论区 】
发信人: NightRunner (子夜狂奔), 信区: GunsAndGears
标 题: The 11 Pointer
发信站: BBS 未名空间站 (Sat Nov 6 21:27:44 2010, 美东)
终于进阶到11 Pointer club.
Today. Archery shot at 8 yards |
|
s*********r 发帖数: 4210 | 38 NB, what's 12 pointer, 11 pointer? |
|
r*****a 发帖数: 27155 | 39 我家Misty六个月的时候在dog park只输给一只pointer
pointer |
|
y**a 发帖数: 13 | 40 What do you mean "function pointer"? Java doesn't have any pointer stuff. |
|
g*****g 发帖数: 34805 | 41 There is no function pointer in Java, your asking for the # makes me
even confused, if you just want to have the functionality of function
pointer such as callbak, use interface. |
|
f******e 发帖数: 582 | 42 Hi,
I have a C program my.c and I would like to apply some basic ideas from the
following paper to analyze the pointers used in my.c.
My question is: is there a free open source tool available for me to start
with? the tool mentioned in the following paper seems obsolete..
Thanks a lot.
http://portal.acm.org/citation.cfm?id=1062455.1062520
Improving software security with a C pointer analysis
Full Text: Pdf
Authors: Dzintars Avots
Stanford University, Stanford, CA
Michael Dalton
Sta... 阅读全帖 |
|
K*****n 发帖数: 65 | 43 new int[5] 和 五次 new int 的 overhead 完全不一样。
你可以查看Family pointer - 16 到 Family pointer + 16所指的memory content。
所以
int * Family = new int[5];
for (int i=0; i<5; i++)
delete (Family+i);
是不可以的。 |
|
h*****n 发帖数: 38 | 44 为什么不用smart pointer,而要用pointer呢? |
|
q*****g 发帖数: 72 | 45 ya, good question
all java "pointers" are smart pointers
but that's one reason java is slower than C++ |
|
e******r 发帖数: 220 | 46
这个CALLBACK到底指的是call什么东西back?
每次就见pass 进去一个function pointer, 然后在操作中的某一步用到这个function pointer.
的f
多态
质上 |
|
j********r 发帖数: 21 | 47 Call-back is widely used for asynchornized interaction. It's implemented by
passing a function pointer to a call. when the called operation is exceuted,
at some point, the call-back function is called to do something for the
caller.
function pointer is like a variable for functions, and a variable for values. |
|
n**d 发帖数: 9764 | 48 I see. Thanks!
We use typedef to declare FT as a type of pointer to function and then use
FT to declare a[2]. If we don't use typedef, how could we declare elements
of a[2] as pointers to functions? |
|
F********E 发帖数: 1025 | 49 Hi,
I have the following fortran90 code:
**********************************************
TYPE :: CPLK
REAL(8), DIMENSION(:), POINTER :: CP
TYPE(CPLK),POINTER :: NEXT
END TYPE CPLK
...
allocate CPLK linked list
let CPTR point to the last link
...
IF ( ASSOCIATED (CPTR) ) THEN
WRITE(OUTU,*)' deallocate this layer'
DEALLOCATE(CPTR)
ENDIF
***********************************************
Execuation o |
|