Thursday, August 27, 2009

pointers and exceptions

what problem will happen if an exception occurs in the function F1() in the following code. how to prevent the problem?

environment: c++

function2(){
Spring* sp1=new Spring("hello");
F1(sp1);
delete sp1;
}

1 comment:

  1. If an exception occurs in F1, pointer sp1 wont be deleted and cause a memory leak.

    To prevent it, use smart pointer.

    ReplyDelete