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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:20:57+00:00 2026-05-21T03:20:57+00:00

I have char* which is of fixed (known) width but is not null terminated.

  • 0

I have char* which is of fixed (known) width but is not null terminated.

I want to pass it into LOG4CPLUS_ERROR("Bad string " << char_pointer); but as its not null terminated it will print it all.

Any suggestions of some light weight way of getting "(char[length])*char_pointer" without performing a copy?

  • 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-21T03:20:57+00:00Added an answer on May 21, 2026 at 3:20 am

    Real iostreams

    When you’re writing to a real iostream, then you can just use ostream.write() which takes a char* and a size for how many bytes to write — no null termination necessary. (In fact, any null characters in the string would be written to the ostream, and would not be used to determine the size.)

    Logging libraries

    In some logging libraries, the stream that you write to is not a real iostream. This is the case in Log4CPP.

    However, in Log4CPlus which is what it appears matt is using, the object that you’re writing to is a std::basic_ostringstream<tchar> (see loggingmacros.h and streams.h for the definition, since none of this is obvious from the documentation). There’s just one problem: in the macro LOG4CPLUS_ERROR, the first << is already built into the macro, so he won’t be able to call LOG4CPLUS_ERROR(.write(char_pointer,length)) or anything like that. Unfortunately, I don’t see an easy way around this without deconstructing the LOG4CPLUS_ERROR error macro and getting into the internals of Log4CPlus

    Solution

    I’m not sure why you’re trying to avoid copying the string at this point, since you can see that there’s a lot of copying going on inside the logging library. Any attempt to avoid that extra string copy is probably unwarranted optimization.

    I’m going to assume that it’s an issue of code cleanliness, and maybe an issue of making sure the copy happens inside the LOG4CPLUS_ERROR macro, as opposed to outside it. In that case, just use:

    LOG4CPLUS_ERROR("Bad string " << std::string(char_pointer, length));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an char array b[20] which i want to write into a file
I have a char pointer which would be used to store a string. It
I have a char array in Python, which is read from a file e.g.
I have a function m(int i, char c) which takes and returns a char
I have a class with parse(int argc, char* argv[]) function which I have to
We have a table T which contains a several char(8) columns (implicitly) which under
I have a table which has a column called Direct of type char(1). It's
I have a simple C function which I declare as: int strLen(const char* str_)
I have a bit-mask of N chars in size, which is statically known (i.e.
I've got a requirement to take some XML and transform it into a fixed-width

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.