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 8003429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:38:49+00:00 2026-06-04T16:38:49+00:00

I was wondering how the following code works. #include <iostream> using namespace std; int

  • 0

I was wondering how the following code works.

#include <iostream>
using namespace std;
int main()
{
    char* buffer = new char(NULL);
    while(true)
    {
        cin >> buffer;
        cout << buffer;
        cout << endl;
    }
    return 0;
}

I can input any amount of text of any size and it will print it back out to me. How does this work? Is it dynamically allocating space for me?

Also, if I enter in a space, it will print the next section of text on a new line.
This however, is fixed by using gets(buffer); (unsafe).

Also, is this code ‘legal’?

  • 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-06-04T16:38:51+00:00Added an answer on June 4, 2026 at 4:38 pm

    It’s not safe at all. It’s rewriting whatever memory happens to lie after the buffer, and then reading it. The fact that this is working is coincidental. This is because your cin/cout operations don’t say “oh, a pointer to one char, I should just write one char” but “oh, you have enough space allocated for me.”

    Improvement #1:

    char* buffer = new char(10000) or simply char buffer[10000];

    Now you can safely write long-ish paragraphs with no issue.

    Improvement #2:

    std::string buffer;

    To answer your question in the comment, C++ is all for letting you make big memory mistakes. As noted in comment this is because it’s a “don’t pay for what you don’t need” language. There are some people who really need this level of optimization in their code although you are probably not one of them.

    However, it also gives you plenty of ways to do it where you don’t have to think about memory at all. I will say firmly: if you are using new and delete or char[] and not because you are using a design pattern with which you’ve familiarized that require them, or because you are using 3rd-party or C libraries that require them, there is a safer way to do it.

    Some guidelines that will save you 80% of the time:

    -Don’t use char[]. Use string.

    -Don’t use pointers to pass or return argument. Pass by reference, return by value.

    -Don’t use arrays (e.g. int[]). Use vectors. You still have to check your own bounds.

    With just those three you’ll be writing “pretty safe”, non-C-like code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm slowly understanding closures more and more, and the following code works. I'm wondering
I'm wondering why the following code doesn't work: String test = new String(new byte[]
I was wondering how can I allow NULL values in the following code below
I'm wondering if using the following Javascript code is reliable: if (!document.cookie) { alert('Cookies
I was wondering if someone could explain how the following code works: public interface
I have written the following code which works, but im wondering can I make
The following T-SQL code segment works but i'm wondering if there's a cleverer and/or
I'm using the following code... var result = document.evaluate(expr,context,null,9,null); When printing the result I
I'm using gcc 4.4 on Debian squeeze. Consider the following code. #include <map> #include
I am wondering why the following contrived example code works perfectly fine in Visual

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.