Variable/pointer scope (rookie Qs)

This is a discussion on Variable/pointer scope (rookie Qs) within the C Programming forums, part of the General Programming Boards category; I have 3 pieces of code below. A simple one really - just need to know which ones are safe. ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    Variable/pointer scope (rookie Qs)

    I have 3 pieces of code below. A simple one really - just need to know which ones are safe. I think A and maybe C is safe. However, B is dangerous from my experience, but I'm not exactly sure why as the memory inside has been allocated to the heap, so it should last 'forever', and not just be local to the function.

    Code:
    // A: I think this is safe:
    #include <iostream>
    struct prvs {	int quantity;	};
    prvs* pr() {
    	prvs* p = new prvs;	p->quantity=3;
    	return p;
    }
    int main(int argc, char**argv)
    {
    	prvs* prv = pr();
    }
    
    
    
    // B: I think this is dangerous:
    #include <iostream>
    struct prvs {	int quantity;	};
    prvs pr() {
    	prvs* p = new prvs;	p->quantity=3;
    	return *p;
    }
    int main(int argc, char**argv)
    {
    	prvs* prv = &pr();	// Will this pointer's contents always survive?
    }
    
    
    
    // C: I'm not so sure about this one
    #include <iostream>
    struct prvs {	int quantity;	};
    prvs pr() {
    	prvs* p = new prvs;	p->quantity=3;
    	return *p;
    }
    int main(int argc, char**argv)
    {
    	prvs prv = pr();
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by twinbee View Post
    I have 3 pieces of code below. A simple one really - just need to know which ones are safe. I think A and maybe C is safe. However, B is dangerous from my experience, but I'm not exactly sure why as the memory inside has been allocated to the heap, so it should last 'forever', and not just be local to the function.

    Code:
    // A: I think this is safe:
    #include <iostream>
    struct prvs {    int quantity;    };
    prvs* pr() {
        prvs* p = new prvs;    p->quantity=3;
        return p;
    }
    int main(int argc, char**argv)
    {
        prvs* prv = pr();
    }
    
    
    
    // B: I think this is dangerous:
    #include <iostream>
    struct prvs {    int quantity;    };
    prvs pr() {
        prvs* p = new prvs;    p->quantity=3;
        return *p;
    }
    int main(int argc, char**argv)
    {
        prvs* prv = &pr();    // Will this pointer's contents always survive?
    }
    
    
    
    // C: I'm not so sure about this one
    #include <iostream>
    struct prvs {    int quantity;    };
    prvs pr() {
        prvs* p = new prvs;    p->quantity=3;
        return *p;
    }
    int main(int argc, char**argv)
    {
        prvs prv = pr();
    }
    Just FYI, this should be posted to the C++ forum.

    Anyway, 'A' is correct, *but* you fail to delete the memory afterwords. 'B' creates a memory-leak, and attempts to obtain the address of an anonymous variable, which is a big no-no (you can form an "immutable temporary" by assigning the return value to a const reference, but that's more advanced stuff). 'C' also has a memory leak...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Thanks. Can you confirm that B would crash or produce strange output (even without any memory leak) if the program was longer? I still don't understand why it's bad though, since the variable that's being returned was allocated on the heap, not on the stack.

    As for code part C, I take it that there would still be a memory leak inside the funtion, even if I used delete at the end?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    B isn't right at all on the assignment. You can't get the address of something you return by value. It's like saying:
    Code:
    int foo() { return 5; } ... int *bar = &foo();
    It shouldn't compile. Invalid lvalue.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C name space and lexical scope
    By password636 in forum C Programming
    Replies: 1
    Last Post: 09-22-2009, 10:50 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21