dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Returning a reference to (--the contents of--) a local variable

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,497

    Returning a reference to (--the contents of--) a local variable

    I know it's bad practice to return a reference to a local variable - but what about returning a reference to the contents of a local variable? I've just come across this code in a program I'm working on:-

    Code:
    BufferSet&
    ProcessThread::get_noinplace_buffers ()
    {
    	ThreadBuffers* tb = _private_thread_buffers.get(); // 'private_thread_buffers' is a member variable
    	assert (tb);
    
    	BufferSet* sb = tb->noinplace_buffers;
    	assert (sb);
    
    	return *sb;
    }
    The code appears to be working - but is that just a happy accident ??
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,515

    Re: Returning a reference to (--the contents of--) a local variable

    sb is a pointer. The contents pointed to by sb are returned as a ref meaning that the value in memory pointed to by sb can be changed outside of the function. Refs are implemented using pointers. So as long as the memory holding the returned value remains valid outside of the function, then the returned ref value is OK to be used.

    The point about returning refs from a function is that the memory used must still be valid after the return. If it is, then fine. If it's not then returning a ref is bad.

    In this case it looks like the returned ref refers to some previously allocated buffer memory. so as long as that memory is not deleted then it should be fine.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  3. #3
    Join Date
    Feb 2017
    Posts
    375

    Re: Returning a reference to (--the contents of--) a local variable

    Quote Originally Posted by John E View Post
    bad practice
    Just a comment.

    If ProcessThread is a class and the get_noinplace_buffers method is public then there's a break of encapsulation because the internal implementation is exposed. This is considered bad practice, at least in the object oriented style of programming.

    Instead the get_noinplace_buffers method should return the number of buffers and there could, if applicable, also be a set_noinplace_buffers method to set the number of buffers.
    Last edited by wolle; December 4th, 2018 at 03:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)