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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:58:33+00:00 2026-05-25T14:58:33+00:00

note: please suppress comments about C++? Use std::string! . This question is using C

  • 0

note: please suppress comments about “C++? Use std::string!”. This question is using C strings but is more about memory management than strings in general.

I have this function

char* strclone( char* src )
{
    char* dst = (char*)malloc(strlen(src+1));
    strcpy(dst,src);
    return dst;
}

The function should work to allocate a new pointer (in strclone), write the string in src to it, and return the address of the new string.

However, when the string is freed, some time later in the program:

str = strclone( some_str_variable );    
// ..code..
free( str ) ; //! ERROR!  

The error reads:

Debug Error!

Program: C:\…

HEAP CORRUPTION DETECTED: after Normal block (#39713) at 0x090CC448.
CRT detected that the application wrote to memory after end of heap buffer.

The error occurs at the line where I call free( str ) in the program. If I change the assignment of str to this:

str = (char*)malloc( strlen( some_string_variable ) +1);
strcpy( str, some_string_variable ) ;
//...
free( str ) ; //fine now

Then there is no bug, the program functions perfectly.

Why does the strclone function not work as expected?

  • 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-25T14:58:33+00:00Added an answer on May 25, 2026 at 2:58 pm

    I believe that the problem is that you’ve written

    (char*)malloc(strlen(src+1));
    

    Notice that in the call to strlen, you have written

    strlen(src + 1)
    

    instead of

    strlen(src) + 1
    

    This first line says “the length of the string starting one character past src,” which is the length of the string minus one (or total garbage if the string is empty). The second one is the one you want – the length of the string plus one for the null terminator. If you use the first version, then in the line

    strcpy(dst,src);
    

    You will end up writing past the end of the buffer, leading to the dreaded Undefined Behavior. In your case, this was manifesting itself with a heap corruption error when you try to free the block, which makes sense because you have indeed corrupted the heap!

    Try moving the +1 out of the parentheses and see if it fixes things.

    Alternatively, most compilers ship with a nonstandard function called strdup that does exactly what your above function attempts to do. You might want to consider just using that instead.

    Hope this helps!

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

Sidebar

Related Questions

NOTE: Please ignore my use of MultivaluedMap instead of multiple vargs String...args . Is
(Note: Please do not mark this question as duplicate. This question is based on
Please note this question is specific to WCF Data Services not normal Wcf Service.
Please note the Edit below for a lot more information, and a possible solution
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note : The code in this question is part of deSleeper if you want
Please note this link Render HTML as an Image is not helpful. In previously
I have a feeling that this is a subjective question, but perhaps there's something
please note to this: public class test extended Jframe implement actionlistener{ test() { Jpanel
Please note, I am a beginner with all of this. I am trying 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.