|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe topic of this article is pointers. I describe below some problems, bugs and technique solutions that correspond to using pointers. This article would be useful for beginners and programmers who are using other programming languages and are starting to study C and C++ now. Bad pointersThere are a lot of programmers who think that pointers are bad constructions as the "go to" operator. Pointers are so bad that you cannot find them in Basic, Java, C#. But really it is not true. All applications that are executed under Windows or Unix use pointers! A lot of API functions receive pointers and return pointers, so if you use API, you use pointers. For example, if I declare in a Visual Basic program, such an API function interface: Private Declare Sub SetLocalTime Lib "kernel32" (localTime As SYSTEMTIME)
Basic instruction Why do not many languages support pointers as a language construction? - Because pointers are dangerous. It is easy to make an error that a compiler will not find. More possible is to make an error that you will not find while debugging the program. If your program is alive, it is only because it has not been started by the right user. Good users will always find a way to crash your program. Here is an example of a very common pointers-related error: char * CloneName(char *pSomeName) { char *pTmpName = new char[strlen(pSomeName)]; // Error! strcpy(pTmpName, pSomeName); return pTmpName; } This function must clone a string. In this example, one byte of memory will be destroyed behind the copy of the string. The right allocation instruction is Good pointersA pointer is a language construction of C++. Historically, this language continues the C language tradition that was created as a good alternative to assembler language. Pointers allow a programmer manage memory allocation very efficiently. If you work accurately, everything will be OK. What is a pointer?A pointer is a special variable that is used for storing some memory address. So Here is a title of "the main" Windows function. It sends a message to a window. LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to send
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)"Some Text"); "Some Text" is a static text constant. It has an address in a memory and type A pointer is also an array. Array type is not actually the same as a pointer type. But it is very close to a pointer and easy to convert to pointer. All dynamic arrays are pointers. Pointers and arraysIt is an article. I do not want to rewrite some C++ book. So, I will demonstrate here only one interesting example. It shows the difference between using 2D automatic array and 2D dynamic array. #include <iostream.h> void OutAutoElm(int nRow, int nCol, int *pAutoArray, int nSize); void OutDynElm(int nRow, int nCol, int **pDynArray); int main(int argc, char* argv[]) { const int nSize = 5; // Size of matrix // Auto Array. Allocate memory in the stack. int AutoArray[nSize][nSize]; int **DynArray; // Dynamic Array pointer. // Memory allocation for Dynamic Array in the heap. DynArray = new int*[nSize]; for(int i=0; i< nSize; i++){ DynArray[i] = new int[nSize]; } // Assign some element of AutoArray AutoArray[2][3] = 7; // and call output function OutAutoElm(2, 3, (int *)AutoArray, nSize); DynArray[3][4] = 9; // Assign some element of DynamicArray OutDynElm(3, 4, DynArray); // and call output function AutoArray[5][0] = 10; // Error! Outside of the array. The last element is [4][4] // But the program executed in my system without any errors. // Release memory of Dynamic Array for(i=0; i< nSize; i++){ delete[] DynArray[i]; } delete[] DynArray; return 0; } void OutAutoElm(int nRow, int nCol, int *pAutoArray, int nSize) { // What a strange expression! int nValue = *(pAutoArray + nRow*nSize + nCol); cout << "AutoArray["<<nRow<<"] ["<<nCol<<"]="<< nValue << endl; } void OutDynElm(int nRow, int nCol, int **pDynArray) { int nValue = pDynArray[nRow][nCol]; // Looks Normal cout << "DynArray["<<nRow<<"] ["<<nCol<<"]="<< nValue << endl; } A very interesting example! DangersThere are some common errors when using pointers. Most of them are very dangerous because they can be executed in your system without runtime errors. And nobody knows when and where they will crash the system.
Here I described only plain errors that are common for C and C++ languages. When we use classes, inheritance, multiply inheritance, templates and other OOP constructions, we have much more opportunities for making mistakes with pointers. Be optimistic! RecommendationsThere are some rules that could prevent you from many errors.
AnnouncementI described only plane problems of using pointers in this article. I plan to continue this topic in the next article that would be called "Pointers and Classes". Links
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||