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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:20:52+00:00 2026-05-27T05:20:52+00:00

I was reviewing my skills with pointers and buffer in C++. I tried the

  • 0

I was reviewing my skills with pointers and buffer in C++. I tried the code below and everything works fine. No leaks, no crash, nothing.

To be honest I didn’t expect this.

When I call char* buf2 = new char[strlen(buf)] I didn’t expect srlen(buf) returning the right size. I always thought that strlen
needs a NULL terminated string to work. Here it is not the case so why it is working this code?

        int main(){
             const char* mystr = "mineminemine";
             char* buf = new char[strlen(mystr)];
             memcpy(buf, mystr, strlen(mystr));

             char* buf2 = new char[strlen(buf)];
             memcpy(buf2, buf, strlen(buf));

             delete[] buf2;
             delete[] buf;
        }
  • 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-27T05:20:52+00:00Added an answer on May 27, 2026 at 5:20 am

    That’s called undefined behavior – the program appears working but you can’t rely on that.

    When memory is allocated there happens a null character somewhere that is close enough to the start of the buffer and the program can technically access all memory between that null character and the start of the buffer so you don’t observe a crash.

    You can’t rely on that behavior. Don’t write code like that, always allocate enough space to store the terminating null character.

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

Sidebar

Related Questions

I'm reviewing some code from one of our contractors: if (userLists != null) {
I was reviewing a code from a simple PONG game code in c#, the
I was recently reviewing some code written by two different contractors, both were basic
When coding and reviewing code, it's easy to spot places where a design pattern
Reviewing our code I've found a curious definition in one of .idl files: [
Reviewing a quite old project I found the following curious code snippet (only relevant
When reviewing code I came across something odd, someone had read that you can
I am reviewing some C code, and found a header file full of defines
I'm reviewing some code and I thought it would be useful for Visual Studio
Reviewing some code that contains a bunch indirectly nested transactionscopes. I would like to

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.