#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;
}
Object oriented programming and structured programming.
pop push c++ 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.
turbo c
Nothing.
we are using c plus plus programming for developing object oriented programing software.
Turbo C is a software where C or C++ programming environment resides in.But C++ is itself a programming language.
No, but it does support modular programming through namespaces.
Object oriented programming and structured programming.
Computer programming.
Programming.
C++ is an object oriented programming language
www.cplusplus.com
Programming language.
pop push c++ 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.
turbo c is a compiler and c++ is a programming language.