answersLogoWhite

0

C plus plus programming for deletion?

Updated: 10/25/2022
User Avatar

Wiki User

10y ago

Best Answer

#include<iostream>

int main()

{

// Allocate some memory for an integer with value 42.

int* p=new int(42);

//

// do some stuff with integer...

//

// Release the memory.

delete(p);

// Nullify the pointer.

p=NULL;

}

Note that nullifying the pointer is not necessary when the pointer would fall from scope anyway (as in the above example), but it's good practice nonetheless. Consider the following:

#include<iostream>

int main()

{

int* p=new int;

std::cout<<"0x"<<std::hex<<p<<" = "<<std::dec<<*p<<std::endl;

delete(p);

std::cout<<"0x"<<std::hex<<p<<" = "<<std::dec<<*p<<std::endl;

}

When you run the above example, you will note that even though we didn't initialise or alter the integer, it still has a value (whatever happens to reside at the memory location at the time). So although the program runs, it may or may not produce the desired behaviour depending on the value of *p. In this case we don't actually care what the value is, so the program appears to run normally. But the program has undefined behaviour because the value of *p is not under our control once we delete the pointer.

If we nullify the pointer immediately after deleting it, the program will crash when we try to print the value of *p (an access violation will occur). While that is clearly undesirable, at least we immediately know we have a problem, and can alter the code accordingly:

int main()

{

int* p=new int;

std::cout<<"0x"<<std::hex<<p<<" = "<<std::dec<<*p<<std::endl;

delete(p);

p=NULL;

// p is still valid (with the value 0x00000000), but *p is not!

std::cout<<"0x"<<std::hex<<p<<std::endl;

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus programming for deletion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why study c plus plus programming?

we are using c plus plus programming for developing object oriented programing software.


Differentiate C plus plus and Turbo C?

Turbo C is a software where C or C++ programming environment resides in.But C++ is itself a programming language.


Is c plus plus a modular programming?

No, but it does support modular programming through namespaces.


What are the two major types of programming languages in c plus plus?

Object oriented programming and structured programming.


What is the purpose of C plus plus?

Computer programming.


What is c plus plus primarily used for?

Programming.


What is the significance of c plus plus?

C++ is an object oriented programming language


What is the best website for C plus plus programming?

www.cplusplus.com


What is the use of c plus plus in banks?

Programming language.


Algorithm of push and pop in c plus plus?

pop push c++ programming


Is NET programming is update from C plus plus programming?

No. .NET programming is Microsoft-specific, similar to Java in some respects, but it is non-portable. C++ is a general purpose and cross-platform programming language.


Is there any difference between turbo c and c plus plus?

turbo c is a compiler and c++ is a programming language.