由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 攒人品 报BB面经
相关主题
问两个题Virtual pointer是每个object都有一个吗?
c++ 程序一问Leetcode OJ的编译器是?
请教一道c/c++题 (转载)请教一个指针的面试题
分享A公司面经发一个fb面经
C++问题3寻找子序列/子段落
这题咋做?Pure Storage面经
a virtual table question by JP morgan这题有好办法吗?
请教一个bloomberg题目这题哪错了?
相关话题的讨论汇总
话题: char话题: foo话题: hello话题: world话题: pointer
进入JobHunting版参与讨论
1 (共1页)
n******n
发帖数: 49
1
昨天早晨电面BB,no luck.
主要是对印度人的口音还是有些不习惯,而且不明原因昨天有一段时间电话背景噪音非
常大,非常影响面试。
题目如下:
1. 打印Hello World字符串。这题看起来简单,但是如果概念不清,很容易弄错或者绕
很久。
回忆了一下,回头又编了一次,代码应该是
void foo(char** a){
*a = "hello world";
}
int main(){
char* c;
foo(&c);
printf("%s",c);
return 0;
}
这题的trap就在char* c之后,只是声明了c但并没有定义c。如果用debugger trace,就
会发现&c = 0x0012ff40但是c是一个bad pointer,即里面是garbage value, 这时候如
果直接用foo(c),编译器会提示错误: the variable "c" can't be used without
definition. 这和下面这段程序是一个道理:
void bar(int a){
cout << a << endl;
}
int main(){
int c;
bar(c); // can't do this! c has garbage value now.
return 0;
}
这里面的原因就在于c是在栈上生成的,所以系统给的是garbage value, 不是默认值0.
所以唯一的办法 就是把c本身的地址传进foo,即把foo写成void foo(char** c), 在
main里调用foo(&c).希望版上的各位看了以后 不要和我犯一样的错误!
第二题以后就简单了。
2. c中static的用法。
3. oo design: how to design a car (wheels, steer, engine, number of seats...)
4. c++ polymorphism
5. what is dynamic binding? how is it implemented with vptr and vtbl?
6. 5 gallon bottle and 3 gallon bottle -> how to get 4 gallon warter?
早晨就受到了thank you letter, 被bb发了好人卡,版上各位继续加油!
r********s
发帖数: 556
2
谢谢分享
c*****o
发帖数: 519
3
Thanks for sharing!
有点没搞清楚,第一题为什么不是从主程序传一个字符串到子程序里打印?

【在 n******n 的大作中提到】
: 昨天早晨电面BB,no luck.
: 主要是对印度人的口音还是有些不习惯,而且不明原因昨天有一段时间电话背景噪音非
: 常大,非常影响面试。
: 题目如下:
: 1. 打印Hello World字符串。这题看起来简单,但是如果概念不清,很容易弄错或者绕
: 很久。
: 回忆了一下,回头又编了一次,代码应该是
: void foo(char** a){
: *a = "hello world";
: }

i**********e
发帖数: 1145
4
void foo(char** a){
*a = "hello world";
}
int main(){
char* c;
foo(&c);
printf("%s",c);
return 0;
}
这个程序有两个错误。
1) *a = "hello world";
This syntax is incorrect. The only exception is during intialization of a C-
style string constant like this: char *a = "hello world"; Note that this is a C-style string constant, if you try to modify the string, the result is undefined.
In this case, you should use strcpy(a, "hello world");
2) c is a pointer to a char. Since it is not initialized, it does not point to anywhere, it might even have an invalid address (ie, address space that's occupied by other processes or the null address 0x0000). You should allocate memory to it, such as malloc or new operator. The other way is use static memory with array (ie, char c[10]).
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 n******n 的大作中提到】
: 昨天早晨电面BB,no luck.
: 主要是对印度人的口音还是有些不习惯,而且不明原因昨天有一段时间电话背景噪音非
: 常大,非常影响面试。
: 题目如下:
: 1. 打印Hello World字符串。这题看起来简单,但是如果概念不清,很容易弄错或者绕
: 很久。
: 回忆了一下,回头又编了一次,代码应该是
: void foo(char** a){
: *a = "hello world";
: }

l****z
发帖数: 9
5
r u really on work?

【在 i**********e 的大作中提到】
: void foo(char** a){
: *a = "hello world";
: }
: int main(){
: char* c;
: foo(&c);
: printf("%s",c);
: return 0;
: }
: 这个程序有两个错误。

g******d
发帖数: 511
6
第一题可以char* c = NULL吗?
o*****e
发帖数: 99
7
Sigh...,
Why are so many misleading information here.
You guys really should try it out before post here.
char* c;
is not a "declare" in this case, it is a definition.
since the stack will allocate 4bytes(8bytes for 64bit machine) for it.
And you don't have to initialize this pointer if you decide to give it a
value in foo().
#include
#include
void foo(char** a){
static char A[20] = "hello world";
*a = A;
}
int main(){
char* c;
foo(&c);
printf("%s",c);
return 0;
}

【在 i**********e 的大作中提到】
: void foo(char** a){
: *a = "hello world";
: }
: int main(){
: char* c;
: foo(&c);
: printf("%s",c);
: return 0;
: }
: 这个程序有两个错误。

g********d
发帖数: 203
8
Isn't string "Hello World" on foo's stack and it gets deallocated as soon as
foo is done?

【在 o*****e 的大作中提到】
: Sigh...,
: Why are so many misleading information here.
: You guys really should try it out before post here.
: char* c;
: is not a "declare" in this case, it is a definition.
: since the stack will allocate 4bytes(8bytes for 64bit machine) for it.
: And you don't have to initialize this pointer if you decide to give it a
: value in foo().
: #include
: #include

g********d
发帖数: 203
9
Nevermind, I didn't see your static keyword.

as

【在 g********d 的大作中提到】
: Isn't string "Hello World" on foo's stack and it gets deallocated as soon as
: foo is done?

s********k
发帖数: 6180
10
我怎么觉得这个没问题啊,你用的是GCC?C确实是bad ptr,但是你传的是指向指针的
指针&C,这个就没问题了。虽然可能不是很严谨,高手看看对不对

【在 n******n 的大作中提到】
: 昨天早晨电面BB,no luck.
: 主要是对印度人的口音还是有些不习惯,而且不明原因昨天有一段时间电话背景噪音非
: 常大,非常影响面试。
: 题目如下:
: 1. 打印Hello World字符串。这题看起来简单,但是如果概念不清,很容易弄错或者绕
: 很久。
: 回忆了一下,回头又编了一次,代码应该是
: void foo(char** a){
: *a = "hello world";
: }

相关主题
这题咋做?Virtual pointer是每个object都有一个吗?
a virtual table question by JP morganLeetcode OJ的编译器是?
请教一个bloomberg题目请教一个指针的面试题
进入JobHunting版参与讨论
L*******e
发帖数: 114
11
这个程序没有问题。
1) pass address of pointer.
2) "hello world" is string literal, which is statically allocated; therefore
it is safe to return from a function.
I was asked same question from different aspect in an interview:
what is the output the following program:
void test( char *p )
{
p = new char[10];
memset(p, 'A', 10);
}
int main()
{
char *k;
test(k);
cout << k << endl;
return 0;
}
b********h
发帖数: 119
12
我觉得楼主的概念不清,楼上的一些回答也有误导。这段程序没有问题,只不过foo里
面有一个类型转化的warning罢了。
*a = "hello world";
从string constant到char*. "hello world"是string literal,既不在栈上,也不在
堆上,而在静态的数据区。
char* c; 既是声明又是定义,只不过值是垃圾罢了。
a******4
发帖数: 70
13
我用VS2010测试了一下,楼主的code没问题
"hello world"不是C-Style string, 而是string literals所以
*a = "hello world"是合法的
s*y
发帖数: 472
14
The hello world program is OK. But unnecessarily long.
The whole problem is that you make even the simplest things complicated.
This means you don't have a good understanding of the language or with
little experience. This line of code does it all:
printf("%s\n", "Hello World");

【在 n******n 的大作中提到】
: 昨天早晨电面BB,no luck.
: 主要是对印度人的口音还是有些不习惯,而且不明原因昨天有一段时间电话背景噪音非
: 常大,非常影响面试。
: 题目如下:
: 1. 打印Hello World字符串。这题看起来简单,但是如果概念不清,很容易弄错或者绕
: 很久。
: 回忆了一下,回头又编了一次,代码应该是
: void foo(char** a){
: *a = "hello world";
: }

z****n
发帖数: 1379
15
我也忍了半天没说。。。这题为啥直接打印hello world不行,要整那么复杂?面试官要
求的?

【在 s*y 的大作中提到】
: The hello world program is OK. But unnecessarily long.
: The whole problem is that you make even the simplest things complicated.
: This means you don't have a good understanding of the language or with
: little experience. This line of code does it all:
: printf("%s\n", "Hello World");

s*y
发帖数: 472
16
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;
}

【在 L*******e 的大作中提到】
: 这个程序没有问题。
: 1) pass address of pointer.
: 2) "hello world" is string literal, which is statically allocated; therefore
: it is safe to return from a function.
: I was asked same question from different aspect in an interview:
: what is the output the following program:
: void test( char *p )
: {
: p = new char[10];
: memset(p, 'A', 10);

1 (共1页)
进入JobHunting版参与讨论
相关主题
这题哪错了?C++问题3
请教这题怎么做啊这题咋做?
2道面试题a virtual table question by JP morgan
发篇面经请教一个bloomberg题目
问两个题Virtual pointer是每个object都有一个吗?
c++ 程序一问Leetcode OJ的编译器是?
请教一道c/c++题 (转载)请教一个指针的面试题
分享A公司面经发一个fb面经
相关话题的讨论汇总
话题: char话题: foo话题: hello话题: world话题: pointer