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

  • Home
  • SEARCH
  • 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 7181443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:33:28+00:00 2026-05-28T17:33:28+00:00

i am making a custom copy function like this: if (s1 == NULL ||

  • 0

i am making a custom copy function like this:

  if (s1 == NULL || s2 == NULL || s2->buf == NULL)
    return;

  if (s1->len + 1 < s2->len + 1)
    {
      if (s1->buf)
        free (s1->buf);

      s1->buf = (char *) malloc (s2->len + 1);
      if (s1->buf == NULL)
      return;
    }

  for (int i = 0; i < s2->len; i++)
    s1->buf[i] = s2->buf[i];

  s1->len = s2->len;

where as s1 and s2 are simply struct pointers that have a char pointer and an unsigned integer in them.

  if (s1->len + 1 < s2->len + 1)
    {
      if (s1->buf)
        free (s1->buf);

      s1->buf = (char *) malloc (s2->len + 1);
      if (s1->buf == NULL)
      return;
    }

this will check to see if there is enough space inside of the s1 to fit for s2. if there isn’t, it will then check for existing memory and free it. after that is done, it will allocate a new memory block for s1, which is the size of s2 + 1 (include \0).

here is where i am confused

if there is enough space in the char pointer for s2 to fit into s1 (including \0) and it doesn’t free() and call malloc() again, it just assigns with the for loop, will it keep the original contents of s1?

  • 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-28T17:33:30+00:00Added an answer on May 28, 2026 at 5:33 pm

    if there is enough space in the char pointer for s2 to fit into s1
    (including \0) and it doesn’t free() and call malloc() again, it just
    assigns with the for loop, will it keep the original contents of s1?

    If there is enough space you don’t need to free or malloc again. Just hold on to the allocated chunk.

    Calling malloc isn’t something special: it simply gives you a pointer where you can start storing data. If you already have that pointer, great.

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

Sidebar

Related Questions

I'm making custom events for C# and sometimes it isn't working. This is how
This is a question on making custom fields in Django. I'm making a field
I'm making a custom message box that lets you copy text, but I wanted
I'm making a custom list of nodes and their comments. I'd like to be
Hi im making a custom Textbox i got everty thing working but how do
I am making a custom form object in Django which has an overrided __init__
I'm making a custom event calendar with PHP. I am trying to get the
I'm making a custom control that can be dragged around and it is semi
I'm making a custom error dialog in my WPF app and I want to
I am making a custom control I need to add some default keybindings, microsoft

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.