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 | |
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;
}
} |
|
|
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的人来说简直是噩梦。 : 如果你在面试中写出这种程序,就直接挂了。
|