-3

for swap function we have two choice: ref-style:

void swap (int &a, int &b)
{
   int temp;

   temp = b;
   b   = a;
   a   = temp;   
}

and pointer-style:

void swap (int *a, int *b)
{
   int temp;

   temp = *b;
   *b   = *a;
   *a   = temp;   
}

ref-style absolutely legal but pointer-style have some issue, when we try to use this function variables pass by value not by reference-even they are pointers- in fact we try to use memory of a local variable outside its function, and may by in some day in some machine we have undefined behavior ,also the code works for examples: In this code:

main()
{
//
{
    int i=12;
    int *j=&i;
}
//in this area, there is not variables i and j, but phisically threre is 
// unsafe-relationship between this address: &i and what point to (12) , 
//any more logic according to this assumtion may be work 
//but not safe -in the scene of undefined behavior- 
dt128
  • 81
  • 9
  • 3
    "we try to use memory of a local variable outside its function" Which local variable? – songyuanyao Jan 21 '17 at 07:18
  • 1
    It's not clear what you are saying or what things you are talking about or what you are asking. Please write more, shorter, clearer sentences. Maybe also give an example of "use". – philipxy Jan 21 '17 at 07:22
  • 2
    In both cases, the caller is responsible for passing valid arguments i.e. ensuring that `a` and `b` refer or point to something with lifetime that extends after the functions return. As long as it does, the behaviour of both forms of your function are well defined. – Peter Jan 21 '17 at 09:17
  • @philipxy i wrote more – dt128 Jan 22 '17 at 15:39
  • Still unintelligible. – philipxy Jan 22 '17 at 20:06

1 Answers1

2

I don't know where you got the sentence about outside a function from. You shouldn't trust them, since that sentence is wrong.

It should be "accessing a local variable or a parameter after the function has returned invokes undefined behavior". (These are my own words.)

To get the official wording, look in the C++ standard. The keywords to look for are lifetime, storage duration and undefined behavior.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • 2
    Note that this if for C++ not C. Might be worth pointing to the most recent C++ standard instead. – tambre Jan 21 '17 at 07:33
  • http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – dt128 Jan 21 '17 at 07:37
  • As a general rule: don't trust wording in the questions, unless they are tagged [language-lawyer]. The wording in the answers is more trustworthy. – Roland Illig Jan 21 '17 at 07:54
  • It's not wrong, just ambiguous, and one possible meaning (presumably the intended meaning) is correct. Given `void f() { int x=1, y=2; swap(x, y); }`, you might take the POV that the execution of `swap` is inside `f`, and therefore the local variables `x` and `y` are accessed inside their function. –  Jan 21 '17 at 08:16
  • 1
    @hvd Your interpretation is blurring the concepts. There is the _scope_ of the function and there is the _execution_ of the function. The words "inside the function" are so ambiguous that I consider them erroneous. Mixing the concepts will lead to big problems when trying to understand what is happening. – Roland Illig Jan 21 '17 at 08:23
  • @RolandIllig But by your interpretation, it's saying `int *f(int *p) { int i = 1; if (p) *p = 2; return &i; }` is valid if used as `f(f(0))`, just because the access to `*p` (`i`) happens inside `f`? To me, *that* is such obvious nonsense that it should be painfully obvious that the OP who wrote that question meant something else, and if you take out that interpretation, there's not much ambiguity left. –  Jan 21 '17 at 08:48