由买买提看人间百态

topics

全部话题 - 话题: void
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
M**A
发帖数: 78
1
来自主题: JobHunting版 - CarerCup 书里面的关于memory的一道题
总结各位评论,欢迎拍砖。
题目
align_malloc(1000, 128) will return a memory address that is a multiple of
128 and that points to memory of size 1000 bytes.
aligned_free() will free memory allocated by align_malloc.
解答
int offset = alignment-1+sizeof(void*);//分配空间给a memory address that is
a multiple of 128
这个offset的空间用来存放memory address,就是*p1 that points to memory of
size 1000 bytes
p1 = (void*)malloc(required_bytes+offset)) //分配空间给memory of size 1000
bytes+memory address that is a multiple of
128, required_byt... 阅读全帖
d****n
发帖数: 233
2
来自主题: JobHunting版 - 反interleave该怎么做?
here is a O(nlogn) solution, Rotate can be further optimized.
public class InterleaveShuffle {
public static void main(String argv[]) {
int [] arr = {1,2,3,4,5,100,200,300,400,500};
InplaceInterleave(arr, 0, arr.length - 1);
InplaceRestore(arr, 0, arr.length - 1);
}

static void Reverse(int A[], int left, int right)
{
while (left < right) {
int tmp = A[left];
A[left++] = A[right];
A[right--] = tmp;
... 阅读全帖
q****x
发帖数: 7404
3
来自主题: JobHunting版 - An example of strategy pattern
/*
Is template method in parallel with strategy to abstract different algorithms? If so, how to modify the following example to template method?
*/
#include
using namespace std;
class Robot;
class Search {
public:
virtual void apply(Robot*) = 0;
};
class Linear: public Search {
public:
void apply(Robot*) { cout << "linear" << endl;}
};
class Spiral: public Search {
public:
void apply(Robot*) { cout << "spiral" << endl;}
};
class Robot {
public:
void go() { m_search->ap... 阅读全帖
O******i
发帖数: 269
4
来自主题: JobHunting版 - 探讨IT大公司的hiring bar?
最近面了一家IT大公司被拒,一共经历了N轮技术面试。自己感觉还不算太坏,但也有
三轮发挥不太完美,所以心里很没底。
结果还是被拒了。
下面是这三轮的详细经历,请大家探讨一下大公司招人的标准。
第i轮是找二叉树从根开始的所有路径,使得该路径上所有节点的值之和等于一个给定
的数。我犯了一个战略错误,因为我在准备过程中看过CarrerCup的更通用的解法,不
要求从根开始,也不要求到叶子结束,于是我直接用了那个思路,在白板上写下了类似
下面的代码
void FindPath(Node* root, int sum, int path[], int level)
{
if (root == NULL)
return;
int s = 0;
for (int i = 0; i < level; i++)
s += path[i];
int value = root->data;
if (s + value == sum)
PrintPath(path, level, value);
path[leve... 阅读全帖
w*******s
发帖数: 96
5
If the string is not the same length, my code will crash because of array
index out of boundary. Can you help find the bug?
//Refer Algorithm 4th, page 722
void exch(string &a, string &b)
{
string c = a;
a = b;
b = c;
}
int compare(int a, int b)
{
if (a if (a==b) return 0;
if (a>b) return 1;
}
void ThreeWayQsort(vector &v, int low, int high, int d)
{
if (high<=low) return;
//3 way partition to handle duplicate
int lt = low;
int gt = h... 阅读全帖
w*******r
发帖数: 2953
6
来自主题: JobHunting版 - 一道OO设计题
改完的程序在下面,这下疾病可以随便定义了,也可以治病了。
想给给个病安个计数器,记得好像有个什么关健词可以让所有的object改动的都是
同一个变量而不是各自的变量(counter),记不起来了,所以这个counter打印得
不对,有知道的告诉我一声。
#include
#include
#include
using namespace std;
class Disability
{
public:
Disability(){counter = 0; };
Disability(string, string);
~Disability(){};
string get_disease_name() const { return name; };
string get_disease_desc() const { return desc; };
void incr_sick_human_counter() {counter++; };
unsigned long get_hum... 阅读全帖
w****x
发帖数: 2483
7
来自主题: JobHunting版 - twitter intern面经
"和twitter很相关的一个问题,根据twitter的follow关系,如何efficiently找到所
有的connected components。有一个很大的文件,每行存一条follow关系的边。"
struct EDGE
{
int x;
int y;
EDGE(int a, int b) : x(a), y(b) {}
};
void _inner_find_comp(int x, hash_map>& mp, set& st,
vector& vecComp);
void _insert_conn(int x, int y, hash_map>& mp);
void GetConnectedComp(vector& edges, vector>& comps)
{
hash_map> mp;
for (vector::iterator it = edges.begin(); it !... 阅读全帖
w****x
发帖数: 2483
8
来自主题: JobHunting版 - twitter intern面经
"和twitter很相关的一个问题,根据twitter的follow关系,如何efficiently找到所
有的connected components。有一个很大的文件,每行存一条follow关系的边。"
struct EDGE
{
int x;
int y;
EDGE(int a, int b) : x(a), y(b) {}
};
void _inner_find_comp(int x, hash_map>& mp, set& st,
vector& vecComp);
void _insert_conn(int x, int y, hash_map>& mp);
void GetConnectedComp(vector& edges, vector>& comps)
{
hash_map> mp;
for (vector::iterator it = edges.begin(); it !... 阅读全帖
c*****e
发帖数: 737
9
来自主题: JobHunting版 - 微软C++面试题
1 #include
2
3 class A
4 {
5 public:
6 A(int v_) : v(v_) {};
7 virtual void foo() {std::cout << "A:" << v << "\n";}
8 int v;
9 };
10
11 class B : public A
12 {
13 public:
14 B(int w_, int z_):w(w_), A(z_){};
15 virtual void foo() {std::cout << "B:" << w << "\n";}
16 int w;
17 };
18
19 class C : public A
20 {
21 public:
22 C(int k): A(k){};
23 v... 阅读全帖
S********t
发帖数: 3431
10
来自主题: JobHunting版 - 说说你面过最难的算法coding题目
好吧,那我说清楚些
有个general tree,内部结构就是:
class Tree {
public:
void Walk(Visitor *v) {
// in preorder sequence
v->PreVisit(...);
...
// in postorder sequence
v->PostVisit(...);
...
}
private:
ValueType node_value;
vector children_;
}
class Visitor {
public:
virtual void PreVisit(const ValueType &node_value, bool is_leaf) = 0;
virtual void PostVisit(const ValueType &node_value, bool is_leaf) = 0;
}
Let's say you define your own tree data structure:
struct MyTree ... 阅读全帖
a**U
发帖数: 115
11
来自主题: JobHunting版 - 一个C++的问题!
来源于面试题目,下面的code可以编译运行成功。关键是我觉得不应该编译运行成功,
请看下面我的comments。请高手指点。
#include
using namespace std;
class A
{
public:
virtual void test(){ cout <<"test A"< };
class B : public A
{
public:
virtual void test(){ cout <<"test B"< };
class C : public A
{
int value;
public:
C(){value = 1; }
virtual void test(){ cout <<"test C"< void accessValue() { cout << "value=" << value << endl; }
void setValue(int value){ this->value... 阅读全帖
c*******r
发帖数: 610
12
来自主题: JobHunting版 - select2perform上面C++测试挺头疼的
下面是我的答案,如果有错误,请指正:)
class A
{
virtual void f() throw (int, double, long) = 0;
}
class B :public A (假设)
{
int f();//1
int f() throw(int);//2
void f() throw(double, long, int);//3
void f();//4
void f() throw(long);//5
}
应该是1,2,4,5不对(如果1-5同时出现就都不对了),因为这几个函数都在一个类(
public 继承,因此也包含基类的void f() =0),是overloading,不能依靠return
type 和exception specification 来区别,所以compile time error
如果Class B里面只有3,那么是可以的,或者只有5也可以,只有4 不行,因为4可以抛
出任何异常,但是基类的函数只能抛出int, long, double,只有1或者2也不行。
t**********h
发帖数: 2273
13
来自主题: JobHunting版 - 发一个有趣的java题
今天面了一个和下面类似的一个题,很有意思,大家一起看看。写出打印的东西
class Egg2 {
protected class Yolk {
public Yolk() { print("Egg2.Yolk()"); }
public void f() { print("Egg2.Yolk.f()");}
}
private Yolk y = new Yolk();
public Egg2() { print("New Egg2()"); }
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}
public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.Yolk {
public Yolk() { print("BigEgg2.Yolk()"); }
public void f() { print("BigEgg2.Yolk.f()"); }
}
p... 阅读全帖
d*******u
发帖数: 186
14
来自主题: JobHunting版 - how to implement malloc?
From c programming language:
typedef long Align; /* for alignment to long boundary */
union header { /* block header */
struct {
union header *ptr; /* next block if on free list */
unsigned size; /* size of this block */
} s;
//The Align field is never used; it just forces each header to be
aligned on a worst-case boundary.
Align x; /* force alignment of blocks */
};
typedef union header Header;
static Header base; /* empty list to get started */
static Header *fr... 阅读全帖
z****e
发帖数: 54598
15
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
因为wait的monitor在l上,而不是在ti上
这个问题可以单独剥离出来看,如果写成一堆的话
那就非常不容易阅读了,这是代码
List l = new ArrayList();

public void h() throws InterruptedException {
ThreadInfo ti =null;

synchronized(l){
if (l.size() >= 2) {
ThreadInfo hThread = l.get(0);
ThreadInfo oThread = l.get(l.size() - 1);
if (hThread.isH() && oThread.isO()) {
synchronized(hThread){
hThrea... 阅读全帖
z****e
发帖数: 54598
16
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
因为wait的monitor在l上,而不是在ti上
这个问题可以单独剥离出来看,如果写成一堆的话
那就非常不容易阅读了,这是代码
List l = new ArrayList();

public void h() throws InterruptedException {
ThreadInfo ti =null;

synchronized(l){
if (l.size() >= 2) {
ThreadInfo hThread = l.get(0);
ThreadInfo oThread = l.get(l.size() - 1);
if (hThread.isH() && oThread.isO()) {
synchronized(hThread){
hThrea... 阅读全帖
s*w
发帖数: 729
17
要有人觉得有所帮助,给发个包子
【 以下文字转载自 Programming 讨论区 】
发信人: saw (句子熊), 信区: Programming
标 题: Re: 一道count frequency of all words的面试题 (转载)
发信站: BBS 未名空间站 (Mon Sep 23 00:28:12 2013, 美东)
又琢磨了两天,看了不少相关东西,终于搞定了,觉得自己对这个多线程的理解加强了
很多。思考比单纯的看人说原理更刻骨铭心。
这个问题我设计的用一个 producer 多个 consumer 的架构,上个版本的是两头用
condition_variable, 一个通知 producer 有空间了开始干活生产,另一个通知
consumer 有库存了开始消费。参见上篇里面的 wait 和 notify_all,notify_one 语
句。 这个思路对于单 producer 单 consumer 没问题,可是不适用于 多 consumer.
因为所有的 consumer 可能同时睡觉(没空存),同时醒来(有库存),结果只有一个
能抢占mutex(拿到库存),... 阅读全帖
f********a
发帖数: 165
18
import java.util.HashMap;
public class MyClass {

public static HashMap hashMap = new HashMap Integer>();
private String s = new String();

public void log1(String msg1, String msg2){
synchronized(hashMap){
System.out.println(msg1);
System.out.println(msg2);
}
}

public void log2()
{
hashMap.put(new String("123"), new Integer(1));
}
public static void log3(String ms... 阅读全帖
e********3
发帖数: 18578
19
来自主题: JobHunting版 - 热腾腾g电面 已挂
BFS+DP,而且需要maintain两个距离,一个是到终点的最小距离,一个是到起点的最小
距离,计算终点的距离需要backtrack,到起点的简单比较保存最小值就行了,类似
Dijkstra算法。电面就考这个有点偏难了,尤其还是同胞就操蛋了,要是老中这么考老
印我绝对赞成。简单的实现还可以把障碍物那个的距离设成100啥的,这样自然就知道
要绕过了。
这个题目的难度比reverse linkedlist, atoi难了几个数量级。。。
我贴两个我自己写的代码抛砖引玉一下,第一个是Harry Potter最小的strength通关,
第二个是经典的Dijkstra algorithm,都是附带了测试数据自动生成的方法,你把这两
个组合一下基本就能解这道题了,过两天我有空了来写一下这道题的具体实现。
import java.util.*;
public class HarryPotter{
private static Random rand = new Random();
private static int[][] matrix;
private static ... 阅读全帖
c********l
发帖数: 8138
20
来自主题: JobHunting版 - Leetcode的系统真是弱爆了
//MITBBS' article system will mess up the source code
// You may probably need to re-arrange the line breaks
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Solution {

public static void main(String[] args) {
Solution sol = new Solution();
String[] dictStrArr = new String[] {
"dose","ends","dine","jars","prow","soap","guns","hops","
cray","hove","ella","hour","lens","j... 阅读全帖
c********l
发帖数: 8138
21
简洁不是最终极的目标,有时候需要reduce runtime complexity
见我之前写的
http://www.mitbbs.com/article/JobHunting/32658475_3.html
//MITBBS' article system will mess up the source code
// You may probably need to re-arrange the line breaks
// by coupondeal@mitbbs
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Solution {

public static void main(String[] args) {
Solution sol = new Solution();
String[] dictStrA... 阅读全帖
m*****n
发帖数: 204
22

The following is an O(nlgn) solution for string of size n.
It maintains a heap for eligible chars, preferring longer streams.
Ineligible chars are maintained in a candidate queue, and added back
to the working queue when their position constraint is removed.
public class MinDistance {
public String rearrange(String source, int distance) {
// TODO Check params.

// WorkingQueue of eligible streams. Longest stream at head,
// but BackupStream at tail.
PriorityQueue阅读全帖
x*********n
发帖数: 100
23
来自主题: JobHunting版 - 求问一道multithreading问题
为什么不用两个lock.写java比较少些,不过概念都一样:
Object lock1 = new Object();
Object lock2 = new Object();
void print12() {
Thread t1 = new Thread() {
public void run() {
print1();
}
};

t1.start();

Thread t2 = new Thread() {
public void run() {
print2();
}
};

t2.start();

synchronized(lock1) {
lock1.notify();
}
}
... 阅读全帖
H**********5
发帖数: 2012
24
来自主题: JobHunting版 - 感觉今天结结实实被烙印阴了
#include
#include
typedef struct node
{
int data;
struct node* next;
};
void printfList(struct node *head)
{
while(head!=NULL)
{
printf("%d-> ",head->data);
head=head->next;
}
printf("\n");
}
void pushAtBegin(struct node **head_ref,int data)
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=data;
new_node->next=*head_ref;
*head_ref=new_node;
}
void pushAtEnd(struct node **head_ref,i... 阅读全帖
t*****a
发帖数: 106
25
来自主题: JobHunting版 - 请教L家生成H2O水分子的题。
加mutex是对的,不好意思,看错了。开始想着用while+wait能按顺序输出HHO,发现不
行。写了个code,贴在这
public class H2O {
private Lock lock;
private Semaphore semH;
private Semaphore semO;
H2O()
{
lock=new ReentrantLock();
semH=new Semaphore(0);
semO=new Semaphore(0);
}
public void H() throws InterruptedException
{
semH.release();
semO.acquire();
System.out.println("H");
}
public void O() throws InterruptedException
{
lock.lock();
semH.acquire();
semH.acquire();
lock.unlock();
semO.release();
semO.release();
System.out.println("O");
}
... 阅读全帖
S*******C
发帖数: 822
26
这答案不对,看我测试结果,IntFileIterator对象是mutable的
[1, 2, 3, 4, 5, 9, 8, 7, 6]
[1, 2, 3, 4, 5, 4, 8, 7, 6]
true
=====================
[1, 2, 3, 4, 5, 9, 8, 7, 6]
[1, 2, 3, 4, 5, 9, 7, 6]
false
=====================
[1, 2, 3, 4, 5, 9, 8, 7, 6]
[1, 2, 3, 4, 5, 9, 8, 7, 1, 6]
false
=====================
[1, 2, 3, 4, 5, 9, 8, 7, 6]
[1, 2, 3, 4, 5, 9, 8, 7, 6, 3, 2]
false
=====================
[1, 2, 3, 4, 5, 4, 8, 7, 6]
[1, 2, 3, 4, 5, 9, 7, 6]
false
=====================
[1, 2, 3, 4, 5, 4, 8, 7, 6]
[1, 2, 3, ... 阅读全帖
l*******t
发帖数: 79
27
题目就是find all primes less or equal than n,要求体现OOD和写unit test。求拍
砖,多谢各位!
ps:不确定如果面试中让写C++ unit test,不用gtest, boost.test这些库,写成这样
可以吗?。。。因为之前自己写代码没有养成写unit test的习惯,感觉不太规范。。
#include
#include
#include
#include
#include
using namespace std;
class PrimeFinder {
friend class PrimeFinderTest;
public:
PrimeFinder(int n = 2) {
is_prime_.push_back(false);
is_prime_.push_back(false);
is_prime_.push_back(true);
primes_.... 阅读全帖
k****r
发帖数: 807
28
来自主题: JobHunting版 - 关于死锁疑问
这个死锁的例子,为什么会发生呢?bow里面的bower.bowBack(this)为什么会被block
住呢,不是两个bow function都synchronized了吗?难道说instance的另一个bowBack
也被syn了吗?
concurrency小白,勿怪。
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s"
+ " has bowed to me!%n",
this.... 阅读全帖
m******0
发帖数: 222
29
来自主题: JobHunting版 - How do you OO design a chicken
suppose in the context of a shooter game:
class Matter {
public String matterType; // "meat", "wood", "stone", "shell"
public String texture; // "solid color", "strip", transparent"
public int length, width, weight;
public void Move(Direction direct) {
}
}
class CreaturePart implements Matter {
public boolean canMove, canOffBody;
public CreaturePart connectsTo; // e.g. Wing connect to body
public offBody() {
Move(new Direction(random));
connectsTo = null;
}
}
class Egg ... 阅读全帖
t*********1
发帖数: 39
30
来自主题: NextGeneration版 - 【奶票哭胖转让专帖】 1月
半价+shipping 转让coupon和奶票:
Enfamil $5 check 一张(void after Jan 31, 2011)
Similac maximum $25.99 off coupon 一张(expiration date
Feb 28, 2011)
Similac $5 check 两张 (void after Feb 2, 2011)
Similac $5 check 四张 (void after Feb 27, 2011)
Similac $5 check 一张 (void after Mar 3, 2011)
Similac $5 check 两张 (void after Mar 6, 2011)
email联系: y*****[email protected]
S*********s
发帖数: 1963
31
不是说不能standby到晚一班,而是说不能在起飞之后再去added onto standby list,
因为机票会被void,就无从standby了。
而要added on standby list,只能去机场,我就是因为有急事,临时去不了机场。这
才有问题。
就是不知道起飞多少小时后会被void,目前三个UNITED的csr三个说法,一个是起飞后
就被void,一个是2hr后被void,一个是24hr内还不会void,还能added on standby
list,只要是standby当天的航班就是。
T*P
发帖数: 2697
32
要小心,就是put on standby list,真有一旦miss一整天都standby不上的情况。

不是说不能standby到晚一班,而是说不能在起飞之后再去added onto standby list,
因为机票会被void,就无从standby了。
而要added on standby list,只能去机场,我就是因为有急事,临时去不了机场。这
才有问题。
就是不知道起飞多少小时后会被void,目前三个UNITED的csr三个说法,一个是起飞后
就被void,一个是2hr后被void,一个是24hr内还不会void,还能added on standby
list,只要是standby当天的航班就是。
d**********o
发帖数: 1321
33
来自主题: WebRadio版 - 潜水员冒泡兼征版友意见
RTOS作业:2514
The purpose of this assignment is to give you more experience using the AVR
ports, and to add some I/O devices that might become useful later for our
RTOS.
You are to use one of the provided keypads to implement a “digital lock.”
Your program should allow the user to enter a four digit code from the
keypad, and if the code that is input matches the one included in your
program, the “lock” should open. In this case, the lock opening will be
represented by the lighting of an LED.
As th... 阅读全帖
d**********o
发帖数: 1321
34
来自主题: WebRadio版 - 潜水员冒泡兼征版友意见
RTOS作业:2514
The purpose of this assignment is to give you more experience using the AVR
ports, and to add some I/O devices that might become useful later for our
RTOS.
You are to use one of the provided keypads to implement a “digital lock.”
Your program should allow the user to enter a four digit code from the
keypad, and if the code that is input matches the one included in your
program, the “lock” should open. In this case, the lock opening will be
represented by the lighting of an LED.
As th... 阅读全帖
k****u
发帖数: 1686
35
来自主题: Fengshui版 - 六爻入门——基础篇
声明
1.此资料中内容是六爻占卜术的最基础的知识;
2.我能整理此资料,并不代表我的技术高,这只是个人机缘的关系。
3.资料中的内容、观点与论坛无关。
整理此资料的目的
1.为了让不懂六爻占卜的朋友对其有所了解;
2.给想学习六爻占卜的易友一个入门的基础资料;
3.为了大家相互交流与个人卦技的提高。
资料内容简述如下:
第一部分 为占卜术基础内容,此中内容为所有占卜方法都使用的基础知识;
第二部分 为六爻占卜术的基础内容,其为六爻占卜方法专用的基础知识。
第三部分 为六爻占卜中一些常见的基础问题的个人浅见。
学习方法:
学习占卜不是一天两天的事儿,我们以六爻为例,你学会了起卦的方法,这并不代
表你就会解卦了,有的人学习了十几年,几十年也没有看到门,可是有的人学了几个月
甚至几天就可以分析简单的卦了,有的人不服气,有的人不明白,其实这其中除了个人
的机缘与命运不同以外,还有个学习方法的问题。(以下内容为一家之言,仅供参考)
1、学习六爻占卜术,首先要有一个稳定的心态,因为时下派系繁多,又都有各自
的一套技法,而我们这些新人也没有办法去分析那一家对那一家错,所以我建议... 阅读全帖
k**t
发帖数: 1108
36
来自主题: Xibei版 - 搬家后
哈哈,我也写,
while (1)
{
if( bHungry ){
Eat( pFood );
BSO( void );
}
elseif( bSleepy ){
if ( Sleep(void) == bWithGirl )
BSO( void );
}
else{
灌水( void );
BSO( void );
}
if (!bLive) break;
}
o***g
发帖数: 2784
37
来自主题: Java版 - 白痴求助
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloBean
implements SessionBean
{
public String hello(String name)
{
return "Hello " + name + "!";
}
public void ejbCreate() {};
public void ejbRemove() {};
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext ctx) {}
}
上面这个Stateless Session Bean怎么把他弄成Web Service,在Jboss4上。
我是java白痴,最好从javac Hello
i**p
发帖数: 902
38
来自主题: Java版 - Java Interface
As I know, the Java Interface can not be used to define objects. But this
example is from a book.
How can we use an interface DotsChangeListener to define dotsChangeListener?
private DotsChangeListener dotsChangeListener;
/** A list of dots. */
public class Dots {
/** DotChangeListener. */
public interface DotsChangeListener {
/** @param dots the dots that changed. */
void onDotsChange(Dots dots);
}
private final LinkedList dots = new LinkedList();
private final List... 阅读全帖
b**l
发帖数: 51
39
or this, works on both 32-nit and 64-bit:
====
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
public class WinRegistry {
// inspired by
// http://javabyexample.wisdomplug.com/java-concepts/34-core-
java/62-java-registry-wrapper.html
// http://www.snipcode.org/java/1-java/23-java-class-for-accessing-
reading-and-writing-... 阅读全帖
l******y
发帖数: 182
40
有这样一个问题:
// 一个数数类。其中,minus只有在count >= 10的时候才能被激活。
class Count {
public count = 0;
private lock = ReentrantLock();
private Condition condition = lock.newCondition();
public void add() {
lock.lock();
count += 1;
condition.signalAll();
lock.unlock();
}
public void minus() {
lock.lock();
while (count < 10)
condition.await();
count -= 10;
lock.unlock();
}
}
// 调用数数类
class Foo {
public static Co... 阅读全帖
p****s
发帖数: 32405
41
如题,一直用老的2.6.11 kernel compile driver,
这两天刚升级到2.6.24, 发现原来的code不能编译了,
其中一个就是这个INIT_WORK的macro改了,原来老的kernel上
prototype里要传三个参数,
INIT_WORK(struct work_struct *work, void (*function)(void *), void *data);
现在看, 只剩俩了,
INIT_WORK(struct work_struct *work, void (*function)(void *));
难道原来的data被wrap到work里去了? 简单的把第三个argument去掉然后
放到workqueue里虽然能编译, 但是数据是会丢的.
vi
发帖数: 309
42
来自主题: Programming版 - 菜鸟读C++ STL源程序的疑问
看STL list node的定义:
struct _List_node_base
{
_List_node_base* _M_next; ///< Self-explanatory
_List_node_base* _M_prev; ///< Self-explanatory
static void
swap(_List_node_base& __x, _List_node_base& __y);
void
transfer(_List_node_base * const __first,
_List_node_base * const __last);
void
reverse();
void
hook(_List_node_base * const __position);
void
unhook();
};
template
struct _List_node : public _List_node
Q**g
发帖数: 183
43
来自主题: Programming版 - A weird segmentation fault!
change:
IntList.h
void operator=(IntList newList);
==>
void operator=(IntList& newList);
and
IntList.cpp
void IntList::operator=(IntList newList)
==>
void IntList::operator=(IntList& newList)
if you have "void operator=(IntList newList)", there is a copy construction
involved and a temp object is created in a bitwise copying fashion. When the
temp goes out of the scope, its destructor destroys the list which is
shared with your actual parameter.
m*******o
发帖数: 264
44
来自主题: Programming版 - C++ 弱问一个
请问这个继承最后输出的怎么是BB,而不是CC呢?main函数里的逻辑过程是什么样的?
#include
using namespace std;
class A{
protected:
virtual void print() { cout << "A" << endl; }
void print2() { cout << "AA" << endl; }
};
class B {
public:
virtual void print() { cout << "B" << endl; }
void print2() { cout << "BB" << endl; }
};
class C : public A, public B{
public:
void print2() { cout << "CC" << endl; }
};
int main()
{
C c;
B* p = &c;
p->print2();
};
i**p
发帖数: 902
45
Which one is the a valid function prototype? Is it C only?
A. template void Display(std::vector& obj);
B. template void Display(std::vector& obj);
C. template void Display(std::vector& obj);
D. template void Display(std::vector& obj);
E. template void Display(std::vector& obj);
s***e
发帖数: 122
46
我对你对这个Warning/Error的理解有不同看法,我觉得那是因为this在new完成前是不
存在的,不过我同意你说的编译器给加上了static,嗯,我事实上同意了this在static函数里不存在:P
事实上,我用VC6, VS2008, g++ 4.3.1都测试了一下,的确operator new不管是不是
static都可以编译通过,而且连warning都没有。
我猜测可能是C++标准里面就是这么说的吧。
#include
#include
class NewTest
{
public:
NewTest() {}
virtual ~NewTest() {}

public:
void* operator new(size_t size) {
void *ptr = (void *)malloc(size);
printf("my own new\n");
return ptr;
}

void operator delete(void
s***e
发帖数: 122
47
为了实现多态,C++里增加了virtual这么一个关键字来修饰类的成员函数。
如果一个成员函数是virtual的,那么当你通过一个对象调用它的时候,无论你用的指
针是基类还是子类,都会调到这个对象的实际类型所实现的这个函数。
你可以跑一下下面这个程序看看virtual和非virtual的区别,然后你就知道shadow为什
么和virtual这么相关了。
#include
class A {
public :
void f() { printf("A::f()\n"); };
virtual void g() { printf ("A::g()\n"); }
};

class B : public A {
public:
void f() { printf("B::f()\n"); }
virtual void g() { printf("B::g()\n"); }
};

void main() {
B b;
A* a = &b;
a->f();
a->g(
p****s
发帖数: 32405
48
来自主题: Programming版 - INIT_WORK从Linux kernel 2.6.20后改了?
【 以下文字转载自 Linux 讨论区 】
发信人: plutus (愿HERO的小朋友们都健康快乐), 信区: Linux
标 题: INIT_WORK从Linux kernel 2.6.20后改了?
发信站: BBS 未名空间站 (Wed Apr 1 12:23:50 2009), 转信
如题,一直用老的2.6.11 kernel compile driver,
这两天刚升级到2.6.24, 发现原来的code不能编译了,
其中一个就是这个INIT_WORK的macro改了,原来老的kernel上
prototype里要传三个参数,
INIT_WORK(struct work_struct *work, void (*function)(void *), void *data);
现在看, 只剩俩了,
INIT_WORK(struct work_struct *work, void (*function)(void *));
难道原来的data被wrap到work里去了? 简单的把第三个argument去掉然后
放到workqueue里虽然能编译, 但是数据是会丢的.
b********e
发帖数: 58
49
来自主题: Programming版 - 这题怎么搞?
The following code (I know it's ugly) seems to do the job.
Tested with gcc under cygwin.
#include
#include
void add(int);
void finish(int);
void (*p[2])(int x);
int sum = 0;
int main(int argc, char **argv)
{
p[0] = add;
p[1] = finish;

doit(atoi(argv[1]));
return(0);
}
int doit(int n)
{
int next;

sum += n;
next = n-1;
(*p[next==0])(next);
}
void add(int x)
{
doit(x);
}
void finish(int x)
{
printf("sum = %d\n", sum);
exit(0);
}
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)