由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请问这样写程序错了吗?
相关主题
G家电面,这回肯定挂了。附面经。请问strcpy()和memcpy()的写法问题
问一个memory allocate/release的问题how to access a const char array in a function
请教大家一道关于c++的面试题问个题?
放c code求老师傅指教C++ 面试题
菜鸟求救 请大家看看我的代码有没有问题A challenging interview question: revert a string
这个Strategy design pattern的例子为什么人为得弄得这么复杂?这题谁知道答案?
bloomberg onsite 面经Facebook phone screen
问一个精华区里的题目分享一道最近碰到的很好的面试题。
相关话题的讨论汇总
话题: pstring2话题: pstring1话题: char话题: strlen话题: int
进入JobHunting版参与讨论
1 (共1页)
q*******i
发帖数: 353
1
int main()
{
char *pString1 = "AAA";
char *pString2 = "BBBBB";
int len = strlen(pString2);
char *NewpString1 = pString1 ;

for(int i=0;i {
*pString1++ = *pString2++;
}
}
return 1;
}
运行之后是 Access violation 。 难道因为pString1 是 string literal,不能写?
要怎么改呢?谢谢
s*****n
发帖数: 5488
2
because you are writing in constant TEXT segment.

【在 q*******i 的大作中提到】
: int main()
: {
: char *pString1 = "AAA";
: char *pString2 = "BBBBB";
: int len = strlen(pString2);
: char *NewpString1 = pString1 ;
:
: for(int i=0;i: {
: *pString1++ = *pString2++;

y*******o
发帖数: 6632
3
"AAA" is a constant char* not char*

【在 q*******i 的大作中提到】
: int main()
: {
: char *pString1 = "AAA";
: char *pString2 = "BBBBB";
: int len = strlen(pString2);
: char *NewpString1 = pString1 ;
:
: for(int i=0;i: {
: *pString1++ = *pString2++;

c***2
发帖数: 838
4
1) You are doing double loops: i++ and *p++
2) You did not check whether *p still points to something


【在 q*******i 的大作中提到】
: int main()
: {
: char *pString1 = "AAA";
: char *pString2 = "BBBBB";
: int len = strlen(pString2);
: char *NewpString1 = pString1 ;
:
: for(int i=0;i: {
: *pString1++ = *pString2++;

c***2
发帖数: 838
5
This program works fine:
d*****t
发帖数: 41
6
2楼正解~
q*******i
发帖数: 353
7
那这个程序怎么改对呢?

【在 s*****n 的大作中提到】
: because you are writing in constant TEXT segment.
q*******i
发帖数: 353
8
这个程序我这里VC也不能运行,错误一样。难道是编译器不一样,有些情况“AAA”在
text字段,有些情况不在?

【在 c***2 的大作中提到】
: This program works fine:
j****k
发帖数: 91
9
二楼应该是这个题目的正解.
但是如果稍微改一下题目, 把pString1,2的定义改为:
char *pString1, *pString2;
pString1 = (char *)malloc(3*sizeof(char));
pString2 = (char *)malloc(21*sizeof(char));
strcpy(pString1,"AAA");
strcpy(pString2,"BBBBBBBBBBBBBBBBBBBB");
这样在for循环里走过第三步的时候pString1就已经到头了, 但为什么接下来的循环都
不会出错? 虽然说c++没有边界检测, 但程序在修改pString1越界指向的内容, 这难道
不该是Access violation吗?
s***f
发帖数: 226
10
for(int i=0;i {
pString1 = pString2;
}
}
相关主题
这个Strategy design pattern的例子为什么人为得弄得这么复杂?请问strcpy()和memcpy()的写法问题
bloomberg onsite 面经how to access a const char array in a function
问一个精华区里的题目问个题?
进入JobHunting版参与讨论
c***2
发帖数: 838
11
I also use VC compiler, but only in cmd, not in dde.
Did you include the headers?
stdio/string

【在 q*******i 的大作中提到】
: 这个程序我这里VC也不能运行,错误一样。难道是编译器不一样,有些情况“AAA”在
: text字段,有些情况不在?

c***2
发帖数: 838
12
this is not the issue. you are just using heap memory instead of stack
memory
Also don't forget to free

【在 j****k 的大作中提到】
: 二楼应该是这个题目的正解.
: 但是如果稍微改一下题目, 把pString1,2的定义改为:
: char *pString1, *pString2;
: pString1 = (char *)malloc(3*sizeof(char));
: pString2 = (char *)malloc(21*sizeof(char));
: strcpy(pString1,"AAA");
: strcpy(pString2,"BBBBBBBBBBBBBBBBBBBB");
: 这样在for循环里走过第三步的时候pString1就已经到头了, 但为什么接下来的循环都
: 不会出错? 虽然说c++没有边界检测, 但程序在修改pString1越界指向的内容, 这难道
: 不该是Access violation吗?

c***2
发帖数: 838
13
this loop won't do anything.
if you want p1 point to p2, one assignment is enough.
I guess you want to do:
pString1[i] = pString2[i];
in that case, you want to check whether p1 has enough memory to hold all of
p2.
otherwise, random memory overwrite may happen, will cause crash.

【在 s***f 的大作中提到】
: for(int i=0;i: {
: pString1 = pString2;
: }
: }

s***f
发帖数: 226
14
careless mistake, yes you are right.
just pString1 = pString2; is enough

of

【在 c***2 的大作中提到】
: this loop won't do anything.
: if you want p1 point to p2, one assignment is enough.
: I guess you want to do:
: pString1[i] = pString2[i];
: in that case, you want to check whether p1 has enough memory to hold all of
: p2.
: otherwise, random memory overwrite may happen, will cause crash.

m******n
发帖数: 1691
15
不知道你这个程序在干嘛。
你的每次for loop,strlen(pString2) 都要重算一遍,每次都会变。
这种程序对于读你的code的人来说简直是噩梦。
如果你在面试中写出这种程序,就直接挂了。

【在 q*******i 的大作中提到】
: int main()
: {
: char *pString1 = "AAA";
: char *pString2 = "BBBBB";
: int len = strlen(pString2);
: char *NewpString1 = pString1 ;
:
: for(int i=0;i: {
: *pString1++ = *pString2++;

e*********n
发帖数: 4599
16
有些高手就是喜欢不写通俗易懂的程序

【在 m******n 的大作中提到】
: 不知道你这个程序在干嘛。
: 你的每次for loop,strlen(pString2) 都要重算一遍,每次都会变。
: 这种程序对于读你的code的人来说简直是噩梦。
: 如果你在面试中写出这种程序,就直接挂了。

1 (共1页)
进入JobHunting版参与讨论
相关主题
分享一道最近碰到的很好的面试题。菜鸟求救 请大家看看我的代码有没有问题
分享A公司面经这个Strategy design pattern的例子为什么人为得弄得这么复杂?
a MS interview question about C++bloomberg onsite 面经
请教一个字符串比较排序的问题 (转载)问一个精华区里的题目
G家电面,这回肯定挂了。附面经。请问strcpy()和memcpy()的写法问题
问一个memory allocate/release的问题how to access a const char array in a function
请教大家一道关于c++的面试题问个题?
放c code求老师傅指教C++ 面试题
相关话题的讨论汇总
话题: pstring2话题: pstring1话题: char话题: strlen话题: int