In C ++ pointer is a variable that stores the address value, rather than the value itself, we take a look at C ++ pointer with the basic principles of:
pointers declaratively:
arithmetic operators on both sides of the space is optional. Conventionally C programmers use the format:
int ptr;
This emphasizes ptr is a value of type int, C ++ programmers and many use this format:
int ptr;
it is emphasized that: int is a type pointing int pointer. Add a space where there is no difference to the compiler.
In C ++, int is a complex type, a pointer to an int
pointer precautions:
C ++ pointer when creating the computer memory allocated to store the address, but not used to store data allocation pointer points to the RAM. Provide space for data is a separate step
C ++ is used to allocate new memory
programmer to tell new, need to allocate memory for the type of data which: a new look for the correct length of a block of memory, and returns the address of the memory block. It is the programmer's responsibility to assign the address of a pointer
example:
int * PTR = new new int;
new new int tells the program, an appropriate memory storage int. new operator is determined according to the number of data bytes of memory required type, then it will find such a memory, and returns its address
to release memory:
Use only delete to free memory allocated using new. For a null pointer is safe to use the delete
example:
int * ptr;
...;
delete ptr;
this will release the memory pointed to by ptr, but ptr pointer itself does not remove, and then can be re-ptr points to another newly allocated memory block. Be sure to use the new pairing and delete; otherwise it will be a memory leak, it said, were allocated memory can no longer be used. If the memory leak is serious, the program will continue to look for more memory due to terminate.
In general, do not create two pointers to the same memory block, as this will increase the likelihood of mistakenly deleted the same memory block twice
examples of the use of the file pointer:
unsigned char pImageBand1 = NULL;
pImageBand1 new new = ( :: the nothrow STD) unsigned char [width1 height1 * bandCount1]; // determine the memory size
GDALDataset *WriteDataSet = poDriver->Create(“D:\Datas\Landsat\band_merge2.tif”, width1, height1, 3, GDT_Byte, papszOptions);
if (WriteDataSet->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, width1, height1, pImageBand1, width1, height1, GDT_Byte, 0, 0) == CE_Failure)
{
return false;
}
图片来源:《C++ Primer Plus(第6版)》