Working with clues in C ++: Possible hazards and hazards

One of the most powerful tools in C ++ for developers are tips. C ++ indicators are used to access different memory locations that are assigned while collecting and executing an application. This gives an estimated amount of flexibility, depth and strength for the application to be written and adds another dimension to object-oriented ideology.

But as we all know from the violence of our children: With great power is a great responsibility (God who sounded such a cliché). Thus, tips in C ++ are double swords. Some such typing or bendil hazards are memory or inaccessible memory and dangling tips.

This article discusses the main focus on the level and the steps that all C ++ developers need to take to prevent such risks.

Focused Focus

The main focuses are:

1. Minnisleka
2. Dangling Pointer
3. Bad Bendill

Minnisleka is a common occurrence in many C ++ codes. Basically, this happens when the cursor, which already points to a memorandum, says that "A & # 39; refers to another minor, say," B ". Since the only way to access assigned smaller blocks are directed through the point, the first location, A, is now completely inaccessible, resulting in less leakage. Because the translator does not share the memory itself (the programmer must use the delete keyword) and as our cursor is now referring to memory location B causes waste of memory and resources of the system to be used.

int * ptr = nullptr;
ptr = new int;
* ptr = 5;
] ptr = new int;

Here is the memory space containing integer 5 & # 39; Now you can not access what the pointer "ptr" is pointing to another memory location now . Consequently, the space used by the system to store one integer wasted until the end of the program.

General rules to prevent less leakage, be sure to talk about & # 39; delete & # 39; and & # 39; new & # 39; The keywords in the app are equal, like each time you assign new memory with "new", you must allocate it by deleting & # 39; delete & # 39;.

Dangling Pointer:
The dangling cursor is a pointer pointing to a location in a memory that is not part of your application. It happens when a programmer does not respond to the cursor toward another memory location after using & # 39; delete & # 39; statement.

int * ptr = new int;
* ptr = 10;
delete ptr;
cout << * ptr;

Notice that the integer memory associated with the cursor has been deleted, but we still try to tag the tag and produce its value. This can lead to a very serious situation in the application, leading to infinite loop or front rush.

Bad bendill:
Always keep the cursor when you create, otherwise it would be a bad pointer and point to any random place in memory. If you understand this cursor infinite, it could lead to a system crash. Since the uninitialized cursor points to a random memory location, this location might well be a memory location previously used by another cursor, leading to significant problems in the application.

int * ptr;
cout << * ptr;

Usually you should first initialize it to NULL value and some other value later in your application. The new standard has created the keyword & # 39; nullptr & # 39; for this purpose.

int * ptr = nullptr;

It is easy to see that C ++ instructions should be treated with great care and caution. There are many possibilities that can lead to crashes and bugs in your plans while using the Pope, but if you try to avoid the indications mentioned above, you should be generally fine !!!

Source by Raza Ahmad

Leave a Reply

Your email address will not be published. Required fields are marked *