What happens exactly during run time?

P

Pranav

class just{
public :
int x;
just(){ x=1234; }
just(int i){ x = i;}
~just(){ cout << " Here We Are\n" ;}
};

int main()
{
just j1;
just *jptr =new just(5432);

delete( jptr);
delete( &j1);

cout << "Test Out \n" ;

return 0;
}

After executing destructor for J1 program calls for an abort in Visual
Studio 6
 
I

Ian Collins

Pranav said:
class just{
public :
int x;
just(){ x=1234; }
just(int i){ x = i;}
~just(){ cout << " Here We Are\n" ;}
};

int main()
{
just j1;
just *jptr =new just(5432);

delete( jptr);
delete( &j1);

cout << "Test Out \n" ;

return 0;
}

After executing destructor for J1 program calls for an abort in Visual
Studio 6

Well what did you expect? j1 wasn't dynamically allocated.
 
P

Pranav

Ya I know that thing, But When I call 'delete(&j1)' the destructor for
'j1' is called after this there is window appears saying 'Abort/Ignore/
Retry'. Why exactly this behaviour occurs? Can any one explain what
happens during runtime?
 
I

Ian Collins

Pranav said:
Ya I know that thing,

What thing? Keep enough context for your reply to make sense.
But When I call 'delete(&j1)' the destructor for
'j1' is called after this there is window appears saying 'Abort/Ignore/
Retry'. Why exactly this behaviour occurs? Can any one explain what
happens during runtime?
What did you expect? You broke the rules by attempting to delete
something that wasn't dynamically allocated, so your runtime barfed.
 
T

Triple-DES

Ya I know that thing, But When I call 'delete(&j1)' the destructor for
'j1' is called after this there is window appears saying 'Abort/Ignore/
Retry'. Why exactly this behaviour occurs? Can any one explain what
happens during runtime?

You should not call delete on something that was not allocated using
new (Except the null pointer). jl is an automatic object, so it will
be destroyed automatically.

DP
 
H

HumbleWorker

Ya I know that thing, But When I call 'delete(&j1)' the destructor for
'j1' is called after this there is window appears saying 'Abort/Ignore/
Retry'. Why exactly this behaviour occurs? Can any one explain what
happens during runtime?

The sequence of runtime action for delete (&j1)
is:-

1. The operator delete calls the destructor for the object at the
address &j1. So your cout displays "Here We Are".

2. The second step that the operator delete has to do is free the
memory, but since, the memory in case of &j1 was NOT allocated with
new, a runtime assertion failure is reported.

HW
 
P

Pranav

The sequence of runtime action for delete (&j1)
is:-

1. The operator delete calls the destructor for the object at the
address &j1. So your cout displays "Here We Are".

2. The second step that the operator delete has to do is free the
memory, but since, the memory in case of &j1 was NOT allocated with
new, a runtime assertion failure is reported.

HW

Thank You HumbleWorker for complete explanation..,
Do anyone have any link/document to memory allocating function
implementation?(malloc,new,etc..)
 
T

tni

Pranav said:
Ya I know that thing, But When I call 'delete(&j1)' the destructor for
'j1' is called after this there is window appears saying 'Abort/Ignore/
Retry'. Why exactly this behaviour occurs? Can any one explain what
happens during runtime?

'new' will first call the destructor, then call 'free'. The destructor
will work, 'free' fails, since j1 wasn't allocated on the heap. In the
free call, there is probably an assertion that fails (if you are using
the debug version of the runtime lib) or a memory protection error.
 
R

Rolf Magnus

tni said:
'new' will first call the destructor, then call 'free'.

Not exactly. It will do something similar to 'free'. On some compilers, it
actually does call 'free', but that's not required by the standard.
 
T

tni

Rolf said:
Not exactly. It will do something similar to 'free'. On some compilers, it
actually does call 'free', but that's not required by the standard.

He said he was using Visual C++. It does use malloc/free in new/delete.
 
P

Pranav

He said he was using Visual C++. It does use malloc/free in new/delete.

Recently I read from a book that free() does not call destructor for
objects properly and also for array of user defined objects. Is it
true?
Also I came through, allocator allocate an extra word of memory
specifying the size of the array from which deallocating function gets
the size of array and this word of memory usually right before the
start address returned by the allocator.
 
I

Ian Collins

Pranav said:
Recently I read from a book that free() does not call destructor for
objects properly and also for array of user defined objects. Is it
true?

Yes, free() is just a C library function frees memory previously
allocated with one of the malloc() family of functions. It knows
nothing of C++ classes.

Operators new and delete may call malloc() and free() to allocate and
free memory, but that is only part of their jobs. The other part is
constructing and destructing the objects.

Assuming they do, operator new will call malloc to allocate memory and
then call the object's constructor. Operator delete will call the
object's destructor and then call free to release the memory.
 
P

Pranav

Yes, free() is just a C library function frees memory previously
allocated with one of the malloc() family of functions. It knows
nothing of C++ classes.

Operators new and delete may call malloc() and free() to allocate and
free memory, but that is only part of their jobs. The other part is
constructing and destructing the objects.

Assuming they do, operator new will call malloc to allocate memory and
then call the object's constructor. Operator delete will call the
object's destructor and then call free to release the memory.


Exactly.., Thank You All, for clarifying my doubts..,


Pranav

--There's a difference between knowing the path and walking it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top