Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:28:36+00:00 2026-05-27T23:28:36+00:00

Here is an easy question. I wrote this C++ code: char chaine[12]; cin.width(12); cin

  • 0

Here is an easy question. I wrote this C++ code:

    char chaine[12];
    cin.width(12);
    cin >> chaine;

But if I enter some text longuer than 12 characters at runtime, visual studio inform me that the stack is now corrupted.

I understand that the problem is a buffer overflow. But I thought that the “width” method would protect against this.

Could someone explain to me what is the function of the width method if it does not protect against the buffer overflow? I searched online but i did not find anything.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:28:37+00:00Added an answer on May 27, 2026 at 11:28 pm

    This code is indeed clearly supposed to limit the input to 11 characters (the 12th character is used for the terminating null character). The standard clearly states in 27.7.2.2.3 [istream::extractors] paragraphs 7 and 8:

    … If width() is greater than zero, n is width(). … Characters are extracted and stored until any of the following occurs: – n-1 characters are stored; …

    I also tried it with gcc which clearly only reads 11 characters. I don’t know what the best work-around for this problem is. Typically, I don’t run into problems like this because I simple read std::string objects which can grow as big as they want. Well, there is some huge limit as well and I have never tried what happens when this would get exceeded. If you absolutely need to read into a char array you could do two things:

    1. you can create an adapter for char arrays and define a suitable input operator yourself
    2. you could create a filtering stream buffer which is temporarily installed and which limits the number of characters or pretends it read a space character.

    Below is an example on how to do the latter. The technique for creating the adapter can actually be used to set the width automatically based on the array’s size.

    #include <iostream>
    #include <cctype>
    
    struct adaptor
    {
        template <int Size>
        adaptor(char (&array)[Size]): it(array), end(array + Size - 1) {}
        mutable char*  it, * end;
    };
    
    std::istream& operator>> (std::istream& in, adaptor const& value)
    {
        std::istreambuf_iterator<char> it(in), end;
        if (it == end)
        {
            in.setstate(std::ios_base::failbit);
        }
        for (; it != end && value.it != value.end && !std::isspace(static_cast<unsigned char>(*it));
             ++it, ++value.it)
        {
            *value.it = *it;
        }
        *value.it = 0;
        return in;
    }
    
    int main()
    {
        char buffer[12];
        if (std::cin >> adaptor(buffer))
            std::cout << "read='" << buffer << "'\n";
        else
            std::cout << "input failed\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a really easy question to answer, but for some reason I'm
This is most likely not an easy one but here is the situation: I
Here is an easy question. How does the industry refer to storing mulitple boolean
Here's an easy one straight from the text book I can't seem to find.
I assume this is pretty easy so here goes; I'm trying to execute these
first post here, and probably an easy one. I've got the code from Processing's
I may be missing something here because I thought this might be really easy
i encountered a bug in some c code i wrote, and while it was
I know this is a vague open ended question. I'm hoping to get some
Note: This is an extension of an earlier question I asked here: Do additional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.