How to debug the VC program

Source: Internet
Author: User

The trace usage is exactly the same as that of printf, And the debugging information is output in the output box.

Assert receives an expression. If the expression is true, there is no action. Otherwise, the current program of the terminal is executed and the macro is interrupted, it should be considered that the function call fails to meet the system's prerequisites for calling this function. For example, for a window not created, call setwindowtext

The Verify and assert functions are similar. The difference is that in the release version, assert does not calculate the expression value, while verify calculates the expression value.

Several functions used for C/C ++ debugging

1. Call Stack Series

Below is the function prototype

#include "execinfo .h"int backtrace(void **buffer, int size);char **backtrace_symbols(void *const *buffer, int size);void backtrace_symbols_fd(void *const *buffer, int size, int fd);

(1) backtrace is used to obtain the call stack of the current program and store the result in the buffer. Generally, we use GDB to debug the program and set the appropriate breakpoint. After stopping the program, we can use the backtrce command to view the current call stack. However, sometimes we use conditional breakpoint. The function of GDB is not as powerful as the program itself. In this case, you can consider calling the backtrace function in the program to obtain the call stack.

(2) backtrace_symbols converts the call stack obtained by backtrace into a string array and returns it in the form of a string array. This requires the memory occupied by the returned string array to be released outside.

 

(3) backtrace_symbols_fd writes the call stack information obtained by backtrace to the file specified by FD.

void * __builtin_return_address (unsigned int level)

This function is used to obtain the current function, or the return address of the function that calls it. After obtaining this address, you can obtain information about the called function through GDB disassembly, this is also a fangfa for obtaining call stacks in applications.

2. Memory Allocation and release

#include "malloc .h"size_t malloc_usable_size((void *__ptr));

This function is used to return the size of the available memory actually allocated after calling malloc. We know that in C ++, operator new () can overload various versions, you can pass in the call information to track memory allocation. But operator Delete () has only two forms and cannot be reloaded at will. In particular, the Global operator Delete () has only one version, which is quite painful, how much memory is released. At this time, the malloc_usable_size () function is useful. You can call it to obtain the size of the memory currently released, in new, please use it to calculate the opened memory so that it can be matched, because the actual memory allocated when calling malloc is much larger than the user applied, therefore, if the statistical methods on both sides do not match, the statistical results will also be highly deterministic.

C ++ Memory Allocation and Management

1 Function

(1) operator New Function

void * ::operator new(size_t);                //Globalvoid * class-name::operator new(size_t); //Class 

The above is the prototype of operator new function in C ++. One is global type, the other is class member type, and the global operator new function is called in the following two cases: one is to allocate C ++ built-in dynamic memory, and the other is to allocate the operator new function defined by the user, when a user applies for dynamic memory of this type with new, the user will call this type of member function operator new instead of the global operator new.

In addition, we noticed that the return value void * type of the function in the prototype above, the first parameter is size_t type, which is required by C ++ compilation. If you need to reload the operator new function, the return value must be of the void * type, and the first parameter must be of the size_t type. Otherwise, the compiler returns the following error message:

error: ‘operator new’ takes type ‘size_t’ (‘unsigned int’) as first parameter

It is worth noting that we can use operator new function to overload the feature, and pass in some additional information through parameters to debug the program and detect memory leakage. For example, we can reload the code below and pass the row number and function name at the call, so that we can track the memory allocation.

void * operator new(size_t unSize, int nLine, const char * pFunc){    prinft("Line: %d, Func: %s, allocate %u byte(s)\n", nLine, pFunc, unSize);    return malloc(unSize);}

(2) operator delete function

void operator delete( void * );void operator delete( void *, size_t );

The above is the prototype of the operator delete function. The operator delete function also has two types of global and class members. Note that a class can only have one operator delete function as its member function, it must be either of the above two types. There is no other form. If a class implements its own operator delete function member function, when this type of memory is released, the compiler calls the operator delete function, instead of the global function.

In the above two prototypes, the first is to pass in the first address of the released memory during the call, and the second is to call, the compiler will pass in the first address and size of the memory to be released. Therefore, this feature can be used, if we implement the member function of the second form of operator delete function in the base class, we can release the sub-class memory.

2 Operator

(1) New operator

[::] new [placement] new-type-name [new-initializer][::] new [placement] ( type-name ) [new-initializer]

The above is the prototype of new operator. In C ++, dynamic memory allocation is usually done by calling new operator, and new operator is used to allocate dynamic memory, the compiler should do the following two tasks:

A. Call operator new function to allocate memory

B. Call the constructor for initialization.

The following describes what the components in the new operator prototype are doing:

Placement: If you overload operator new function, placement can be used to pass additional parameters.

Type-Name: specifies the type of memory to be allocated. It can be a built-in or custom type.

New-intializer: Specifies the initialization parameter of the memory to be allocated, that is, the constructor parameter. Note that initialization parameters cannot be specified when an object's array-type memory is allocated: in other words, you want to allocate an object's array-type memory, the object must have a default constructor.

(2) Delete Operator

[::] delete cast-expression[::] delete [ ] cast-expression

The above is the prototype of Delete operator. The first is used to release the memory of the common object type, and the second is used to release the memory of the array type of the object. In C ++, the dynamic memory allocated by new operator must be released by calling Delete operator. The delete operator is usually used to release the memory compiler for the following two cases:

A calls the object destructor to analyze the object

B. Call operator delete function to release the memory.

3. Notes for using new/delete

(1) how to distinguish operator new/delete functions from new/delete operator?

As described above, it is not difficult to see that we allocate/release dynamic memory and call New/delete operator. In the process of calling New/delete, the compiler automatically calls operator new/delete function to complete the actual memory allocation/release.

(2) Using Delete operator to release a memory that is not released by new operator is unpredictable. Therefore, remember that operator new and operator delete must be used in pairs, this is the basis for writing a program.

(3) a std: bad_alloc exception will be thrown when the new operator fails to be called, provided that you have not reloaded the corresponding operator new function; Delete operator fails, the common cause is that the same memory is deleted multiple times.

(4) If a piece of memory is deleted, and then it is referenced (dereference), the results are unpredictable and may cause program crash.

(5) delete a null pointer is safe and harmless.

(6) operator new/delete functions of the class member type must be static functions, so they cannot be virtual functions and comply with public, protected, private access permission Control

 

How to debug the VC program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.