Home  >  Article  >  Computer Tutorials  >  malloc function for dynamic memory allocation in C language

malloc function for dynamic memory allocation in C language

WBOY
WBOYforward
2024-01-17 09:09:06897browse

malloc function for dynamic memory allocation in C language

malloc function in C language

Need to include header files:

#i nclude

or

#i nclude

Function declaration (function prototype):

void *malloc(int size);

The malloc function is used to apply to the system to allocate a memory space of a specified size, and the return type is void*. In C and C++, a pointer of type void* can be cast to a pointer of any other type.

It can be seen from the function declaration that malloc and new have at least two differences: new returns a pointer of the specified type and can automatically calculate the required size. For example:

int *p;

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

or:

int* parr;

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

And malloc must calculate the number of bytes required by us, and force conversion to a pointer of the actual type after returning.

int* p;

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

First, the malloc function returns the void * type. If you write: p = malloc (sizeof(int)); then the program cannot be compiled and an error will be reported: "void* cannot be assigned to an int * type variable." So you must pass (int *) to cast.

Second, the actual parameter of the function is sizeof(int), which is used to specify the required size of an integer data. If you write:

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

The code can also be compiled, but in fact only 1 byte of memory space is allocated. When you store an integer in it, 3 bytes will be left homeless and directly "live in" Neighbor’s house”! The result is that all the original data content in the subsequent memory is cleared.

malloc can also achieve the effect of new [] and apply for a continuous memory. The method is nothing more than specifying the memory size you need.

For example, if you want to allocate 100 int type spaces:

int* p = (int *) malloc ( sizeof(int) * 100 ); //Allocate memory space that can hold 100 integers.

Another difference that cannot be seen directly is that malloc only allocates memory and cannot initialize the obtained memory, so the value of the new memory obtained will be random.

Except for the different allocation and final release methods, the pointer is obtained through malloc or new, and other operations are consistent.

The problem with the C language malloc function is that I use malloc twice in the following function to give p a total of 6 units of memory

Expand All

You allocated memory twice. The first time was 3 ints. Unfortunately, you lost it. The second time you allocated three ints, you still used this pointer. You did not assign values ​​to the first three ints of the new memory and wrote directly out of bounds. With the 3 int units outside the boundary, it was a fluke that the program did not crash immediately

int *p, *p1,i;

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

for(i=0;i

printf("\n int put:");

scanf("%d",p i);

}

p1 = (int *)malloc(3*sizeof(int));

for(i=0; i

printf("\n int put:");

scanf("%d",p1 i);

}

for(i=0;i

for(i=0;i

Super simple malloc function problem

You can take a look at the prototype of the malloc function

void* malloc(int)

The parameter of malloc is an integer, indicating the amount of memory to be applied for, in bytes. The return value is a pointer to that memory, which is a null type pointer.

And sizeof is an operator, through which you can get the number of bytes in memory occupied by a type or variable, and the result is an integer. So pp=(float *)malloc(sizeof(float)); actually calculate sizeof(float) first to get the number of bytes occupied by a float type in memory. In a 32-bit environment, it should be 4 bytes, and the result is 4. Then malloc(4) applies for a 4-byte memory space, returns the address of this memory area, and then forces it to be converted to float* type.

About linked lists, each linked list element stores the address of the next or previous element. Naturally, each linked list element occupies memory space. When you want to add a new element to the linked list, you must first allocate an address to this element. , otherwise there will be no place to save the data.

The above is the detailed content of malloc function for dynamic memory allocation in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete