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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:05:40+00:00 2026-05-12T21:05:40+00:00

I am working in C++ with two large pieces of code, one done in

  • 0

I am working in C++ with two large pieces of code, one done in “C style” and one in “C++ style”.

The C-type code has functions that return const char* and the C++ code has in numerous places things like

const char* somecstylefunction();
...
std::string imacppstring = somecstylefunction();

where it is constructing the string from a const char* returned by the C style code.

This worked until the C style code changed and started returning NULL pointers sometimes. This of course causes seg faults.

There is a lot of code around and so I would like to most parsimonious way fix to this problem. The expected behavior is that imacppstring would be the empty string in this case. Is there a nice, slick solution to this?

Update

The const char* returned by these functions are always pointers to static strings. They were used mostly to pass informative messages (destined for logging most likely) about any unexpected behavior in the function. It was decided that having these return NULL on “nothing to report” was nice, because then you could use the return value as a conditional, i.e.

if (somecstylefunction()) do_something;

whereas before the functions returned the static string “”;

Whether this was a good idea, I’m not going to touch this code and it’s not up to me anyway.

What I wanted to avoid was tracking down every string initialization to add a wrapper function.

  • 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-12T21:05:40+00:00Added an answer on May 12, 2026 at 9:05 pm

    Probably the best thing to do is to fix the C library functions to their pre-breaking change behavior. but maybe you don’t have control over that library.

    The second thing to consider is to change all the instances where you’re depending on the C lib functions returning an empty string to use a wrapper function that’ll ‘fix up’ the NULL pointers:

    const char* nullToEmpty( char const* s)
    {
        return (s ? s : "");
    }
    

    So now

    std::string imacppstring = somecstylefunction();
    

    might look like:

    std::string imacppstring( nullToEmpty( somecstylefunction());
    

    If that’s unacceptable (it might be a lot of busy work, but it should be a one-time mechanical change), you could implement a ‘parallel’ library that has the same names as the C lib you’re currently using, with those functions simply calling the original C lib functions and fixing the NULL pointers as appropriate. You’d need to play some tricky games with headers, the linker, and/or C++ namespaces to get this to work, and this has a huge potential for causing confusion down the road, so I’d think hard before going down that road.

    But something like the following might get you started:

    // .h file for a C++ wrapper for the C Lib
    namespace clib_fixer {
        const char* somecstylefunction();
    }
    
    
    // .cpp file for a C++ wrapper for the C Lib
    namespace clib_fixer {
        const char* somecstylefunction() {
            const char* p = ::somecstylefunction();
    
            return (p ? p : "");
        }
    }
    

    Now you just have to add that header to the .cpp files that are currently calling calling the C lib functions (and probably remove the header for the C lib) and add a

    using namespace clib_fixer;
    

    to the .cpp file using those functions.

    That might not be too bad. Maybe.

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

Sidebar

Related Questions

I'm working on a C++ project with a large number of classes (150+), each
The Problem I'm working on a problem that involves sharding. As part of the
For software development one often needs images. But when I start working on an
My company has a large application we are finishing up. It utilizes WCF as
I'm currently working on what I would call integration tests. I want to verify
What solutions are there for working on a LaTeX document on both Windows and
I work for a firm that provides certain types of financial consulting services in
I am working on upgrading our site to work in different languages. My plan
I have recently just started working with Lucene (specifically, Lucene.Net) and have successfully created

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.