为什么char *在执行一段时间后被覆盖...... [英] why char * gets overwritten after some time in execution...
问题描述
Hye all,
我在调试我的程序时(或者在运行时
执行中)注意到有时(不常规)char *指针得到
被覆盖并失去其价值...
所以,因为这个我的几个产品都失败了,这是我有的原因强制使用char []而不是char *
,因为char []不会覆盖我们的数据...
所以,任何人来自你可以让我明白为什么会这样吗?
假设我的代码是,
char * name;
strcpy(名称,Jigar Mehta);
..
..
..
..
//经过一些执行(比如说50行),如果我看到
名称地址中有什么内容,它会被覆盖并且价值变为而不是
Jigar Mehta......
Jigar Mehta写道:Hye all,
我在调试程序时(或者在运行时执行)时注意到,有时(不常规)char *指针被覆盖并失去其值。 ..
所以,因为这个我的几个产品都失败了,这就是我使用char []而不是char *
强制使用它的原因因为char []不要覆盖我们的数据...
所以,你们中的任何人都可以让我理解为什么会这样???
假设我的代码是,
char * name;
strcpy(name,Jigar Mehta);
您的问题的一般答案 - 使用标准: :string。
你的问题是char *是char的* POINTER *(或字符数组中的第一个
元素)。
定义:
>
char * str;
根本不为char分配任何存储空间,如果你写入
的单位化 ; STR"你正在调用 - 未定义的行为。
例如:
char * str; strcpy(str,这超出了记忆中的一些随机位置);
您可以分配自动通过在
函数中放置一系列字符然后指向它来存储,但这是另一个来源
未完成的行为。
>
例如
char * foo()
{
char数据[100];
char * pointer_to_char;
pointer_to_char =& data [0];
strcpy(str," ;当执行离开范围时,这已经被拆除了);
返回pointer_to_char;
}
谢谢你的回复,但我是你建议的std :: string新手..
你请告诉我如何在程序中使用这个代码...我
了解概率。那个char *正在......我在MS编程
VC ++ 6.0 ..那么,我能正常使用std :: string还是任何
特别的.h应该包括在内..
再次感谢,
Jigar Mehta
Jigar Mehta写道:
感谢您的回复,但我是您建议的std :: string的新手。
你请告诉我怎么样通过给出代码在程序中使用它......我理解了概率。那个char *正在......而且我在MS编程VC ++ 6.0 ..所以,我能正常使用std :: string还是应该包含任何
特殊的.h ...... / blockquote>
你需要一本书。说真的。
加速C ++通过Koenig& Moo
通常是个不错的选择。
-
Karl Heinz Buchegger
kb ****** @ gascad.at
Hye all,
I have noted while debugging my program (or also in runtime
execution) that sometimes (not regularly) char * pointer gets
overwritten and looses its value...
So, because of this my couple of products have failed and this is the
reason I have made it compulsary to use char [] instead of char *
because char[] don''t overwrite our data...
So, anybody from you can please make me understand why this happens ??
suppose my code is,
char *name;
strcpy(name, "Jigar Mehta");
..
..
..
..
//After some lines of execution (say 50 lines) if I see what''s there in
name address, it gets overwritten and value becomes "" instead of
"Jigar Mehta"...
解决方案Jigar Mehta wrote:Hye all,
I have noted while debugging my program (or also in runtime
execution) that sometimes (not regularly) char * pointer gets
overwritten and looses its value...
So, because of this my couple of products have failed and this is the
reason I have made it compulsary to use char [] instead of char *
because char[] don''t overwrite our data...
So, anybody from you can please make me understand why this happens ??
suppose my code is,
char *name;
strcpy(name, "Jigar Mehta");
General answer to your problem - USE std::string.
Your problem is that "char *" is a *POINTER* to a char (or the first
element in an array of chars).
The definition :
char * str;
Does not allocate any storage for "char"s at all and if you write to an
unitialized "str" you are invoking - UNDEFINED BEHAVIOUR.
For example:
char * str; strcpy(str, "THIS OVERWRITES SOME RANDOM PLACE IN MEMORY");
You can allocate "automatic" storage by placing an array of chars in the
function and then point to it however this is another source of
"UNDEFINED BEHAVIOUR".
e.g.
char * foo()
{
char data[ 100 ];
char * pointer_to_char;
pointer_to_char = &data[0];
strcpy(str, "THIS IS DEALLOCATED WHEN EXECUTION LEAVES SCOPE");
return pointer_to_char;
}
Thanks for you reply, but I am new to std::string that you suggested..
You please tell me how to use this in program by giving a code... I
understand the prob. that char * is doing... and I am programming in MS
VC++ 6.0.. So, will I be able to use std::string normally or any
special .h should be included..
Thanks again,
Jigar Mehta
Jigar Mehta wrote:
Thanks for you reply, but I am new to std::string that you suggested..
You please tell me how to use this in program by giving a code... I
understand the prob. that char * is doing... and I am programming in MS
VC++ 6.0.. So, will I be able to use std::string normally or any
special .h should be included..
You need a book. Seriously.
"Accelerated C++" by "Koenig & Moo"
is often a good choice.
--
Karl Heinz Buchegger
kb******@gascad.at
这篇关于为什么char *在执行一段时间后被覆盖......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!