由买买提看人间百态

topics

全部话题 - 话题: pointers
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
S********0
发帖数: 29
1
来自主题: JobHunting版 - Leetcode新题 Copy List with Random Pointer
同求问题,本地是过的,runtime error, 上代码。
public class Solution {
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
... 阅读全帖
S********0
发帖数: 29
2
话不多说直接上代码,在本地测了没发现问题。。
谢谢.....
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
RandomListNode n... 阅读全帖
p*****2
发帖数: 21240
3
来自主题: JobHunting版 - 面试题总结(2) - Two/Three pointers
面试题总结(2) - Two/Three pointers
简称two pointers吧。大概把分类粗略的搞了一遍(http://leetcode.cloudfoundry.com/), 发现利用two pointers解决的题目数量很大。two pointers我指的是一类题,而不一定是真正的two pointers, 比如可能是three pointers, 也可能不是pointer, 而是index。这类题基本上就是发生在array, string, linked list这三种数据结构上,是一种基本的算法和编程技巧,同样超高频率的出现,可以说是面试必遇的题。
two pointers常常和其他的算法混杂起来出现。比如binary search本身也可以归类为
two pointers的。如果这样算的话,Leetcode上边1/4的题目都跟它相关。因此,two
pointers是必须熟练掌握的基本编程技巧。
Two pointers大概分三种类型
1. 两个pointers从头往后走:感觉绝大多数的linked list的题目都涉及到这个操作,
当然还有array。
Imple... 阅读全帖
i**********e
发帖数: 1145
4
The pointer is always on the stack (it stores a 32-bit integer (the address
of the data it is pointing to)). The data it is pointing to can be on the
stack or the heap.
For example,
int *p = new int(3); // p is pointing to an int on the heap
int x = 3;
int *p = &x; // p is pointing to an int on the stack
The compiler knows that it's a pointer to pointer to Node. (you declare it
as Node **p). The pointer type is used for pointer arithmetic purpose. That'
s why a generic pointer (void *... 阅读全帖
z****e
发帖数: 2024
5
来自主题: Programming版 - c++ interview: iterator 和 pointer区别?
iterator is a smart pointer.
all thing related is about smart pointer vs naked pointer.
such like:
1.resource management
2.exception safety
3.reference counter
etc,
smart pointer has cp-ctor and op= overloaded.
but using smart pointer, usually you lose the usual pointer arithmetic.
to calculate smart pointer difference, you need to call distance(first, last).
nothing comes at no cost, smart pointer is usually more space consuming and time consuming than regular pointer. But being a coder, you ge
m*********2
发帖数: 701
6
LZ不如谈下这low-level detail.
For example:
1) How is Node** p represented in the low level?
p, the pointer to pointer to a Node, is on the stack?
where is *p located then? on the stack or the heap?
My understanding is:
Node p ==> the struct is on the stack.
Node *p ==> the pointer is on the stack, the struct is on heap
Node **P ==> the pointer of pointer is on the stack... not sure where
the other two is going to be.
In addition, does the complier keep track of RTTI of p (in case of Node
**p)? Does t... 阅读全帖
t****t
发帖数: 6806
7
strictly speaking, it's not equivalent. but in your case it's about the same.
as i said, char (*str)[] means "pointer to array of char", but that doesn't
matter as long as it is a pointer. let's say it's "pointer to T". then
forcefully convert string literal "ABC" (which has type "pointer to char")
to "pointer to T", then forcefully convert to back to "pointer to char". the
result will be the original pointer to char. it doesn't matter what is T,
wha matters is, str is a pointer to something.
t****t
发帖数: 6806
8
basically, if str is a pointer, then only thing allocated for str is space
for a pointer. so naturally str won't point to anything on stack, because
there's nothing (except the pointer itself) there. it has to point to the
string literal, doesn't it?
so that's why i said as long as it is a pointer. the type of pointer doesn't
matter, for C all kinds of pointers can be converted back and forth.
but i repeat, an array is never a pointer. if str is an array, it will be
wrong.

why
r***h
发帖数: 70
9
来自主题: JobHunting版 - 菜鸟问个C++的pointer问题
在看Programming interview exposed
不太明白里面讲link list插入新head的程序的例子.给出2个例子,书上说第一个是错的
,第二个才对.不明白为什么必须用**head而不是*head,难道head本身做为pointer pass
给函数不改变指针的内容吗? 请帮忙讲解一下,谢谢!
For example, the following code is incorrect because it fails to update the
head pointer in the calling function:
int BadInsert(element *head)
{
element *newElem;
newElem = (element *) malloc(sizeof(element));
if (!newElem) return 0;
newElem->next = head;
head = newElem;
return 1;
}
The correct way to update the head pointer in C is to pas... 阅读全帖
s******n
发帖数: 3946
10
Consider a Linked List with each Node, in addition to having a 'next'
pointer also has a 'random' pointer. The 'random' pointer points to some
random other Node on the linked list. It may also point to NULL. To simplify
things, no two 'random' pointers will point to the same node, but more than
1 Node's random pointer can point to NULL.
Now please reverse all the random pointers using space O(1)
w***g
发帖数: 5958
11
callback function一般使用function pointer实现的。C++里的functor就是包装了的f
unction pointer。function pointer除了用来callback,还可以用来干别的,比如多态
在不少语言里就是通过function pointer实现的,比如C++里的virtual function本质上
就是function pointer,C虽然不直接支持多态,但是用function pointer可以模拟。
X****r
发帖数: 3557
12
来自主题: Programming版 - c++ interview: iterator 和 pointer区别?
你这个是原则性的错误。iterator既可以是smart pointer,也可以是naked
pointer,甚至可以是不smart也不naked的pointer,因为iterator并不要求
管理资源。iterator和smart pointer根本就是两码事。iterator未必没有
pointer arithmetic,比如我刚才说的random access iterator就完全
具有所有的pointer arithmetic。
t****t
发帖数: 6806
13
来自主题: Programming版 - c++ pointers are iterators, why?
short answer: iterators are generalized pointers. so pointers, by definition
, are iterators.
long answer: there is no iterator "type" or pointer "type". iterator and
pointer are CONCEPTs, which are classes of types (that satisfy some
condition). pointer satisfy all the requirement of iterator so pointer is
iterator.
for implementation, anything suuport "iterator" (or some subclasses of
iterators) must be implemented with template (template is a set of
overloaded functions, so you are right, it ... 阅读全帖
t****t
发帖数: 6806
14
void (*A)(int): declare A as [pointer to function that takes int as
parameter, with no return]
void (*A(....))(int): declare A as a function that returns [pointer to
function that takes int as parameter, with no return], taking whatever
parameter (...)
replace "..." with "int signo, void (*func)(int)", you get what it means:
declare signal as a function, that returns [pointer to function that takes
int as parameter, with no return], and takes two parameter: first is int,
second is [pointer to fu... 阅读全帖
O*******d
发帖数: 20343
15
我是一般情况下,尽量用smart pointer。 在一些高强度的循环中,把raw pointer从
smart pointer中调出来,直接用。这样可以避免smart pointer的overhead。 我的原
则是,用smart pointer来管理memory,尽量不用delete。 我写的code中,几乎没有
delete。
S*A
发帖数: 7142
16
来自主题: Programming版 - C++ pointer to function is buggy
C++ 我不熟悉啊,C 我还略知道一点。
这个相关的规定在 C99 6.6.9 Constant Expression 章节里面。
也就是说,地址表达式可以 “&” 取地址是显式取地址,
然后也可以对数组和函数隐含取地址,不需要 “&”。
数组和函数的地址是特例,取地址可以隐含,因为数组和函数的
本来使用都用特殊语法后缀,数组要后面有 【】,函数后面有()。
所以没有后缀的用法就可以分别定义。这里退化为地址是最自然的。
注意看原文里的 “implicitly" use an expression of array or function type.
9.
An address constant is a null pointer, a pointer to an lvalue designating an
object of static
storage duration, or a pointer to a function designator; it shall be created
explicitly using
the unary & operator or an inte... 阅读全帖
s********k
发帖数: 6180
17
来自主题: JobHunting版 - One question about Void pointer (转载)
【 以下文字转载自 Programming 讨论区 】
发信人: silverhawk (silverhawk), 信区: Programming
标 题: One question about Void pointer
发信站: BBS 未名空间站 (Mon Mar 28 10:32:50 2011, 美东)
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 poin... 阅读全帖
t******g
发帖数: 252
18
Never mind. I get it.
p is a pointer to a pointer to Node. It was assigned pointer to head which
is itself a pointer to Node.
I think I was tired from a long day.

please
is
n*******t
发帖数: 6
19
for example:
我有两个function pointer, 分别指向两个不同function。
complie的时候,这两个function pointer就得到function地址了吗?
可以用两个function pointer之差来indicate 系统stack 是upward 还是downward吗?
FLAG的一个面试官说不可以,因为function pointer是compile time 得到地址的。
stack 是runtime形成的。
然后把我往网上上的一个很popular的解法上引导。
但是网上所有关于这个解法的讨论都说,是因为compiler 优化造成compile time 的地
址和stack 无关。 这样,不是把gcc 的优化去掉,或者标成volatile不就可以了吗?
再问一个base class 和 相应 derived class的memory layout 能说明stack grow
direction 吗?
面试官也说不行。说,就是扯上virtual inheritance, 也是compile stage的。
s********k
发帖数: 6180
20
【 以下文字转载自 Programming 讨论区 】
发信人: silverhawk (silverhawk), 信区: Programming
标 题: One question about Void pointer
发信站: BBS 未名空间站 (Mon Mar 28 10:32:50 2011, 美东)
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 poin... 阅读全帖
z****e
发帖数: 2024
21
来自主题: Programming版 - c++ interview: iterator 和 pointer区别?
我没说iterator 绝对没有pointer arithmetic啊,要就自己overload。但是比如STL,
除了random access类型的,其他运算符都不全,所以我说"usually"也没说错啊。
iterator 不要求管理资源不代表不能管理资源啊。所以我也没说错啊。iterator本身
就是一种design pattern啊,你想让它作甚,就作甚啊。
iterator是smart pointer,我也没说错啊。关于什么是精确的smart pointer,不是现在还有标准的争论么? 我这里大致的用一下,就原则性错误了?
如果说iterator是naked pointer,没错,但是这个题目就没什么可答的了。
我还是觉得,我可能语言没有整理好,就是条理性差些,侧重点答的不好,
但是没有原则性错误。
以上你逐条批判的话,我也都防住了。
t****t
发帖数: 6806
22
来自主题: Programming版 - c++ interview: iterator 和 pointer区别?
广义上讲,说smart pointer就是pointer with extra features也不能算错。但是这个
说法并不是被广泛接受的。如果你要用,一定要说明你说的smart pointer是什么意思。
在C++里,如果不加说明,smart pointer就是指我刚才说的意思,也就是不需要手工de
lete。
c**********e
发帖数: 2007
23
来自主题: Programming版 - C++ Q05: pointer to constant variable
Which one of the following statements about const variables is true?
a) The address of a const variable can only be assigned to a float variable.
b) A pointer to a non-const cannot be assigned the address of a const
variable.
c) A pointer to a const can be assigned to multiple objects at compile time.
d) Pointers to const objects must be initialized to NULL before they can be
referenced.
e) A pointer to a non-const cannot be assigned the address of a volatile
variable.
j***i
发帖数: 1278
24
来自主题: Programming版 - C++ Q05: pointer to constant variable
你哪里找的这么多题呀

Which one of the following statements about const variables is true?
a) The address of a const variable can only be assigned to a float variable.
b) A pointer to a non-const cannot be assigned the address of a const
variable.
c) A pointer to a const can be assigned to multiple objects at compile time.
d) Pointers to const objects must be initialized to NULL before they can be
referenced.
e) A pointer to a non-const cannot be assigned the address of a volatile
variable.
a****o
发帖数: 686
25
来自主题: Programming版 - C++ Q70: this pointer (skillport) (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q70: this pointer (skillport)
发信站: BBS 未名空间站 (Mon Oct 11 20:02:47 2010, 美东)
Which one of the following operations requires explicit use of a this
pointer?
a) Calling a member function that requires a pointer to that object from
another member function in the same object?
b) Calling a global function that requires a pointer to that object from
a member function?
d****j
发帖数: 293
26
来自主题: Programming版 - C++ Function Pointer Array 的问题
Essential C++ 第二章介绍了如何使用function pointer,我试了了一下,想获取一个
定义好的function pointer array的长度,却怎么也搞不定,请指教。
具体的问题是,有5中sequence,如fibonacci序列,squared序列,等等,假设有下列5
个对应的function,输入参数为 int size,返回一个size长度的const vector;
const vector* fibon_seq(int);
const vector* lucus_seq(int);
const vector* pell_seq(int);
const vector* triang_seq(int);
const vector* square_seq(int);
再有一个从一个vector中取出第n个元素的函数,参数为pos,要取的数字的位置,elem,
引用,存储返回值,fp,第三个是function pointer,具体如下:
bool seq_elem(int pos, int& elem, co... 阅读全帖
v******l
发帖数: 512
27
来自主题: Programming版 - c++ pointer conversion question
First, I suppose your last statement should be:
printf("%d\n", a[0]);
OR
printf("%d\n", *a);
because otherwise you're printing a pointer as an integer, which does not
make much sense here.
With that change, I doubt this sample will ever print 200 at all because you
clearly give the compiler that logic you want (unlike that last sample you
deliberately hide the fact from the compiler that the two pointers are to
the same address). I'm pretty sure it will print 1065353216 on any 32/64-bit
small-en... 阅读全帖
g****u
发帖数: 252
28
来自主题: Programming版 - C++的smart pointer注定是个二流的东西
如果所有的指针都用smart pointer包装, 只能说这个语言设计得失败. 其实C++的王道
是RAII. 只要坚持RAII, 所有的资源都在同一对花括号内获取和释放,指针根本不会用
错, 用不着什么smart pointer. 在加上smart pointer名目和种类繁多,都快赶上茴字
的四种写法了, 连我这个写了10年C/C++的都不怎么搞得清(smart pointer这东西至少
10年前就应该有了), 一旦普及起来必将bug百出引入无穷后患.
N******K
发帖数: 10202
29
把所有raw pointer替换为 shared_ptr or weak_ptr 等等 是sb行为
函数的传入传出指针都用raw pointer就行 只要保证这个函数内部没有内存泄露就可
以 函数内部可以用shared_ptr 或者 unique_ptr
例外就是 类的成员变量 如果是其他类的指针 就得用shared_ptr 或者weak_ptr 当用
函数初始化这些成员变量时,函数输入要是 smart pointer
C++ 自动内存管理 靠的就是 函数返回时 清空stack 或者 {}结束 清空局部变量
模仿GC来用smart pointer是行为艺术
w*******y
发帖数: 60932
30
Link:
http://www.meritline.com/led-laser-pointer-key-chain-torch-974-
2-LED with Laser Pointer Key Chain Black Torch Flashlight, Random Color
Features:
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:... 阅读全帖
k***e
发帖数: 556
31
今天想实现前两天大家讨论的minmaxheap找median,结果碰到一个问题。
I want pass function pointer to create a heap that has comparison function
as I defined. Thus I don't need both minHeap maxHeap. By the way, because I
have heap with template parameters, I also need function pointers with
template parameter.
It looks like:
template
class Heap {
private:
T* data;
public:
Heap(const vector& dataSrc, bool (*cmp)(T, T);
};
However, I cannot get a function pointer with template parameter. I really
had no idea h
c**********e
发帖数: 2007
32
来自主题: JobHunting版 - C++ Q70: this pointer (skillport)
Which one of the following operations requires explicit use of a this
pointer?
a) Calling a member function that requires a pointer to that object from
another member function in the same object?
b) Calling a global function that requires a pointer to that object from
a member function?
c********1
发帖数: 161
33
来自主题: JobHunting版 - One question about Void pointer (转载)
For me, I prefer the second one if your idea is to create a kind of linked
list whose "next" pointer points to the next element of the same data
structure. And this should be safe.
For the void pointer, it just means a pointer pointing to a void type. So
you may have to define how large you have to allocate memory for "next" to
point to and you may need to do some necessary type casting.
So, it really depends on your program.
i**********e
发帖数: 1145
34
Node *head = new Node(3);
Node **p = &head;
*p = new Node(4);
p = &((*p)->next);
*p = new Node(5);
Attached is an image to visualize the pointers for level three.
Remember, a pointer always occupy 4 bytes of memory no matter what type it
is. It stores nothing but the address of the data it is pointing to (which
is a 32 bit integer). The data type is only used to do pointer arithmetic (
ie, to calculate the number of bytes to jump over to the next element in an
array).
一些常见面试题的答案与总结 -
http://www.... 阅读全帖
l*****a
发帖数: 14598
35
obviously, base class pointer is not a derive class pointer
here u need type conversion
B* b=dynamic_casta;
s******n
发帖数: 3946
36
every object will have a pointer to the vtable for each virtual class.
So if an object inherit two virtual classes, it will have two pointers to
the two virutal classes, if this class added new virtual functions, then
this class itself will have a virtual table, so it will totally have three
pointers to vtable.
r****t
发帖数: 10904
37
【 以下文字转载自 Programming 讨论区 】
发信人: repast (xebec), 信区: Programming
标 题: c++ 里面 this pointer 是完全 un necessary 的吗?
发信站: BBS 未名空间站 (Fri Dec 30 13:20:47 2011, 美东)
以前一直不自觉的用,如果所有 unqualified-id 都默认是通过 this pointer, 是不
是 this pointer 是完全不必要的?
f********1
发帖数: 228
38
I am not familiar with unique pointer. It seems that means you can only
modify the memory location with one pointer. I don't know how to apply it
here. Could you elaborate?
Also, will function pointer be helpful in anyway?
c**z
发帖数: 669
39
请大家看看为什么编译通不过,谢谢. 始终说最后的函数地址不match定义 。
牛人看看。
//header file
class Function_pointer
{
public:
int next(int n);
int before( int n);
int next_after_next( int n);
typedef int (*f)(int n);
void update( int n, f pointer );
};
// .cpp file
# include
# include"Header.h"
using namespace std;
int Function_pointer::next(int n)
{
return n +1;
}
int Function_pointer::before( int n)
{
return n - 1;
}
int Function_pointer::next_after_next( int n)
{
return n + 1 + 1;
}
void Function_pointer::up... 阅读全帖
f********4
发帖数: 988
40
来自主题: JobHunting版 - Leetcode新题 Copy List with Random Pointer
第二个loop就是改random pointer的,不能把链断开。因为后面的random pointer有可
能会指向前面的node,或者它本身,这样,你把前面的指针断开了,就找不到复制的那
个node了。。。所以第二个loop就改random pointer,再多加一个loop专门改链的指针
m********c
发帖数: 105
41
来自主题: JobHunting版 - c/c++ double pointer研究
昨天发了一个pure storage的题目,发现用double pointer非常容易做。今天认认真真
研究了一下double pointer,感觉虽然不容易掌握,但是掌握了它就很强大。
做leetcode题目Remove Duplicates from Sorted List II时,正好就用上了,下面是
我用double pointer的实现。
ListNode *deleteDuplicates(ListNode *head) {

ListNode **lpp = &head;
int currValue = INT_MAX;
while (*lpp) {
if ((*lpp)->val == currValue) {
*lpp = (*lpp)->next;
}
else if ((*lpp)->next && (*lpp)->val == (*lpp)->next->val) {
currValue = (*lpp)->val;
... 阅读全帖
e*******s
发帖数: 1979
42
Copy List with Random Pointer
A linked list is given such that each node contains an additional random
pointer which could point to any node in the list or null.
Return a deep copy of the list.
下面注释里面是别人写的能跑过的 上面的是我的 到现在都改得几乎一模一样了
可是还是超时
/**
* 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:
RandomList... 阅读全帖
r***c
发帖数: 95
43
来自主题: JobHunting版 - 来问一个关于smart pointer的弱问题
大家都是大牛,菜鸟来问一个弱问题。
Smart pointer implementation里, dereferencing operator 和 member selection
operators是这样定义的。
T& operator* () const // dereferencing operator
{
return *(m_pRawPointer);
}
T* operator->() const // member selection operator
{
return m_pRowPointer;
}
想知道,因为什么,前者returned by reference, 后者 returned by pointer呢?能
不能前者return-by-pointer, 后者return-by-reference呢?
n*******7
发帖数: 181
44
基本概念不清啊。function pointer是runtime 变量,可以赋函数地址的值。函数地址
是compile time决定。stack方向应该看stack pointer,和function无关。

:for example:
:我有两个function pointer, 分别指向两个不同function。
t****t
发帖数: 6806
45
来自主题: Programming版 - Why do I need to use "plain" pointer?
i guess c++ tries to imitate c while adding OO. pointer is inherited from c
and is closer to hardware behaviour (than smart pointers).
well there's trade off everywhere. c++ is just result of some trade off
between execution efficiency and developping efficiency. some language make
some job easy, while making some other job (nearly) impossible. some
language make all jobs equally difficult (or equally easy, depending on the
point of view).
smart pointer makes you safer since you don't have maint
s****u
发帖数: 118
46
其实你send一个pointer有什么用呢?
pointer就是一个整数,直接send就好了
但是你这个pointer到了另一个电脑上基本就是非法的哦

send
b***y
发帖数: 2799
47
来自主题: Programming版 - [合集] pointer in C
☆─────────────────────────────────────☆
yunhai (飞纵千里山) 于 (Thu Aug 25 13:42:40 2005) 提到:
int (*p)[2]
int a[2]
p: a pointer which point to an int array which has size of 2
// Is this crrect?
usage:
int (*p)[2];
int a[2] = {3,4};
p = a; // error
p = &a; // right
who can explain them?
☆─────────────────────────────────────☆
thrust (Thrust Jaeina) 于 (Thu Aug 25 13:46:15 2005) 提到:
anything special? to compare with simple pointers, look at this
int *p, a; /* p is pointer to int */
p=&a; /* c
b***y
发帖数: 2799
48
来自主题: Programming版 - deque的pointer和reference是怎么回事?
那这个pointer是怎么定义的? 我一直以为iterator就是用pointer实现的. 用pointer来
访问container的东西, 具体怎么操作?
S**I
发帖数: 15689
49
来自主题: Programming版 - deque的pointer和reference是怎么回事?
我记得只有vector和string的iterator是用pointer实现的;用pointer访问container
应该是这样子:T* = &(*iter)

pointer来
m*****r
发帖数: 130
50
来自主题: Programming版 - deque的pointer和reference是怎么回事?
这个iterator也可以是pointer实现的。简单的deque实现就是一个array。如果用
pointer直接取地址就好了。

pointer来
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)