Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The difference and use of new and malloc in C++

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the difference and use of new and malloc in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

By definition: malloc memory allocation dynamic memory allocation is a function in c

How to use it:

Extern void * malloc (unsigned int num_bytes)

Extern void * malloc (unsigned int num_bytes)

Um_bytes memory block byte length.

The memory block size is determined: malloc is calculated by us to get a new piece of memory, and then the data type is specified and the memory value is random.

When in use: you need to introduce the header file library function stdlib.h or malloc.h (malloc.h is the same as alloc.h).

Memory allocation location: dynamically allocated memory in the heap.

The specific allocation process: the program applies to the operating system, the operating system traverses the list of idle nodes, assigns the first heap node larger than the application space to the program, and then deletes this node from the list of idle nodes.

Successful allocation: the return value is a pointer to the allocated memory.

Failed allocation: the return value is empty NULL.

Return type: void* (pointer to the undetermined type).

Void* types can be converted to any other type by casting (because the data type stored by the user is unknown, it is up to the user to determine the data type).

Memory block release: the free () function returns memory to the program or operating system.

Note: both malloc and free belong to the standard library function of cAccord +. Memory leaks may occur if you do not release the application after pairing the application.

When using free, you need to check whether the pointer is empty.

Ew is the operator in C++ (its status is equivalent to "+", "=").

When using: new, it not only allocates memory, but also initializes and executes the corresponding constructor, and the data type needs to be specified during initialization.

Memory allocation location: the free storage area allocates memory for objects.

When in use: there is no need to introduce the header file, new is a reserved word.

Ew and delete are used in pairs.

Be careful to set the pointer to 0 when using delete, otherwise it will form a dangling pointer (the memory the pointer refers to has been released and still points to it), resulting in an error.

Ew can be thought of as the execution of malloc plus constructor, but new is more advanced.

Several uses of ew:

1 int * p=new int; / / open an int variable 2 int * p=new int [10] in the free storage area; / / open an int array with 10 elements 3 int * p=new int (10) in the free storage area; / / open an int variable in the free storage area and initialize it to 10

There is no need to check if it is empty when freeing memory.

If p equals NULL, delete p does nothing. Since tests are available later, and most test methodologies force explicit testing of each branch point, you should not add extra if tests.

Wrong:

1 if (p! = NULL) 2 delete p; 3 correct: delete

Questions that may be asked during the interview:

1) both of them can be used to request dynamic memory and free memory.

2) malloc is a library function that can only act on internal data types. For non-internal data dynamic objects, it is impossible to initialize and destroy objects, that is, to execute constructors and destructors, while operators such as new and delete can be completed within the control of the compiler. Object initialization and destruction tasks, that is, the execution of constructors and destructors.

Since the function of new/delete completely covers malloc/free, why doesn't C++ eliminate malloc/free? This is because C++ programs often call C functions, while C programs can only use malloc/free to manage dynamic memory.

Instead of trying to use malloc/free for memory management of dynamic objects, we should use new/delete. Since "objects" of internal data types have no process of construction and destructing, malloc/free and new/delete are equivalent to them.

Note: if you release a "dynamic object created by new" with free, the object may cause a program error because it cannot execute the destructor.

If you use delete to release "malloc requested dynamic memory", the result will also cause an error in the program, but the readability of the program is very poor. So new/delete must be used in pairs, and so does malloc/free.

A zero-value pointer is a pointer with a value of 0, which can be any pointer type, a general variant type void*, char*,int*, and so on. Null pointer is just a programming concept, just like a container may have two basic states: null and non-null, and a value of 0 may be stored in non-null time, so null pointer is assumed to be a pointer that does not provide any address information.

Memory leaks can be detected for either malloc or new, but the difference is that new can indicate which line of that file, but malloc does not have this information.

This is the end of the content of "the difference and use of new and malloc in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report