Please write the malloc realloc free function in C.

Updated on technology 2024-02-10
7 answers
  1. Anonymous users2024-02-06

    Look"Malloc", there is an article on the first page that is clearly written.

  2. Anonymous users2024-02-05

    The differences between the malloc function and the realloc function in C are as follows: different allocations, different releases, and different forced transformations.

    First, the distribution is different.

    1. Malloc function: The malloc function directly allocates the unallocated memory block.

    2. realloc function: The realloc function is to reallocate the allocated memory block.

    Second, the release is different.

    1. Malloc function: The memory block allocated by the malloc function generally needs to use free(size t size) to release the memory block.

    2. Realloc function: The memory block allocated by the realloc function does not need to use free(size t size) to release the memory block.

    3. Forced conversions are different.

    1. Malloc function: The malloc function needs to be forcibly converted.

    2. Realloc function: The realloc function does not need to be forcibly converted.

  3. Anonymous users2024-02-04

    * Request n consecutive spaces with an integer length, if the first address of these spaces is successfully returned, 0 is returned if it fails

    sizeof(int)*n) * reallocates space to a pointer that has been assigned an address, and the parameter p is the original space address*

  4. Anonymous users2024-02-03

    Reference**.

    The content is relatively comprehensive.

  5. Anonymous users2024-02-02

    As LS says, allocating and freeing memory is determined at runtime, because whether or not memory can be allocated depends on the state of the runtime and cannot be determined at compile time. Therefore, the result obtained by malloc does not save the allocated length at all, and leaves it to the implementation of functions such as malloc free to decide.

    However, what ls said is too absolute, in the general implementation, the underlying layer of allocated memory may indeed be implemented in the form of a lookup table, but the size of the release when releasing memory is often determined by the allocated memory itself. Specifically, when a malloc (100) or other method is used to request 100 bytes of memory, the actual memory returned will be slightly larger than 100 bytes, and the extra part will be used to store the size of the sequential memory and other information that may be needed in the first few bytes of memory. free uses this information to mark the usage of this memory in the data structure used for the allocation (usually returned to the cache rather than to the operating system to accommodate future redistributions that may be needed immediately).

    This can also explain two other common problems: one is why using the wrong parameter for free is easy to cause the program to crash; Second, for very small memory (such as the size of an int), dynamic methods such as malloc are frequently used to allocate space, which is extremely inefficient.

  6. Anonymous users2024-02-01

    What is freed up is 100 bytes of memory.

    p = malloc(100);Draw a 100-size block in memory and point to that memory with p.

    free(p), that is, to release the space that the p pointer points to.

  7. Anonymous users2024-01-31

    There is a memory maintenance table for memory usage.

    The table records the start address and length of the occupied memory.

    After the malloc function is executed, the corresponding information will be added to the table.

    Then, when free, it will look up the value of this pointer, find this starting address in the table that has occupied memory, and if you find it, it will release the memory area recorded in the table, and then delete this record.

    If it cannot be found, an error is reported.

Related questions
10 answers2024-02-10

Hotkey. Registration (Tab 1.)Take the window handle ()0, F1, > register F1 >>>More

16 answers2024-02-10

Definition of a for loop.

for(the initial value of the variable; termination of operating conditions; The initial value of the first cycle i is 0, and it runs until i=3 stops i=2, and the condition is still satisfied, and the loop body is still executed, and the step size is 1+1 each time >>>More

7 answers2024-02-10

It's called the [batch] program, in fact, don't think that the program you mentioned above to deal with garbage is really powerful, it's amazing, when you have learned DOS, that program can be said to be a very simple thing; >>>More

4 answers2024-02-10

2. 1, True 2, False (there is only one program) 3, False (not necessarily) 4, True. >>>More

15 answers2024-02-10

C++ is an object-oriented language, not very easy to learn, because it is more flexible, so there are a lot of problems to consider when doing programs! Because of the direct operation of the hardware, it inherits the advantages of the C language, so it is very efficient, and is generally used for the underlying and embedded systems.