Click here to Skip to main content
15,918,109 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: I find an interesting and confusing code..have a look. Pin
JackPuppy13-Mar-09 8:55
JackPuppy13-Mar-09 8:55 
GeneralRe: I find an interesting and confusing code..have a look. Pin
David Crow13-Mar-09 8:58
David Crow13-Mar-09 8:58 
GeneralRe: I find an interesting and confusing code..have a look. Pin
JackPuppy13-Mar-09 9:03
JackPuppy13-Mar-09 9:03 
QuestionRe: I find an interesting and confusing code..have a look. Pin
David Crow13-Mar-09 9:07
David Crow13-Mar-09 9:07 
JokeRe: I find an interesting and confusing code..have a look. Pin
CPallini13-Mar-09 9:09
mveCPallini13-Mar-09 9:09 
GeneralRe: I find an interesting and confusing code..have a look. Pin
led mike13-Mar-09 9:03
led mike13-Mar-09 9:03 
GeneralRe: I find an interesting and confusing code..have a look. Pin
CPallini13-Mar-09 9:07
mveCPallini13-Mar-09 9:07 
GeneralRe: I find an interesting and confusing code..have a look. Pin
JackPuppy13-Mar-09 9:30
JackPuppy13-Mar-09 9:30 
OK,let me expain:

fun(){ 
int temp; 
throw temp; 
} 


void fun1(){ 
try{ 
int a; 
fun(); 
int* p=null; 
p=new int[40] 

} 
catch(int temp){ 
delete[] p; 

} 
}

main(){
fun1();
}


when main() call fun1(), a,p are push into stack without initialize.
fun()throw an exception so the try-catch block detect it.
and the program jump into catch{} and in the catch{} it delete[] p;
wait for a sec, p wasn't initialize yet so this is wrong;
however how can we determine whether p was initialize?
because when p was pushed into stack it contain some data and become a dangling pointer.
by convention, we will write catch{
if(p!=NULL)
delete[] p;

}

but it wasn't the case at this circumstance because p is not null at the very begining!
of cause you can write like this:


int* p=null;
fun();
p=new int[40];

so p is null before calling fun().
so It's OK now.

I find this interesting because I have to pay so much programing knowledge to explain this small mistake, the knowledge concerned are : stack, heap management, PE format, linker, loader, lib.
GeneralRe: I find an interesting and confusing code..have a look. Pin
bulg13-Mar-09 10:35
bulg13-Mar-09 10:35 
GeneralRe: I find an interesting and confusing code..have a look. Pin
JackPuppy13-Mar-09 10:38
JackPuppy13-Mar-09 10:38 
GeneralRe: I find an interesting and confusing code..have a look. Pin
bulg13-Mar-09 10:55
bulg13-Mar-09 10:55 
GeneralRe: I find an interesting and confusing code..have a look. Pin
Stuart Dootson13-Mar-09 13:57
professionalStuart Dootson13-Mar-09 13:57 
QuestionMAPI problem - PR_IPM_OUTBOX_ENTRYID doesn't exist?? Pin
tsunamisama13-Mar-09 8:04
tsunamisama13-Mar-09 8:04 
AnswerRe: MAPI problem - PR_IPM_OUTBOX_ENTRYID doesn't exist?? [modified] Pin
tha_specializt13-Jan-11 1:36
tha_specializt13-Jan-11 1:36 
GeneralRe: MAPI problem - PR_IPM_OUTBOX_ENTRYID doesn't exist?? Pin
sergiobuonanno19-Jul-11 3:44
sergiobuonanno19-Jul-11 3:44 
Questionthis pointer... Pin
siva45513-Mar-09 6:58
siva45513-Mar-09 6:58 
AnswerRe: this pointer... Pin
Maximilien13-Mar-09 7:34
Maximilien13-Mar-09 7:34 
GeneralRe: this pointer... Pin
CPallini13-Mar-09 7:53
mveCPallini13-Mar-09 7:53 
AnswerRe: this pointer... Pin
David Crow13-Mar-09 7:42
David Crow13-Mar-09 7:42 
AnswerRe: this pointer... Pin
CPallini13-Mar-09 7:49
mveCPallini13-Mar-09 7:49 
AnswerRe: this pointer... Pin
led mike13-Mar-09 9:00
led mike13-Mar-09 9:00 
GeneralRe: this pointer... Pin
Maximilien13-Mar-09 9:21
Maximilien13-Mar-09 9:21 
GeneralRe: this pointer... Pin
led mike13-Mar-09 12:05
led mike13-Mar-09 12:05 
GeneralRe: this pointer... Pin
Maximilien13-Mar-09 14:56
Maximilien13-Mar-09 14:56 
QuestionReg :: Customizing CDatetimeCtrl Pin
chandru.jeeva13-Mar-09 6:11
chandru.jeeva13-Mar-09 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.