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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:44:41+00:00 2026-05-31T23:44:41+00:00

Possible Duplicate: Why does this intentionally incorrect use of strcpy not fail horribly? Below

  • 0

Possible Duplicate:
Why does this intentionally incorrect use of strcpy not fail horribly?

Below see below code:

char* stuff = (char*)malloc(2);
 strcpy(stuff,"abc");
 cout<<"The size of stuff is : "<<strlen(stuff);

Even though I assigned 2 bytes to stuff, why does strcpy still work and the output of strlen is 3. Shouldn’t this throw something like index out of bounds?

  • 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-31T23:44:42+00:00Added an answer on May 31, 2026 at 11:44 pm

    C and C++ don’t do automatic bounds checking like Java and C# do. This code will overwrite stuff in memory past the end of the string, corrupting whatever was there. That can lead to strange behavior or crashes later, so it’s good to be cautious about such things.

    Accessing past the end of an array is deemed “undefined behavior” by the C and C++ standards. That means the standard doesn’t specify what must happen when a program does that, so a program that triggers UB is in never-never-land where anything might happen. It might continue to work with no apparent problems. It might crash immediately. It might crash later when doing something else that shouldn’t have been a problem. It might misbehave but not crash. Or velociraptors might come and eat you. Anything can happen.

    Writing past the end of an array is called a buffer overflow, by the way, and it’s a common cause of security flaws. If that “abc” string were actually user input, a skilled attacker could put bytes into it that end up overwriting something like the function’s return pointer, which can be used to make the program run different code than it should, and do different things than it should.

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

Sidebar

Related Questions

Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: What does this C++ code mean? I'm trying to map a C
Possible Duplicate: Weird java behavior with casts to primitive types Why does this code
Possible Duplicate: Why does this Random Number Generator not random? I have this test
Possible Duplicate: Why does this trigger fail? It says invalid identifier. CREATE MATERIALIZED VIEW
Possible Duplicate: Got Hacked - Anyone know what this PHP Code Does? I just
Possible Duplicate: What does this line of code mean? I was reading an article
Possible Duplicate: Why does this Javascript code inside a non-Javascript browser have extra commenting?
Possible Duplicate: Python list append behavior Why does this code: x = [[]]*3 x[0].append('a')
Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like

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.