CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    153

    null terminator, using setw() char aaray

    Hi

    1: The code and the excerpt below are from a book. I don't understand the part in red. I understand that setw() specifies the number of numbers and characters which can be inserted. For example, in the given code "20" characters would be inserted into the array though the user can type any number of characters more than 20 but only first 20 entered would be taken seriously. Suppose the 20th entered character for the given code was 'z'. According to what the book says, 'z' would be replaced by a null or in other words 'z' won't even be taken seriously. But how? How does setw() know that it has to disregard the 20th input?

    This program uses the setw manipulator to specify the maximum number of characters the input buffer can accept. The user may type more characters, but the >> operator won’t insert them into the array. Actually, one character fewer than the number specified is inserted, so there is room in the buffer for the terminating null character. Thus, in SAFETYIN,a maximum of 19 characters are inserted
    Code:
    // safetyin.cpp
    // avoids buffer overflow with cin.width
    #include <iostream>
    #include <iomanip>                  //for setw
    using namespace std;
    
    int main()
       {
       const int MAX = 20;              //max characters in string
       char str[MAX];                   //string variable str
    
       cout << "\nEnter a string: ";
       cin >> setw(MAX) >> str;         //put string in str,
                                        // no more than MAX chars
       cout << "You entered: " << str << endl;  
       return 0;
       }


    2:
    A C-string is terminated by a null which is essentially an integer 0.

    Code:
    int main()
    {
       char array[20];
    
       for (int i=0; i<(20 - 1); i++)
    {
       cin >> array[i];
    }
    
       // rest of the code
    }
    Once you have entered 19 characters, the for loop would exit and the compiler would automatically insert a null at 20th position. But look at it little deeply. When we defined the array above, it was initialized to some garbage values; you can say at 20th position we had some random value. Correct? When the loop exited, the random value at 20th position was overridden by a null. I hope I have it right. But suppose the limit for the loop was
    Code:
    (int i=0; i<25; i++)
    and we had inputted more characters than the size of the array. Won't the compiler in this case again override the value at 20th position because [20] is the allowed array size and the compiler knows this. The compiler does override the random/garbage value as I mentioned.

    Please help me with the queries above. Thanks.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: null terminator, using setw() char aaray

    All your questions can be answered in debugger session with memory window on showing your buffer content. Along with writing array elements the window will show the changes in red.

    Regarding 2. Your assumption is wrong. Compiler never does null termination when you operate with the buffer char elements. Only c-string-aware functions do such termination. And writing outside the allocated buffer memory is going to corrupt adjacent memory (corrupting function stack in your case ). This is known as buffer overflow.

    ...and the compiler knows this
    The thing to remember: compiler knows nothing but what you instruct it to do. In your code it's just a buffer defined as 20 chars. It even cannot assume this is a string, and only you know that. If you need it to know you need a string, you use some string type. The same way, you can instruct it to check buffer overruns, and then it will know this to check.
    Last edited by Igor Vartanov; October 12th, 2011 at 02:14 AM.
    Best regards,
    Igor

  3. #3
    Join Date
    Mar 2011
    Posts
    153

    Re: null terminator, using setw() char aaray

    Thank you, Mr. Igor.

    I understand it now.

    Best wishes
    H

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured