malloc function Details (vs. New)

Source: Internet
Author: User

The full name of malloc is memory allocation, which is called dynamic RAM allocation, and when it is impossible to know the exact location of the memory, it is necessary to use the dynamic allocation memory when you want to bind the real memory space.

First, prototype: extern void *malloc (unsigned int num_bytes);

Header files: #include <malloc.h> or #include <alloc.h> (Note: The contents of alloc.h and malloc.h are identical. )

Function: Allocates a memory block of length num_bytes bytes

Note: If the assignment succeeds, it returns a pointer to the allocated memory, otherwise the null pointer is null.

Use the free () function to release memory blocks when memory is no longer in use.

Example:

[C-sharp]View PlainCopy
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. int main ()
  4. {
  5. Char *p;
  6. p= (char *) malloc (100);
  7. if (p)
  8. printf ("Memory allocated at:%x/n", p);
  9. Else
  10. printf ("Not Enough memory!/n");
  11. Free (p);
  12. return 0;
  13. }

Second, function declaration (function prototype):

void *malloc (int size);

Description: malloc allocated memory space to the system to allocate a specified size byte. The return type is the void* type. Void* represents a pointer to an indeterminate type. c,c++ Specifies that the void* type can be cast to any other type of pointer. This can be found on MSDN with the relevant explanations, as follows:

malloc Returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, with a type cast on the return value. The storage space pointed to by the return value are guaranteed to being suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.

Iii. different points of malloc and new

As you can see from the function declaration. malloc and new are at least two different: new returns a pointer of the specified type, and can automatically calculate the desired size. For example:

int *p;

p = new int; The return type is the int* type (integer pointer), and the allocation size is sizeof (int);

Or:

Int* Parr;

Parr = new int [100]; The return type is the int* type (integer pointer), and the allocation size is sizeof (int) * 100;

 

malloc, however, must be computed by us to calculate the number of bytes and forcibly converted to a pointer of the actual type after the return.

int* p;

p = (int *) malloc (sizeof (int));

First, the malloc function returns the void * type if you write: p = malloc (sizeof (int)); The program cannot compile, error: "Cannot assign void* to int * type variable". Therefore, the cast must be passed (int *).

Second, the argument to the function is sizeof (int), which indicates the size required for an integral type of data. If you write:

int* p = (int *) malloc (1);

Code can also be compiled, but in fact only allocates 1 bytes of memory space, when you put an integer inside, there will be 3 bytes homeless, and directly "live in the neighbor's house"! The result is that the contents of the original data in the back memory are all emptied.

Malloc can also achieve the effect of new [], requesting a contiguous amount of memory by specifying the size of the memory you need.

For example, to allocate 100 spaces of type int:

int* p = (int *) malloc (sizeof (int) * 100); Allocates a memory space that can be placed down to 100 integers.

Another thing that cannot be directly seen is that malloc allocates memory and does not initialize the resulting memory, so the value will be random in a new piece of memory.

In addition to the method of allocation and final release, pointers are obtained through malloc or new and are consistent on other operations.

Summarize:

The malloc () function actually looks for a space in memory for a specified size, and then assigns the first address of the space to a pointer variable, where the pointer variable can be either a single pointer or an array's first address, depending on the specifics of the parameter size in the malloc () function. The memory space allocated by malloc here is logically contiguous, and can be physically continuous or discontinuous. For our programmers, our focus is on logical continuity, because the operating system will help us arrange memory allocations, so we can use it as a continuous.

malloc function Details (vs. New)

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.