C language usage and the difference between malloc, free and new, delete the

Many people learned the C malloc are not very understanding, know to use malloc to add headers, know that malloc allocates a block of contiguous memory, know and free functions are used together. But however:

Some people still will: malloc or keyword as provided by the C system, in fact: a common C standard library function malloc only provided

And many, many people are on the specific implementation mechanism malloc is not very understanding.

1, on several malloc and related functions

#include <stdlib.h> (Linux lower) 

       void * the malloc (size_t size); 
       void Free (void * PTR); 
       void * calloc (size_t of nmemb, size_t size); 
       void * realloc (PTR void *, size_t size); 
 It could be argued (window under) prototype: extern void * malloc (unsigned int num_bytes);
  Header: #include <malloc.h> or #include <alloc.h> contents of both are exactly the same.

  If the allocation is successful: Points are allocated memory space pointer is returned

       Otherwise, it returns a null pointer is NULL.

       Meanwhile, when the memory is no longer used, should be used free () function of the memory blocks freed.

  malloc allocated memory size is at least the number of bytes specified by the size parameter

  malloc return value is a pointer to the starting address of the memory section available

  Multiple calls to malloc allocated addresses should not overlap, unless a particular malloc allocated address is freed

  malloc memory allocation should be completed as soon as possible and return (you can not use NP-hard memory allocation algorithms malloc and free functions are paired, that is, if you do not release the memory leak after application; if it is released for no reason did not do anything, can only be released once release If the release of two and more than two errors occur (but released null pointer exception, the release of a null pointer actually equal to nothing to do, so, how many times are possible release))

  Should be achieved while achieving malloc and deallocation of memory sizing function (realloc and free)

      About: void *, represent undetermined type of pointer. C, C ++ provisions, void * types can be strong into any other type of pointer.  

    Void * on the other saying:

         void * p1;

         int *p2;

         p1 = p2; that is any other type can be assigned to it directly, without the need for strong turn, but not vice versa.

2, malloc and new

It returns a pointer to the specified new type, and may automatically calculate the required size. 

      P int *; 
      P = new new int; // return type of type int *, allocated size sizeof (int) 
      P = new new int [100]; // return type of type int *, allocated size sizeof (int ) * 100 
      and malloc we must calculate the number of bytes, and turn into strong when the actual return pointer of the specified type. 
      int * P; 
      P = (int *) the malloc (the sizeof (int)); 
      . 1, the malloc returns a void *, if we written: p = malloc (sizeof (int )); indirect described (the void * conversion to the int *, which is unreasonable) 
      2, the argument is the malloc sizeof (int), for indicating a size of data desired shaping, if we write: 
      P = (int *) the malloc (. 1), it can be seen out: just applied for a byte of space, if stored for an integer, then to the inside, 
      will take up an additional 3 bytes, may change the original data memory space 
     3, malloc simply allocate memory, and can not it is initialized, so a new memory obtained, its value will be random. In a general sense: I 
       habitually will be initialized to NULL. Of course, also be used memset function.

simply put:

malloc function is actually in memory: find a designated space, and then the first address space to a pointer variable, where the pointer variable can be a single pointer, the first address may also be an array, it depends details size malloc function parameters. Here malloc memory space is allocated in logically contiguous, but physically may not be continuous. We, as a programmer, concern is continuous, the other, the operating system helped us deal with the logic.

3, the difference between malloc, free and new, delete the

(1) malloc and new are allocated on the heap memory. Stack memory allocation area is assigned automatically released, the main function to store parameters, local variables, and the like.

(2) Malloc free and used in C programs, C ++ programs and the use of new and delete, delete the array delete [] p, after the release of the pointer, to the pointer blanking.

(3) New and delete call constructors and destructors.

(4) Malloc is a function, new keyword.

(5) Malloc not given initial value, new new can, as int * p = new int (2). Int representative of a type assigned memory space, and 2. If the new new initial value int () on behalf of given initial value 0, new int [10] representational 10 int.

(6) Malloc returned pointer is a void *, the new type is that it returns a pointer to the allocated space.

Guess you like

Origin www.cnblogs.com/yuanqiangfei/p/11248118.html