Simple Question
-
char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V
-
char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V
it won't work as you are trying to cat a 12 characters long string into a 3 characters long array... [edit] there will be an "out of range exception"... According to the MSDN[^], "Because strcpy does not check for sufficient space in strDestination before copying strSource, it is a potential cause of buffer overruns." [/edit]
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005 -
char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V
-
char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V
char *strcpy( char *strDestination, const char *strSource ); The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking is performed when strings are copied or appended. Have a look at the strlen() function. Regards
We can do no great things, only small things with great love. - Mother Theresa
-
-
-
it won't work as you are trying to cat a 12 characters long string into a 3 characters long array... [edit] there will be an "out of range exception"... According to the MSDN[^], "Because strcpy does not check for sufficient space in strDestination before copying strSource, it is a potential cause of buffer overruns." [/edit]
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005 -
this is not happening .. its displayin "codeproject" then what's the use of declaring it as the var t2 as [3] ? i dont understand! :confused: V
if you can see it, it is pure luck ! it is because - as it's been asnwsered to me here - there is some empty room after the char[3] variable, but never you should think there will always be... moreover, what do you think about releasing the memory ?
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:26 Monday 29th August, 2005 -
-
TOX, i too got "CodeProject".. y no error is displayed? V -- modified at 7:32 Monday 29th August, 2005
yes, i understood that point. did you visit the link to the msdn i provided ? strcpy() doesn't check for sufficient space in strDestination before copying strSource...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005 -
char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V
To complete the previous answers: It will perhaps output 'Codeproject' but you cannot be sure because strcpy is copying the charcters outside the bounds of t2 (so in 'unprotected memory'). So, this memory is not 'locked' and your program can write other things in it. Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value that can lead to really baaaaaad things ;-) (like your variable changed magically)
-
if you can see it, it is pure luck ! it is because - as it's been asnwsered to me here - there is some empty room after the char[3] variable, but never you should think there will always be... moreover, what do you think about releasing the memory ?
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:26 Monday 29th August, 2005 -
if you can see it, it is pure luck ! it is because - as it's been asnwsered to me here - there is some empty room after the char[3] variable, but never you should think there will always be... moreover, what do you think about releasing the memory ?
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:26 Monday 29th August, 2005 -
u know buffer overflow error when u declare it as t2[3]="Codeproject".... releasing ? what will it do here? V
i think it's a good point to wonder how things work, but there's no reason to do so when you perfectly know it is a bad thing to do... doing t2[3]="Codeproject", it will free t2, but t2 don't have "Codeproject" in it... (the constructor thrown the exception while trying to assigning the string...) (ps: you can delete your other postwritten twice)
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:44 Monday 29th August, 2005 -
To complete the previous answers: It will perhaps output 'Codeproject' but you cannot be sure because strcpy is copying the charcters outside the bounds of t2 (so in 'unprotected memory'). So, this memory is not 'locked' and your program can write other things in it. Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value that can lead to really baaaaaad things ;-) (like your variable changed magically)
cedric moonen wrote: Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value cedric, if the other variable is declared ,as u said it could reside only in 'protected memory' then how will it(unsure strcpy!) overwrite on a content which is in protected memory?.. plz expln Thanx V -- modified at 7:46 Monday 29th August, 2005
-
yes, i understood that point. did you visit the link to the msdn i provided ? strcpy() doesn't check for sufficient space in strDestination before copying strSource...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005 -
cedric moonen wrote: Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value cedric, if the other variable is declared ,as u said it could reside only in 'protected memory' then how will it(unsure strcpy!) overwrite on a content which is in protected memory?.. plz expln Thanx V -- modified at 7:46 Monday 29th August, 2005
Sorry, protected was not really the good word. What I meant is memory allocated for another variable. strcpy will never check 1) that it writes still in the bounds of the string 2) if the memory it writes to is already allocated or not. When you declare a variable, memory is allocated for it to holds its value. So if something write at this location in memory, the value of the variable will altered.
-
Sorry, protected was not really the good word. What I meant is memory allocated for another variable. strcpy will never check 1) that it writes still in the bounds of the string 2) if the memory it writes to is already allocated or not. When you declare a variable, memory is allocated for it to holds its value. So if something write at this location in memory, the value of the variable will altered.