Jump to content

Recommended Posts

Hi!

why do the compiler accept the object "texto" after delete it?

should issue a warning or erro message to attempt access a memory that not exist a object ?

void main()

{

 

//texto.portada_lcd();

while(1){

class Ctexto *texto = new class Ctexto;

port.Toggle();

pinB0=0;

texto.texto2();

delete texto;//destroy object

texto=NULL;//

texto.texto2();// forced erro. work fine don't issues erro code or warnings

}

}

 

thanks

 

Douglas

Link to post
Share on other sites

New and delete are run time operations. At compile time when compiler generates code it does not know when objects are created or deleted. In the code you posted technically it's possible to analyze it at compile time and know that object 'texto' was used after deletion but this potencially can bring more bad than good as users will expect compiler to check all cases and in many it's just not possible to analyze the code (due to variable re-assignment, calls, loops etc.)

 

Regards,

Pavel

Link to post
Share on other sites
New and delete are run time operations. At compile time when compiler generates code it does not know when objects are created or deleted. In the code you posted technically it's possible to analyze it at compile time and know that object 'texto' was used after deletion but this potencially can bring more bad than good as users will expect compiler to check all cases and in many it's just not possible to analyze the code (due to variable re-assignment, calls, loops etc.)

 

Regards,

Pavel

 

thanks for reply!

I understand now, very thanks Mr. Pavel!

but I have another question about this, when in debugging this code works like nothing occurs with f11 key, treats the function like normal of a object declared,going within the functions members and so on... but it is deleted. is correct this behavior at debug time?

I'm sending the code for analizes.

thanks.

Link to post
Share on other sites
New and delete are run time operations. At compile time when compiler generates code it does not know when objects are created or deleted. In the code you posted technically it's possible to analyze it at compile time and know that object 'texto' was used after deletion but this potencially can bring more bad than good as users will expect compiler to check all cases and in many it's just not possible to analyze the code (due to variable re-assignment, calls, loops etc.)

 

Regards,

Pavel

 

thanks for reply!

I understand now, very thanks Mr. Pavel!

but I have another question about this, when in debugging this code works like nothing occurs with f11 key, treats the function like normal of a object declared,going within the functions members and so on... but it is deleted. is correct this behavior at debug time?

I'm sending the code for analizes.

thanks.

 

Speaking formally acces of a deleted object leads to unpredicted behaviour. This means that code may work but it also may crash. Going deeper in your particular case when an object is deleted compiler just cleares flag used by allocation code to check if a piece of memory is free or occupied. This means that all object data is not touched at the deletion point and if accessed it appeares that everything object is still there and works just fine. However this will work only till another object is placed at this location and overwrites old data left from previous object. As a programmer when you delete an object you should always assume that this object does not exist any longer and do not access any of its data.

 

Regards,

Pavel

Link to post
Share on other sites
New and delete are run time operations. At compile time when compiler generates code it does not know when objects are created or deleted. In the code you posted technically it's possible to analyze it at compile time and know that object 'texto' was used after deletion but this potencially can bring more bad than good as users will expect compiler to check all cases and in many it's just not possible to analyze the code (due to variable re-assignment, calls, loops etc.)

 

Regards,

Pavel

 

thanks for reply!

I understand now, very thanks Mr. Pavel!

but I have another question about this, when in debugging this code works like nothing occurs with f11 key, treats the function like normal of a object declared,going within the functions members and so on... but it is deleted. is correct this behavior at debug time?

I'm sending the code for analizes.

thanks.

 

Speaking formally acces of a deleted object leads to unpredicted behaviour. This means that code may work but it also may crash. Going deeper in your particular case when an object is deleted compiler just cleares flag used by allocation code to check if a piece of memory is free or occupied. This means that all object data is not touched at the deletion point and if accessed it appeares that everything object is still there and works just fine. However this will work only till another object is placed at this location and overwrites old data left from previous object. As a programmer when you delete an object you should always assume that this object does not exist any longer and do not access any of its data.

 

Regards,

Pavel

Thank you Mr. Pavel!

Douglas

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...