s*y 发帖数: 472 | 1 Usually when you pass a pointer, you would expect the content pointed to
by the pointer will be modified.
In your case, the pointer itself got changed, which doesn't make a
difference. Either do a return assignment instead of passing in as
argument or pass in a pointer to the pointer.
therefore
void test( char *p )
{
p = new char[10];
memset(p, 'A', 10);
}
int main()
{
char *k;
test(k);
cout << k << endl;
return 0;
} |
|
g***s 发帖数: 3811 | 2 public class ColorBalls {
public static void arrange(int[] balls, int colorNum){
int[] count = new int[colorNum];
int[] pointers = new int[colorNum];
//count color
for (int color : balls) count[color]++;
//set the pointers
for (int i=0;i
pointers[i+1] += pointers[i] + count[i];
}
int currentColor = 0;
int c = count[currentColor];
for (int pos=0;pos
if (... 阅读全帖 |
|
k***t 发帖数: 276 | 3 写个Least Recently Used Cache的Class好像是几家公司常见题。
有没有面试可用的精简一些的Sample Code?
或哪个大拿给写一个范本。谢了。
还有,copy-on-write的string class。
我这里贡献一个smart pointer class。Code来自http://www.codeproject.com/KB/cpp/SmartPointers.aspx 。
class RC
{
private:
int count; // Reference count
public:
void AddRef()
{
// Increment the reference count
count++;
}
int Release()
{
// Decrement the reference count and
// return the reference count.
return --count;
... 阅读全帖 |
|
S**I 发帖数: 15689 | 4 ☆─────────────────────────────────────☆
yuhanlin (Yuhan) 于 (Mon Aug 29 00:18:17 2011, 美东) 提到:
周五面完最后一个onsite, 累的惨兮兮的, 好容易爬回家. 不管结果如何, 这段时间找
工作算是告一段落了.
下面把这段时间面试中被问到的题目整理一下, 供大家参考. 我也就不说具体是那些公
司了, 都是很典型的面试题, 到哪里都有可能会被问到.
* implement memcpy? How to improve? how to determine if a system is
32bit or 64bit?
* how is static keyword used in Java?
* a list of intervals, no overlapping and sorted, write a function to
insert an interval into the list and still keep the list sorted and no
overlapping.... 阅读全帖 |
|
e******o 发帖数: 757 | 5 刚才面了一个OR的position一个三哥面的,挂了
三个一上来就问你怎么没发paper,我说我发了,没写到简历上去
问了一堆linear programming的问题。Danzig decompositon, bender's Decompositon
.可惜都是我第一年的时候学得,早还给老师了。然后问了一堆branch and bound的问
题。
然后三哥看我做过parallel computing, 结果问了一堆multi-thread的问题。原来在三
哥心目中 parallel computing == multi-thread programming. 三哥连super-
computing里的communication问题 都不知到。结果他问他的,我答我的,结果就悲剧
了。
然后三哥问pass by pointer 和 pass by reference 的区别。我就说pass by
reference就是在functions 中assign the original object another name. pass by
pointer就是construct anot... 阅读全帖 |
|
s***0 发帖数: 117 | 6 To make it clearer for C/C++ people.
Node node = new Node();
new Node() creates an object on the heap, and returns the pointer to the
object.
node is really a pointer. When we pass node, the function makes a copy of
the pointer. The rest follows the behaviour of pointers, except node.data is
actually node->data in C/C++.
Plus you don't have worry about garbage collection, so it's more like a
smart pointer.
data |
|
s**x 发帖数: 7506 | 7
New head is just a pointer, when declare a pointer variable, only the memory
for the pointer is allocated, what the pointer points to is not allocated.
And in your case, this pointer value is not initialized , so it actually
points to a random object, so it is really bad. |
|
m********6 发帖数: 58 | 8 LZ在GOOGLE面试过两次, 第一次是大学毕业。 为了追CHICAGO的一个女孩子, 申请了
GOOGLE CHICAGO的位子。第一轮CAMPUS面试通过, 第二轮NEW YORK后, 收到CHICAGO
的EMAIL问我availability说要为我订机票第三轮面试, 三天后却收到NEW YORK的电话
说我申请的位子取消了, 我没被录用。后来我在NEW YORK找了一份IT的工作。 三年后
, 我决定跳槽其他的IT工作, GOOGLE刚巧邀请我去面试, 所以我第二次去GOOGLE面
试。 这次拿到了offer, 但是也拿到了更好的HFT的offer。虽然没去GOOGLE工作, 但
是我很喜欢GOOGLE的面试, 觉得每次都有收获。在此分享一下我被问过的问题。我也
很希望看到其他朋友们的面试问题, 我会当兴趣爱好来试解答。
Behavior Questions:
1. In java, a method declared as private restrict access to within the class
. For example, a private void do... 阅读全帖 |
|
m*****k 发帖数: 731 | 9 7. Next tree sibling. Given a tree where each node has left and right
pointers
, implement a function that sets the next pointer. Next pointer will point
to a node in the same level immediately to the right. For example, if a node
has both left and right children, next pointer of the left child will point
to the right child. The next pointer of the right child will point to
parent's sibling's left child. The fact that left child and right child can
both be null make things complicated.
BFS using... 阅读全帖 |
|
a****l 发帖数: 8211 | 10 depending on perspective, I would say the huge difference is in only in the
implementation , not in the concept. My understanding is just that pointers
are much safer to use in Java than in C++, but they are still pointers.
BTW, I think calling "pointers" as "references" does not make them anything
less than a pointer. I would stick to the C++ definition that "reference"
is only the pointer pointing to a real object. I just feel that "
nullreference" thing is a badly guided effort of Java/Sun d |
|
O******e 发帖数: 734 | 11 // concatenates string A and string B.
=> is a Fortran pointer assignment. Fortran pointers are not the same as
pointer in C/C++, so
integer,pointer::A
integer,target::B
A = >B
is not exactly equivalent to
int *A, B;
A = &B;
and
integer,dimension(:),pointer::A
integer,dimension(1:N),target::B
A => B
is not the same as
int *A, B[N];
A = B;
If you want to know about the subtle differences, read Metcalf et al.,
Fortran 95/2003 Explained. |
|
k**f 发帖数: 372 | 12
Usually const is used along with pointer or reference in the return type.
The returned value can only be assigned to a const pointer or bonded to a
const reference. If the underlying type is a class (struct), only the const
method can be called using such const pointers and references.
Otherwise, if you return a non-const pointer or reference, you can change
the object through the pointer or reference. |
|
O******e 发帖数: 734 | 13 The meaning of the terminology "pointer" is different
in Fortran 9x and C.
In C, a pointer refers directly to an address.
In F9x, a pointer is actually a complicated data structure.
This is the essence of your problem--misuse of the F9x "pointer".
Before I get into all the details, how well do you understand the
following F9x concepts:
1. array subsection
2. copy-in and copy-out of actual array arguments
3. argument passing by reference in F9x, or passing by pointer in C |
|
t****t 发帖数: 6806 | 14 教你一个偷懒的办法: 找个unix, 运行cdecl
cdecl> explain char **arr[12][12][12]
declare arr as array 12 of array 12 of array 12 of pointer to pointer to
char
cdecl> explain char ** (* p) [12][12]
declare p as pointer to array 12 of array 12 of pointer to pointer to char |
|
X****r 发帖数: 3557 | 15 Also, peeking at pointer-to-member is not a good way to
reveal memory layout of an object. For example, whether or not
the internal value of pointer-to-member addresses are bumped by
one are compiler-dependent -- this is used to allow a null pointer
to be converted to a 'null' pointer-to-member. But there other
ways to designate a 'null' pointer-to-member, e.g. ~0, in
which case bumping by 1 becomes unnecessary.
A simpler yet more reliable way to look at memory layout of
an object is to directly |
|
z****e 发帖数: 2024 | 16 smart pointer 不是 绝对不是 永远不是 pointer!
smart pointer 就一user defined class 对象而已。
pointer 里边只有机器地址,没有dtor。
而smart pointer 有dtor。
析构 |
|
d****p 发帖数: 685 | 17 reference is
1. a pointer
2. a pointer to a valid object
3. a pointer which is always associated with the valid object it points to
When you try to reseat a reference, you are breaking rule 3. If you want sth
that could be reseatable, use a
pointer.
Syntactically a reference is "reseated" since you are actually assigning the
new object to the object the
reference was originally bound to. In your case, ref = v2 is actually v1 =
v2
reference is a concept manipulated by compiler. Its physical prese... 阅读全帖 |
|
d****n 发帖数: 1637 | 18 Void pointer can be flexibly converted into vary types with the cost of type
cast.
Also, the data structure size is not changing, but a two pointers: one is
the necessary next pointer, the other is datum.
here is the what I said "cost" from one of my favorite person's blog.
"We usualy use void* to implement generic containers in C. To do in this way
, we need to pay an overhead on retrieving data pointed by a void* pointer.
We often, but not always, need a function call for operations on void* d... 阅读全帖 |
|
v******y 发帖数: 84 | 19 这是我做的10题,大家试试
以后公布答案
Which of the following is the most portable way to declare a C preprocessor
constant for the number of seconds in a (non-leap) calendar year?
Response:
#define SECONDS_PER_YEAR 60 * 60 * 24 * 365
#define SECONDS_PER_YEAR 60 * 60 * 24 * 365;
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365UL)
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)
Score 1 of 1
Question:
Which of the following is the most flexible way to declare a C preprocessor
macro that takes two arguments and returns th... 阅读全帖 |
|
w******w 发帖数: 126 | 20 C++ 的标准里面的auto pointer 被多少人诟病啊。
还好现在新标准出来了 那这个auto pointer 真心被弃之如敝履了。
完全用 shared pointer 替代了。就算不用新标准,也会选择boost的 smart pointer。
用这个auto pointer 太容易产生隐含的bug了。 |
|
M*****R 发帖数: 650 | 21 I think copy-by-value is C++'s original sin.
Once you have copy-by-value, you need copy constructor.
but you cannot write a copy constructor without copy by reference.
So you have both, but then what about pointers?
It cannot manage the memory itself because it's not type-aware.
So you need RAAI, aka. smart pointers.
Now what happens when you copy a collection object,
oh, right, you need to know whether to do a shallow copy, or deep copy.
So how do I mandate shallow copy, you need move construct... 阅读全帖 |
|
T*T 发帖数: 421 | 22 ☆─────────────────────────────────────☆
happyangel (快乐的天使) 于 (Fri Dec 8 15:25:42 2006) 提到:
1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)
2 . 写一个"标准"宏MIN ,这个宏输入两个参数并返回较小的一个。
3. 预处理器标识#error的目的是什么?
4. 嵌入式系统中经常要用到无限循环,你怎么样用C编写死循环呢?
5. 用变量a给出下面的定义 a) 一个整型数(An integer) b)一个指向整型数的指针
( A pointer to an integer) c)一个指向指针的的指针,它指向的指针是指向一个
整型数( A pointer to a pointer to an intege)r d)一个有10个整型数的数组(
An array of 10 integers) e) 一个有10个指针的数组,该指针是指向一个整型数的
。(An array of 10 pointers to integers) f) 一个指向有10个整型数 |
|
a******h 发帖数: 19 | 23 In Java, you can only use local variable instead of pointer.
One variable for low pointer, one for mid pointer, and one for high
pointer. |
|
s****t 发帖数: 36 | 24 那个copy random pointer linklist的, 大家看看对不对啊?O(3N)~~ O(N)吧
/*
A contain data, next pointer, and random pointer, copy the A structure in
B
*/
struct node{
int data;
struct node* next;
struct node* random;
node(int value, node* n, node* r){
data = value;
next = n;
random = r;
}
}*llist;
void copyrandompointerlist(llist src, llist& dst){
// assume the first node doesn't use.
// src is the head pointer of A,
assert(src != NULL);
llist src_index, dst_index, temp;
src_in |
|
b********w 发帖数: 110 | 25 My solution is use one hashtable entry to maintain a linked list of pointers
. The pointers are pointing to data elements in the hashtable and arranged
by the order the elements are inserted.
So once a element needs to be removed, use the first pointer (head) to
find it and update the hashtable. And remove the head of the linked list.
The pointer of the newly inserted elements can be simply appended to that
linked list.
Above operations take O(1). |
|
m*****g 发帖数: 226 | 26 stl container use iterators instead of pointers.
however, what if i want to use a pointer to pointers, can i do it with
iterators? how?
for example, with normal pointers, i can do
int *A, *B;
int **P;
if(...) *P = A;
else *P=B;
how to do this with iterators?
thx |
|
a**1 发帖数: 213 | 27 That will be too complicated compared to other questions.
What I am thinking is there is a collection of aliasIDs already there. Then
generate an array of pointers pointing to those aliasIDs. But there is still
something wrong here.
is:
aliasID
assigned to each pointer in the array(we get the address of these pointers
through void * parameter).
GetNextAlias will return the next available pointer or NULL. |
|
s*****n 发帖数: 994 | 28 round 1/A:
void printTree (node * root){
if (!root) return;
node* pointer;
stack*> astack;
astack.push(root);
while (!astack.empty()){
pointer=astack.top();
if (!pointer) continue;
print(pointer);
astack.pop();
astack.push(point->left());
astack.push(point->right());
}
}
level |
|
l******l 发帖数: 66 | 29 Here is a thought:
With the sorted index-value array x as above:
1. Left pointer start from x[0], using binary search (or linear search,
won't affect
overall big-O complexity ) find the first j (right pointer) such that
x[j].num-x[0].num>k;
then from j to the end, find the largest index, let it be m.
2. Now move left pointer to x[1], move right pointer from j to the left,
until we find the first j'
such that x[j'].num-x[1].num>k. Compare new indexes with m, update the new
m
. Also update the lar... 阅读全帖 |
|
g**e 发帖数: 6127 | 30 /**
Maintain two pointers, head and tail,
1. start sweeping the array from head if a[head]
otherwise
2. for each a[i], if current value is less or equal to a[head] (or a[tail]),
skip
3. else compute area with a[i] & a[tail] (or a[head]), set start=i (or stop=
i), update max volume if necessary
4. until head meets tail, return max volume
All items are scanned at most once, total time O(n).
*/
public static int maxVolume(int[] a) {
if (a.length<=1)
return ... 阅读全帖 |
|
j***y 发帖数: 2074 | 31
如果能写成这样,那么就可以写成int fun(int **p)了。
记得int main(int argc, char *argv[])也可以写成int main(int argc, char **argv)
Chevy提供的是正确的用法。
相似的例子在K&R的书上也有:
If a two-dimensional array is to be passed to a function, the parameter
declaration in the function must include the number of columns; the number
of rows is irrelevant, since what is passed is, as before, a pointer to an
array of rows, where each row is an array of 13 ints. In this particular
case, it is a pointer to objects that are arrays of 13 ints. Thus i... 阅读全帖 |
|
P**********c 发帖数: 3417 | 32 programming perls里13章第4题的答案里说可以这样
void insert (t) {
for(p=&head; (*p)->val < t; p= & ((*p)->next))
;
if((*p)->val==t)
return;
*p = new node (t, *p);
n++;
}
书里的意思似乎是说因为用了pointer to pointer, 就不需要update老的parent的next
了。为什么会这样呢?没想明白。即使pointer to pointer,原来那个位置的parent的
next必须指向新的node, 也就是新的*p啊。虽然p没变,但是*p变了还是不行的吧。后
面第7题又用了类似的方法。是不是我有一个memory的基本东西没转过弯来呢。 |
|
G******i 发帖数: 5226 | 33 ☆─────────────────────────────────────☆
DrunkMonkey (努力改造,早日释放) 于 (Sun Mar 13 19:20:08 2011, 美东) 提到:
首先解释一下自己的背景和找工作的目标。本人烂校CS Ph.D.毕业,Ph.D.期间无工作经验,无Intern经验。目标是在local找一份工作,不求A,M,G,不求高薪,只要能给offer给办H1B就行。
先说说找工作的过程吧:2010年11月开始断断续续的看CRLS。虽然以前上过数据结构和
算法课,不过都快忘光了,基本上是从头学一遍。2011年1月底投出第一份简历。上周
拿到一个offer。还有一个onsite结束后面试官说我是目前的candidate里最好的,很可
能会给offer,但是要等到下周中才有结果。
第一个公司催的紧,而且地理位置很理想,也是我想要的职位,打算从了。花了一个半
月找工作,终于可以划下句号了。
以菜鸟的状态进入本版,这期间得到了版上的很多帮助和
BLESS。谢谢!
找工作的过程中有喜有忧,也有一点小小的心得,和大家分享。希望能对仍在找工作的
同学有点... 阅读全帖 |
|
c**********e 发帖数: 2007 | 34 C++ Q90: class templates
What type of class template can be nested?
A. Only non-template classes
B. Only class templates
C. Both class templates and non-template classes
D. Neither class templates nor non-template classes
C++ Q91: this pointer
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 ... 阅读全帖 |
|
c**********e 发帖数: 2007 | 35 The given answer is B.
C++ Q91: this pointer
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 |
|
H****s 发帖数: 247 | 36 如果你没有大项目的经验,你是根本无法体会到design pattern的美感的,想出这些
pattern的人肯定是有相当多年丰富的project经验。 如果你整天就写个几百行顶大几
千行的程序那你可以不follow那些pattern, 但想象一下你要写个3万行的程序而且要
维护多年, 在设计阶段当中你就会体会到这些pattern简直就令人拍案叫绝啊, 原来
前辈们对这些设计问题早就有了一个系统的解决方案了。
具体问题楼上已经回答的很好了!
不过 in C++, smart pointer is an overloaded dereference operator (*, ->).
Iterator is a kind of mart pointer and it used the concept of smart pointer
but iterator is not only a smart pointer. |
|
a******u 发帖数: 239 | 37 I think my answers to Question 7 and 8 are correct, but they said they are
not correct, why?
I already tested the code by Visual C++ (c compiler).
Thank you very much.
Line in file Contents
30 int * someIDs, theFirst, *r;
110 someIDs =GetSomeIDs(); /* defined below */
111 theFirst = someIDs [0];
112 r= ReorderIDs(someIDs);
113-150 /* we want to use ‘theFirst’ and ‘r’ here*/
499 /*-------- GetSomeIDs-----*/
500 int * GetSomeIDs()
501 {
502 int ids[8];
503-5... 阅读全帖 |
|
p*i 发帖数: 411 | 38
array of pointers
pointer to array
function that returns a pointer to integer
function pointer |
|
c**********e 发帖数: 2007 | 39 My solution: Suppose the window width is L.
Use two heaps, one min-heap and one max-heap, and an array Node* arr[L],
which are pointers to the nodes.
Each node of the heap is a structure. It not only includes the data, a left
pointer, and a right pointer, but also an integer. The integer is the
location of the node in the array.
When we delete an value from the window, we know its location i (0 <= i < L)
. By arr[i], we find the node in either heap, and we delete it. When we
delete the node, s... 阅读全帖 |
|
s*********5 发帖数: 514 | 40 这个解法漏掉了一个重要的点,就是要记住beginning node, 来查当前的link是否是
circle还是lollipop. 如果只是简单的circle, 就give up. 而且其中遍历过的数字
,要变成负数,这样不用再把这个数当成linked list的head再查一遍。
比如, 2, 1, 4, 3, 6, 5, 8, 7, 8
会有3个circle, 1个lollipop
2<=>1
4<=>3
6<=>5
8<=>7<-8
Phase I needs to find the lollipop entrance
Proof: Let s and c denote the lengths of the stick and the cycle portions of
the lollipop. If the slow pointer in Phase I ends in s + x rounds, then 2(s
+x) = s+x + mc, where m is a positive integer. In other words, s+x = mc. So
in ... 阅读全帖 |
|
w*****d 发帖数: 105 | 41 1,what is abi and api, give some example
api: usually means interfaces of a library or class.
abi: this is means the binary conversion rules for different compilers or
platforms, such as arguments order in stack for a function call.
2, what happens when exception is thrown?
in C++, the following operations are carried out:
1)unwind the current stack, call any dtor for local vars if necessary, until
there is a catch block that can handle the exception obj;
2)if there is no such catch block in the... 阅读全帖 |
|
y***n 发帖数: 1594 | 42 Node *curr: pointer to a node.
Node *&curr: reference to pointer to a node.
Node ** curr : pointer to pointer to a node.
每个都写一下,就会加深理解。。
还有这些问题去StackOverflow, 很多牛人会来回答,还可以得分 |
|
m******0 发帖数: 222 | 43 at least的话,也不对,道理是一样的,随着right pointer移动,distinct字母数既
可能增加,也可能减少。传统two pointer方法失去了basis。
我的想法是:
设置两个pointers,p1
1. p1=0, p2 scan from 1 to len
2. p1=1, p2 scan from len to 2
以上两步,就能得到所有[0,i], [1,i] (1<=i<=len)范围内的distinct char的个数(
pointer每移动一次,可以O(1)时间得到distinct char的个数)
然后再令p1=2, 重复上面步骤,又得到[2,i], [3,i]的distinct char个数
依次下去,O(n^2)时间可以搜索到所有情况。 |
|
w******t 发帖数: 16937 | 44 分特,想看专业的?
看这个。声明:因为网络安全原因,我删去了一些必须删去的内容。
http://schema.org/WebPage">Google
|
|