Programming experience (1)
Define the problem of string
2004-10-13
Author: Yang Yanqing E-mail: Blankmanattomdotcom is here: http://blog.9cbs.net/blankman/archive/2004/10/24/Programing_experience1.aspx
A friend encountered in the development of the development, the TC3 is compiled, the operation is normal; the compilation of VC6 is passed, running off-bounds; Linux GCC Version
3.2.2
Compilation pass, run the Segmentation Fault error.
[Source Procedure]
#include
#include
int main ()
{
CHAR CH;
Char * s = "Hello!";
INT i = 0;
INT J = Strlen (S) -1;
While (j> i)
{
Printf ("/ Ni =% D / T J =% D", I, J);
CH = s [J];
s [j] = s [i];
S [I] = CH;
i ;
J -;
}
Printf ("% s / n", s);
Getch ();
Return 0;
}
[LINUX] G Test1.cpp -o test1 ./test1 segmentation fault No output before the error prompt, single-step debugging is normal before returnit. [Problem reason] GCC "char * s =" Hello! "S points to a constant space, so it cannot be assigned. The TC3 border check is not very strict, so there is no error. [Solution] The definition is in the form of "char s [] =" Hello! ". [Unplenish problem] I have time to put the assembly code of TC3 and GCC, it will be clear. Next: Programming Experience (2) - "The problem of parameter stack sequence (2004-10-24)" Declaration: Original, copyright, please indicate the source. Http://blog.9cbs.net/blankman/archive/2004/10/24/programing_experience1.aspx