How to use the C ++ standard

I have this classic question about how C ++ Standard should (I mean the actual official document is finalized), for example. C ++ 98, C ++ 03 can be used for training and learning C ++. My idea is only from the point of view of the average C ++ user, and not from the point of view of language lawyers or those who want to be on the standards committee, compilers, etc.

Here are my personal thoughts:

a) This is a good place to start learning C ++. Books such as "C ++ in a Nutshell", "C ++ Programming Language", etc., work very well on this front, while at the same time closely touching the standard.

b) It is necessary to return to the standard only when

  • the compiler gives behavior that does not match what general books say, or

  • certain behavior is inconsistent for compilers, for example. GCC, VS, Comeau, etc. I understand that these compilers can be inconsistent in very few cases / dark corners of the language, for example. handling patterns / exceptions, etc. However, it is really known about the possible behavior of different compilers only when one of them transfers and / or migrates to another environment or when there is a compiler update, for example.

  • if the concept is poorly explained / not explained in books, for example. if it’s a truly advanced concept.

Any thoughts / ideas / recommendations on this?

+3
source share
4 answers

++ . , . , , , , , (, , , , , ).

, . ,

  • , , , , , .

  • , , , ( ), .

  • , , : , , . , , . , .

  • ++ , .: -)

  • . , . - , - .

+7

.

++ , STL .

+2

I use g++to compile my C ++ programs, and there I use the option -std=c++0x(earlier, -std=c++98) to make sure that my code always complies with the standard. If I get any warning or error regarding standard compliance, I will investigate this to educate myself and correct my code.

0
source
CPP coding guidelines
MEMORY MANAGEMENT

BUFFER OVERRUNS

STATIC BUFFER OVERRUNS

A static buffer overrun occurs when a buffer, which has been declared on the stack, is written to with more data than it was allocated to hold. The less apparent versions of this error occur when unverified user input data is copied directly to a static variable, causing potential stack corruption.

HEAP OVERRUNS

Heap overruns, like static buffer overruns, can lead to memory and stack corruption. Because heap overruns occur in heap memory rather than on the stack, some people consider them to be less able to cause serious problems; nevertheless, heap overruns require real programming care and are just as able to allow system risks as static buffer overruns.
Soulflower - Oil and Handmade Organic Soap
Perfect Remedy For All Skin & Hair Problems. Buy Now!

ARRAY INDEXING ERRORS

Array indexing errors also are a source of memory overruns. Careful bounds checking and index management will help prevent this type of memory overrun.
1. Use memcpy_s instead of memcpy
2. In memcpy_s destination size should be more than or equal to source size
3. If destination size is less than source size, relevant handling to be implemented. e.g.
a. creating fatal error log and throwing exception
b. copying truncated data

MEMORY LEAKS

It is the responsibility of each application to "free" dynamically requested memory when it is finished using it. When an application dynamically allocates memory, and does not free that memory when it is finished using it, that program has a memory leak.
1. Differentiate between delete of object and delete [] of array of objects.
Incorrect
char* myCharArray = new char[BUFF_SIZE];
delete myCharArray;
Correct
char* myCharArray = new char[BUFF_SIZE];
delete [] myCharArray;
2. While deleting check for NULL. If not NULL, delete and set to NULL
Correct
If(ptrData!= NULL)
{
delete ptrData;
ptrData=NULL;
}

3. Check size of STL container before iterating over its elements.

4. If STL containers contain pointers. Iterate over elements and delete each pointer individually. And clear container after this.
Incorrect

std::list<StructImageList*> listFinalImageIds //allocation and appending of pointers to list. ………

m_listFinalImageIds.clear();

Correct

std::list<StructImageList*> listFinalImageIds //allocation and appending of pointers to list. …… //clean up after list use is completed.

if(!m_listFinalImageIds.empty())

{ for(std::list<StructImageList*>::iterator it = m_listFinalImageIds.begin(); it != m_listFinalImageIds.end(); it++){ StructImageList* pData = *(it);

if(pData!= NULL) {

delete pData; pData = NULL;

}

}

m_listFinalImageIds.clear();
5. Use of BSTR. use smart pointer _bstr_t for handling of BSTRs.

https://msdn.microsoft.com/en-us/library/zthfhkd6.aspx


more at : 

http://www.writeulearn.com/cpp-coding-guidelines/

0
source

Source: https://habr.com/ru/post/1762577/


All Articles