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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:42:28+00:00 2026-05-27T04:42:28+00:00

My colleague recently compiled our program in Windows, and discovered a bug of the

  • 0

My colleague recently compiled our program in Windows, and discovered a bug of the sort:

  std::string a = "hello "; 
  std::string b = "world"; 
  const char *p = (a+b).c_str();
  printf("%s\n", p);

which for some reason did not crash in our Linux executables.

None of our compilers give any kind of warning, so we are now worried that this error might exist in the code.

Although we can grep for c_str() occurrences and do a visual inspection, there is a possibility that one might have also done the following:

struct I {
     int num;
     I()   { num=0; }
};

struct X { 
     I *m;
     X()  { m = new I; }
     ~X() { delete m; }
     I  get() { return *m; }   // version 1, or
     I& get() { return *m; }   // version 2
};

and accessed it like:

  I& a = X().get();         // will get a reference to a temporary, or a valid copy?
  cout << a.num;

instead of :

 cout << X().get().num;   

which is safe (isn’t it?)

Question: Is there a way I can catch such errors (perhaps using the compiler, or even an assertion) ?
I need to be sure that if author of struct X changes get() between version 1 and 2 that the program will warn for the error

  • 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-27T04:42:28+00:00Added an answer on May 27, 2026 at 4:42 am

    Check out this question’s solution, I think it does something similar to what you’re looking for:

    C++ catching dangling reference

    There are runtime based solutions which instrument the code to check
    invalid pointer accesses. I’ve only used mudflap so far (which is
    integrated in GCC since version 4.0). mudflap tries to track each
    pointer (and reference) in the code and checks each access if the
    pointer/reference actually points to an alive object of its base type.
    Here is an example: {…}

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

Sidebar

Related Questions

We recently added auditing to our database. A colleague implemented it using triggers and
Recently, I work on a video player program on Windows for a CCTV program.
Recently a colleague at work told me not to use string.Empty when setting a
I've recently inherited our Silverlight application from a departed colleague, along with a long
I recently had a discussion with a colleague about serialization of byte data over
A colleague has a batch script program which needs to to run on a
At my company, we recently switched from VC9 to VC10. We migrated our projects
I was recently advocating to a colleague that we replace some C# code that
A colleague of mine recently stated it is fine for multiple read write threads
I recently had a debate with a colleague who is not a fan of

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.