Run-Time Check Failure #3-the variable i is being used without being definedCode:#include <stdio.h> #include <string.h> int main(int argc,char * argv[]) {int i; char buf[4]; strcpy (buf,"aaaa"); printf("%d\n",i); return 0; }
why ........
Run-Time Check Failure #3-the variable i is being used without being definedCode:#include <stdio.h> #include <string.h> int main(int argc,char * argv[]) {int i; char buf[4]; strcpy (buf,"aaaa"); printf("%d\n",i); return 0; }
why ........
1st, you're trying to print the value of i, but you have not assigned a value to i. So there's no telling what's in the memory location containing i.
2nd, you're trying to copy a 4 char string into an array of 4 elements, you need 1 more element to hold the '\o' null terminating char.
[edit] Oh just noticed too... if you're trying to program in C++ you should be using cout << i instead of the printf() statement. I won't bother about the format of your #includes because that just may be because you're using an older compiler.
Last edited by Scribbler; 02-13-2005 at 01:56 AM.
like this ??..Code:#include <stdio.h> #include <string.h> int main(int argc,char * argv[]) {int i=3; char buf[5]; strcpy (buf,"aaaa"); printf("%d\n",i); return 0; }
why these codes can run without mistake??Code:#include <stdio.h> main() { char buff[4]; strcpy(buff,"aaaa"); printf("%s",buff); }
Because 5 chars "aaaa\0" is larger than 4 chars (char buff[4])
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Adding to what Salem said:
A 'string literal' like "aaaa" is actually stored in memory like this:
aaaa\0
So, when you use strcpy() to try and copy that into an array with length=4, it won't work.
Last edited by 7stud; 02-13-2005 at 10:15 AM.
Since you have written into memory where you weren't supposed to, the behavior is undefined. Undefined means that the program might or might not give the appearance of running as expected (maybe the memory you wrote to was not being used for anything else at that time). It might do anything. If you are lucky it will give a run-time error so that you know that something is wrong. If you are not lucky, the program will appear to run OK and will give expected output. Then, sometime in the future, this program, or a program that incorporates this code will cause user problems that you didn't forsee.Originally Posted by pacific
Nothing in the C language checks for memory accesses out of bounds. It is the programmer's responsibility.
This is extremely important, and it is an illustration that you can't prove a program is correct by testing it.
When I ran the following, I could see the consequences of out-of-range array access:
With my compilers I get this (no compiler error or warning messages; no run-time messages):Code:#include <stdio.h> #include <string.h> int main(void) { int x=-1; char buff[4]; int y=-1; printf("Before strcpy:\n"); printf("x = %d\n", x); printf("y = %d\n\n", y); strcpy(buff,"aaaa"); printf("buff: <%s>\n\n",buff); printf("After strcpy:\n"); printf("x = %d\n", x); printf("y = %d\n", y); return 0; }
What do you get?Before strcpy:
x = -1
y = -1
buff: <aaaa>
After strcpy:
x = -256
y = -1
Regards,
Dave