can you tell me why11?

This is a discussion on can you tell me why11? within the C++ Programming forums, part of the General Programming Boards category; Code: #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; } ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    24

    can you tell me why11?

    Code:
    #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;
    }
    Run-Time Check Failure #3-the variable i is being used without being defined
    why ........

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    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;
    }
    like this ??..

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    Code:
    #include <stdio.h>
    main()
    { char buff[4];
     strcpy(buff,"aaaa");
     printf("%s",buff);
    }
    why these codes can run without mistake??

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by pacific
    Code:
    #include <stdio.h>
    main()
    { char buff[4];
     strcpy(buff,"aaaa");
     printf("%s",buff);
    }
    why these codes can run without mistake??
    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.

    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:


    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;
    }
    With my compilers I get this (no compiler error or warning messages; no run-time messages):
    Before strcpy:
    x = -1
    y = -1

    buff: <aaaa>

    After strcpy:
    x = -256
    y = -1
    What do you get?

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21