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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:08:12+00:00 2026-05-28T07:08:12+00:00

my main concern is if i am doing this safely, efficiently, and for the

  • 0

my main concern is if i am doing this safely, efficiently, and for the most part doing it right.

i need a bit of help writing my implementation of a string class. perhaps someone could help me with what i would like to know?

i am attempting to write my own string class for extended functionality and for learning purposes. i will not use this as a substitute for std::string because that could be potentially dangerous. 😛

when i use std::cout to print out the contents of my string, i get some unexpected output, and i think i know why, but i am not really sure. i narrowed it down to my assign function because any other way i store characters in the string works quite fine. here is my assign function:

void String::assign(const String &s)
{
    unsigned bytes = s.length() + 1;

    // if there is enough unused space for this assignment
    if (res_ >= bytes)
    {
        strncpy(data_, s.c_str(), s.length()); // use that space
        res_ -= bytes;
    }
    else
    {
        // allocate enough space for this assignment
        data_ = new char[bytes];
        strcpy(data_, s.c_str()); // copy over
    }

    len_ = s.length(); // optimize the length
}

i have a constructor that reserves a fixed amount of bytes for the char ptr to allocate and hold. it is declared like so:

explicit String(unsigned /*rbytes*/);

the res_ variable simply records the passed in amount of bytes and stores it. this is the constructor’s code within string.cpp:

String::String(unsigned rbytes)
{
    data_ = new char[rbytes];
    len_ = 0;
    res_ = rbytes;
}

i thought using this method would be a bit more efficient rather than allocating new space for the string. so i can just use whatever spaced i reserved initially when i declared a new string. here is how i am testing to see if it works:

#include <iostream>

#include "./string.hpp"

int main(int argc, char **argv)
{
    winks::String s2(winks::String::to_string("hello"));
    winks::String s(10);

    std::cout << s2.c_str() << "\n" << std::endl;

    std::cout << s.unused() << std::endl;
    std::cout << s.c_str() << std::endl;
    std::cout << s.length() << std::endl;

    s.assign(winks::String::to_string("hello")); // Assign s to "hello".

    std::cout << s.unused() << std::endl;
    std::cout << s.c_str() << std::endl;
    std::cout << s.length() << std::endl;

    std::cout.flush();
    std::cin.ignore();

    return 0;
}

if you are concerned about winks::String::to_string, i am simply converting a char ptr to my string object like so:

String String::to_string(const char *c_s)
{
    String temp = c_s;
    return temp;
}

however, the constructor i use in this method is private, so i am forcing to_string upon myself. i have had no problems with this so far. the reason why i made this is to avoid rewriting methods for different parameters ie: char * and String

the code for the private constructor:

String::String(const char *c_s)
{
    unsigned t_len = strlen(c_s);
    data_ = new char[t_len + 1];
    len_ = t_len;
    res_ = 0;
    strcpy(data_, c_s);
}

all help is greatly appreciated. if i have no supplied an efficient amount of information please notify me with what you want to know and i will gladly edit my post.

edit: the reason why i am not posting the full string.hpp and string.cpp is because it is rather large and i am not sure if you guys would like that.

  • 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-28T07:08:13+00:00Added an answer on May 28, 2026 at 7:08 am

    You have to make a decision whether you will always store your strings internally terminated with a 0. If you don’t store your strings with a terminating zero byte, your c_str function has to add one. Otherwise, it’s not returning a C-string.

    Your assign function doesn’t 0 terminate. So either it’s broken, or you didn’t intend to 0 terminate. If the former, fix it. If the latter, check your c_str function to make sure it puts a 0 on the end.

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

Sidebar

Related Questions

My main concern is with the boolean flag... is it safe to use it
This is on a 64 bit Windows Machine, also running Visual Studio 2010. A
Main concern is that it can happen at the moment of application crash. How
I know.. I know... Performance is not the main concern here, but just for
I have the following code: #include <iostream> #include <string> void main() { std::string str;
I have a program on my production server, that returned this error: My main
Main problem: how do i group elements by their Date, only if continuous and
main() { printf(Hello World.); } Why does no warning is produced in gcc compiler
main() { char a,b,c; a=6; b=-5; c = a+b ; printf (%d %d %d
main.c (with all the headers like stdio, stdlib, etc): int main() { int input;

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.