String is getting printed even after the size of array is exceeded

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Linish Francis
    New Member
    • Mar 2015
    • 6

    String is getting printed even after the size of array is exceeded

    #include<iostre am.h>
    #include<conio. h>
    void main()
    {
    clrscr();
    char name[5];
    cout<<"Enter your name";
    cin>>name;
    cout<<"Your name is"<<name;
    getch();
    }

    In the above program the size of the array of the variable name is 5. which means the variable cant store more than 5 characters.
    which also means
    If I give the string "LINISH"
    It should only print LINIS
    But while the program is running, Even if I type a 10 characters string, It is getting printed Completely..why ???

    Kindly be kind enough to go through the below program also:

    #include<iostre am.h>
    #include<conio. h>
    void main()
    {
    clrscr();
    char name[5];
    char game[5]
    cout<<"Enter your name";
    cin>>name;
    cout<<"Enter your favorite game";
    cin>>game;
    cout<<name"Love s"<<game;
    getch();
    }

    In this program when I input a string for the variable name,It is getting printed completely, irrespective how many characters are there in the string.
    But If the string (which is input to the second variable that is game )holds more than 5 characters. the input of the first variable(name) is getting disturbed..why? ?/

    look at the below cited output to be more clear about my doubts.
    OUTPUT NO:1

    Enter your name:LINISHFRAN CIS (Note that the input holds more than five chars)
    Enter your game:GOLF(input is less than five chars)

    LINISHFRANCIS loves GOLF(Two inputs are getting printed comopletely)

    OUTPUT NO:
    Enter your name:LINISHFRAN CIS (Note that the input holds more than five chars)
    Enter your game:FOOTBALL(i nput is more than five chars)

    ALL loves FOOTBALL [Note that "ALL" is the last three letters of FOOTBALL

    I am using TurboC++ for windows 7

    Thanks in advance
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There are no array controls in C or C++. There are no string controls either. So, a string will print until a \0 is encountered regardless of how much memory is involved. Try it sometime. Omit the \0 and print the string and see what happens.

    An array is just a piece of allocated memory. You can store a 100 byte string in an array of 5 bytes. Of course, the last 95 bytes is laying outside the array memory and has just trashed something else and when your program uses those bytes it crashes with a memory corruption error.

    Your experience with the string and printf is called indeterminate results. Maybe it works and maybe it doesn't.

    You are the one managing memory. Not the compiler.

    You also might read: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    • Linish Francis
      New Member
      • Mar 2015
      • 6

      #3
      Thank you much.....the first half of my question is answereds clearly...i really wonder if you could answer the second half too..

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        It's like I said: Indeterminate behavior.

        I compiled your code and ran it on Visual Studio 2013 and I get: LinishFrancislo vesfootball followed by a runtime crash that says the memory in the area of "games" is corrupt.

        Comment

        • Linish Francis
          New Member
          • Mar 2015
          • 6

          #5
          Is it?... I am using tubo C++ version 3 and yours is visual studio.Each one is responding one type for the same coding. So it must be depending on the software used to run the program I guess.anyways thanks a lot.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Each compiler is its own implementation of C. So it's not unexpected that different compilers generate different code.

            Comment

            Working...