consider the following code

afunction(int *x)
{
x=new int;
*x=12;
}
int main()
{
int v=10;
afunction(&v);
cout<<v;
}

i expected 12 as the output but its wrong and correct one is 10 . But i don't know how this is done internally ?
i was wondering if somebody could explain about it ?

thanks.

Recommended Answers

All 5 Replies

First you create an int, named v, and give it the value of 10.

Then, you create an unnamed pointer to v, like this: &v. You pass that unnamed pointer to the function named afunction, which gets a copy of that unnamed pointer.

Inside afunction, you give that copy of an unnamed pointer a name. You call it x. You then make that pointer (named x) point at a completely new int you created. It was pointing at v, and now it points to a completely new int you created. This completely new int has some random value when you create it, and then you use the pointer named x to set the value of this completely new int to 12.

Then, your function finishes. What happened to the copy of the pointer you passed in? It is destroyed. That copy of a pointer is gone forever.

What happened to the int v? Nothing. Nothing changed the int v, so its value is just what it was before; 10.

a lot thanks for your explanation . I really appreciate your answer.

earlier x was pointing to the address where v was stored but now i realize that this statement (x=new int;) will make x pointing to another location . therefore the statement(*x=12) will not write at the location where v is stored because x is now no longer pointing to v[because of this(x=new int)].

once again thanks.

I also believe x will remain in memory (a leak) until your program ends.

I also believe x will remain in memory (a leak)

how x will remain in memeory ? does it not removed automatically when function return back to main ?

Somebody here could explain about memory leaks . i'm not familiar to that.

Memory leaks in C++ are about balance and understanding scope [a bit].

First of the fundermentals: for every allocation there must be an equal deallocation.

That is about it.

Consider your example:

void 
afunction(int *x)
{
  // Memory allocation BUT no deallocation
  x=new int;
  *x=12;
}

int main()
{
  // No explicit memory allocation or deallocation in this function!
  int v=10;
  afunction(&v);
  cout<<v;
}

Here you can see one new but no delete. Thus you must have a memory leak.

This is insignificant in this programs, (one extra int) and when the program finishes the memory is returned to the system. BUT imagen that this function is called thousands and thousands of times ... then we have a problem.

Further on this subject : consider the following broken code:

void func(const int N)
{
   int* Array = new int[N];
   // ... do stuff with Array

   delete Array; 
}

Note it doesn't have the correct deletion operator, it should have had delete [] Array, as it was allocated as a block.

These examples are relatively simple, but a major cause of confusion it seems [if the code reviews I go to are anything to go on] are scope rule: Consider this code:

   void func(const int N)
    {
       int Array[100];
       if (N<100)
         {
            // ... do stuff with Array
         }
       delete Array; 
       return;
    }

Now we see a delete without a new. The array was not initialed with a new. So no delete! The array will be dropped from memory when the funcntion goes out of scope, i.e when the program exists the { }, then contain the allocation.

One final note: Consider this [which is a common memory leak problem]

   void func(const int N)
    {
       int* Array=new int[N]
       if (N==7)
         {
            // special case ... do some special stuff
            return;
         }
        // ... do normal stuff with Array
       delete [] Array; 
       return;
    }

Look what happens here, the early return is means that there is a memory leak in the special case but not the normal case. That normally makes it (a) a testing nightmare, (b) one of those bug that occur in rare difficult to reproduce circumstances.

Can I note there are a large number of classes available to help overcome some of these problems, including stuff like shared_ptr, auto_ptr etc. But you really have to be comfortable with basic pointers to use them well.

commented: explained very well +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.