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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:21:56+00:00 2026-05-27T17:21:56+00:00

I am trying to do something useful for a wrapper I am working out.

  • 0

I am trying to do something useful for a wrapper I am working out. I am writing a wrapper for the standard string library for C because I don’t like it.

I don’t want to introduce C strings to my wrapper right now. Ihave a struct called `str which contains the string’s length and the buffer:

struct str
{
    char *buf;
    size_t len;
};

In my header file I have this:

typedef struct str str;

Now I have implemented encapsulation (I think).

So I declare strings like this:

str *a_string = NULL;

I have already done everything that I want to do with this, but I have one problem. What I would like to add something like this for one function:

str.h:

extern str *str_str(str *, char *);

and str.c

str *str_str(str *buf, char *ns)
{
    buf->len = strlen(ns);
    buf->buf = (char *) malloc(buf->len + 1);
    strncpy(buf->buf, ns, buf->len);
    return buf;
}

and testing this works well:

str *s = str_new();
printf("%s\n", str_cstr(str_str(s, "hello")));

output: hello

but will this work?

str_assign(s, str_str(s, "ok"));

That’s essentially assigning s to s, I think. When I print, it prints nothing!

I don’t get any errors!
All help is greatly appreciated. I am fairly new with the C language.

Source of str_assign():

void str_assign(str *s1, str *s2)
{
    s1->len = s2->len;
    s1->buf = (char *) malloc(s2->len + 1);
    strncpy(s1->buf, s2->buf, s2->len);
}
  • 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-27T17:21:57+00:00Added an answer on May 27, 2026 at 5:21 pm

    I know this is C, but it is useful to compare this with C++. When you write an assignment operator in C++, you always have to consider the case of self-assignment. The same applies when you do the equivalent job in C.

    Therefore, I think you need:

    void str_assign(str *s1, const str *s2)
    {
        if (s1 != s2)
        {
            free(s1->buf);  // Added!
            s1->len = s2->len;
            if ((s1->buf = (char *) malloc(s2->len + 1)) != 0)
                memcpy(s1->buf, s2->buf, s2->len + 1);
            else
                s1->len = 0;  // Is this sufficiently safe with a null buf?
        }
    }
    

    You can use memcpy() because (a) the strings are guaranteed to be disjoint, and (b) you know how long the string is, so you don’t need to check for the end of string at each step of the way as you would with strncpy() or strcpy().

    Actually, there’s a case for saying you should never need to use strcpy() or strncpy() or strcat() or strncat(); you should always know how long the source and destination strings are (otherwise you can’t be sure that there won’t be a buffer overflow), so you can always use memmove() or memcpy() instead.


    I also note that you’re going to have to worry about memory leaks at some point. I just amended the assignment above to release the old string before overwriting it with the new.

    You could also optimize operations by only allocating new space when the new string is longer than the old. You could consider using realloc() instead of malloc(), too. However, you must be careful of the memory leak trap. This is the wrong way to use realloc():

    s1->buf = realloc(s1->buf, s1->len + 1);
    

    If realloc() fails, you’ve just overwritten your point with a null – losing the only reference you had to the old space. You should always store the result of realloc() in a different variable from the first argument:

    char *new_buf = realloc(s1->buf, s1->len + 1);
    if (new_buf == 0)
        ...handle out of memory condition...
    

    You might eventually decide to keep two lengths in your structure – the space allocated and the space used. Then you can reuse space more efficiently, only allocating more space when the new string is longer than the previously allocated space, but still allowing you to shorten a string at any time.

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

Sidebar

Related Questions

I'm trying something like this Output.py print Hello Input.py greeting = raw_input(Give me the
I'm trying something like this, but this example does not work. jsObj = {};
I'm trying something like this: [ServiceContract ( CallbackContract = typeof (CallbackContract_1), CallbackContract = typeof
As the title goes I am trying something like $(ul li:nth-child(' + xx +
Is it possible to set for(AssociatedControlID) attribute using jQuery? I am trying something like
I'm trying out something in jQuery and stumbled upon a site http://pupunzi.com/ , on
I'm trying something really simple here, but can't figure out where I'm going wrong.
I'm working with the pcap library and I'm trying to capture the source and
I am trying to create my own std::string wrapper to extend its functionality. But
Trying to do something weird that might turn into something more useful, I tried

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.