Three questions about lifetime and scope.

From: Jason Heyes (jasonheyes_at_optusnet.com.au)
Date: 11/23/04


Date: Tue, 23 Nov 2004 21:50:22 +1100

Consider the function with this prototype:

void foo(Bar bar);

1. Can a program call foo and allow only one addressable Bar object to exist
at a time?

Now consider the following program:

class Bar
{
    int value;
public:
    Bar() : value(0) { }
    void change(int new_value) { value = new_value; }
};

void foo(Bar bar) { }

int main()
{
    Bar bar;
    bar.change(1);
    foo(bar);
    return 0;
}

2. Can this program be modified without change to foo and Bar so that:

i) main calls foo with the same value of bar, and
ii) only one addressable Bar object exists at a time.

3. What is the practical significance, if any, of the previous two questions
in relation to data sharing and copy-on-write?

Please support your answers with examples. Any help is appreciated.



Relevant Pages

  • Re: linking C++ functions in a C program
    ... contains one function named foo() which is compiled in C, ... Therefore, no matter how you classify bar(), this program ... void foo; ... guess the compiler can generate two references to foo, one adorned, ...
    (comp.lang.c)
  • Circular dependency - I think..
    ... The header file for FOO is composed of BAR, ... int GetFbk(); ... void ComputeTorquerCmd(); ...
    (comp.lang.cpp)
  • Re: is assignment atomic/thread safe?
    ... void foo() ... with one thread running foo() while another runs bar(). ... and thus no guarantees are made about _when_ any given piece of code will execute. ...
    (comp.lang.java.programmer)
  • Re: Library bug or my fault?
    ... compiled using ARM/Linux cross-compiler and run on an ARM9 target. ... void memcpy ... 29 void cp(const Foo *foo) ... 31 Bar bar; ...
    (comp.lang.c)
  • Re: Insert with response
    ... FooBar, there's no way and no need to put them in synch. ... column in the foo table to 250 calumns in the bar table. ... set statistics time off ...
    (microsoft.public.sqlserver.programming)