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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:33:48+00:00 2026-05-15T17:33:48+00:00

Suppose the following piece of code struct S { S(int & value): value_(value) {}

  • 0

Suppose the following piece of code

struct S {
    S(int & value): value_(value) {}
    int & value_;
};

S function() {
    int value = 0;
    return S(value);   // implicitly returning reference to local value
}

compiler does not produce warning (-Wall), this error can be hard to catch.

What tools are out there to help catch such problems

  • 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-15T17:33:49+00:00Added an answer on May 15, 2026 at 5:33 pm

    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:

    #include <stdio.h>
    struct S {
        S(int & value): value_(value) {}
        int & value_;
    };
    
    S function() {
        int value = 0;
        return S(value);   // implicitly returning reference to local value
    }
    int main()
    {
        S x=function();
        printf("%s\n",x.value_); //<-oh noes!
    }
    

    Compile this with mudflap enabled:

    g++ -fmudflap s.cc -lmudflap
    

    and running gives:

    $ ./a.out
    *******
    mudflap violation 1 (check/read): time=1279282951.939061 ptr=0x7fff141aeb8c size=4
    pc=0x7f53f4047391 location=`s.cc:14:24 (main)'
          /opt/gcc-4.5.0/lib64/libmudflap.so.0(__mf_check+0x41) [0x7f53f4047391]
          ./a.out(main+0x7f) [0x400c06]
          /lib64/libc.so.6(__libc_start_main+0xfd) [0x7f53f358aa7d]
    Nearby object 1: checked region begins 332B before and ends 329B before
    mudflap object 0x703430: name=`argv[]'
    bounds=[0x7fff141aecd8,0x7fff141aece7] size=16 area=static check=0r/0w liveness=0
    alloc time=1279282951.939012 pc=0x7f53f4046791
    Nearby object 2: checked region begins 348B before and ends 345B before
    mudflap object 0x708530: name=`environ[]'
    bounds=[0x7fff141aece8,0x7fff141af03f] size=856 area=static check=0r/0w liveness=0
    alloc time=1279282951.939049 pc=0x7f53f4046791
    Nearby object 3: checked region begins 0B into and ends 3B into
    mudflap dead object 0x7089e0: name=`s.cc:8:9 (function) int value'
    bounds=[0x7fff141aeb8c,0x7fff141aeb8f] size=4 area=stack check=0r/0w liveness=0
    alloc time=1279282951.939053 pc=0x7f53f4046791
    dealloc time=1279282951.939059 pc=0x7f53f4046346
    number of nearby objects: 3
    Segmentation fault
    

    A couple of points to consider:

    1. mudflap can be fine tuned in what exactly it should check and do. read http://gcc.gnu.org/wiki/Mudflap_Pointer_Debugging for details.
    2. The default behaviour is to raise a SIGSEGV on a violation, this means you can find the violation in your debugger.
    3. mudflap can be a bitch, in particular when your are interacting with libraries that are not compiled with mudflap support.
    4. It wont’t bark on the place where the dangling reference is created (return S(value)), only when the reference is dereferenced. If you need this, then you’ll need a static analysis tool.

    P.S. one thing to consider was, to add a NON-PORTABLE check to the copy constructor of S(), which asserts that value_ is not bound to an integer with a shorter life span (for example, if *this is located on an “older” slot of the stack that the integer it is bound to). This is higly-machine specific and possibly tricky to get right of course, but should be OK as long it’s only for debugging.

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

Sidebar

Ask A Question

Stats

  • Questions 445k
  • Answers 445k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should check out http://code.msdn.microsoft.com/WindowsAPICodePack . It has support for… May 15, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer figured: it's entirely possible, the call to ZipInputStream.getNextEntry() positions the… May 15, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer Have you tried stating the Source or ElementName property when… May 15, 2026 at 7:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.