为什么这很糟糕 [英] why is this bad
问题描述
char * s =" " ;;
int main()
{
strcpy(s,Hello World);
}
这个程序很好地崩溃了。我认为s是一个指向字符串的指针
存储在可执行内存中,因此strcpy会覆盖程序的部分
,对吧?
我不会这样做,但是我发现了这个错误,我希望能够将这个错误解释给那个写它的人。
谢谢,
-
-Gernot
int main(int argc,char ** argv){printf
(%silto%c%cf%cgl%ssic%ccom%c,ma,58,''g'',64," ba",46,10); }
________________________________________
想找个好游戏?亲自动手吧!
GLBasic - 你可以这样做
www.GLBasic .com
char* s=" ";
int main()
{
strcpy(s, "Hello World");
}
This program crashes nicely. I think s is a pointer to a string that
is stored in the executable memory, so the strcpy will overwrite part
of the program, right?
I would not do this, but I found this error and I want to be able to
explain it to the one who wrote it.
Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
推荐答案
我想字符串文字存储在只读内存中。试试
char sz [256] =" " ;;
//等。
I guess the string literal is stored in read-only memory. Try
char sz[256] = " ";
//etc.
" Gernot Frisch" < Me@Privacy.net>在消息中写道
news:31 ************* @ individual.net ...
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:31*************@individual.net...
char * s =" " ;;
int main()
{
strcpy(s,Hello World);
}
此程序很好地崩溃了。我认为s是指向存储在可执行内存中的字符串的指针,因此strcpy会覆盖部分程序,对吧?
我不会这样做,但我发现了这个错误,我希望能够把它解释给那个写它的人。
谢谢,
-
-Gernot
int main(int argc,char ** argv){printf("%silto%c%cf%cgl%ssic%ccom%c",
" ma",58,''g' ',64," ba",46,10);}
________________________________________
寻找好游戏?亲自动手吧!
GLBasic - 你可以做
www.GLBasic.com
char* s=" ";
int main()
{
strcpy(s, "Hello World");
}
This program crashes nicely. I think s is a pointer to a string that is
stored in the executable memory, so the strcpy will overwrite part of the
program, right?
I would not do this, but I found this error and I want to be able to
explain it to the one who wrote it.
Thank you,
--
-Gernot
int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",
"ma", 58, ''g'', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
字符串文字实际上是const char *类型(即使你声明
你的只是char *) ,这可能就是崩溃的原因。
所以当声明字符串文字时总是使用类型const char *所以
编译器可以捕获这样的错误......如果你想修改字符串,声明
它为:
char foo [] = {" hello" };
/ WP
String literalts are really of type const char* (even though you declared
yours as just char*), that''s probably the reason for the crash.
So when declaring string literals always use type const char* so the
compiler can catch such errors...if you want to modify the string, declare
it as:
char foo[] = { "hello" };
/ WP
" William Payne" < ER ************* @ yahoo.com>在消息中写道
news:cp ********** @ news.island.liu.se ...
"William Payne" <er*************@yahoo.com> wrote in message
news:cp**********@news.island.liu.se...
Gernot弗里希" < Me@Privacy.net>在消息中写道
新闻:31 ************* @ individual.net ...
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:31*************@individual.net...
char * s =" " ;;
int main()
{
strcpy(s,Hello World);
}
此程序很好地崩溃。
char* s=" ";
int main()
{
strcpy(s, "Hello World");
}
This program crashes nicely.
字符串文字实际上是const char *类型(即使你宣称你的只是char *),这可能是崩溃的原因。
String literalts are really of type const char* (even though you declared
yours as just char*), that''s probably the reason for the crash.
当然,有趣的是,为什么C ++类型机制
允许你初始化类型为< char *的对象>有一些东西
真的是一个< const char *> ?!?
IMO这是C ++类型机制的缺陷。
- Risto -
The interesting bit, of course, is why does C++ type mechanism
allow you to initialize an object of type <char *> with something
that really is a <const char *> ?!?
IMO that''s a flaw in C++ type mechanism.
- Risto -
这篇关于为什么这很糟糕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!