malloc function allocation memory failure reason and solution _stm32

Source: Internet
Author: User
Tags null null
The reason and solution to the failure of malloc function allocating memory first of all, the conclusion

The reason the malloc () function allocates memory failure:
1. Not enough memory.
2. There was a cross-border access to memory in the previous program, causing some of the information involved in the malloc () allocation function to be corrupted. The next time you use the malloc () function to request memory will fail, and return null pointer nulls (0).

Workaround:
1. In the STM32 The heap space memory insufficiency solution may refer to my last article

STM32 Allocation stack space insufficiency problem reason and solution method http://blog.csdn.net/lighthear/article/details/69485942

2. Starting with the malloc () function that cannot be assigned, and looking back at the nearest malloc () function, the offending code should be in this section, probably because the pointer is out of bounds, manipulating the unknown memory, causing the malloc () to continue allocating memory. Problems encountered

Recently encountered STM32 to create a linked list, the first run is normal, the second failed to create a normal problem. On-line debugging found that the malloc () function was not allocated to memory, but I have previously adjusted the memory space, there should be no small data is not enough memory. Solving method

Initially thought it was because the first run of the free memory operation did not perform properly, resulting in a waste of too much memory space to generate memory, tried several ways to rewrite the released function, also looked at the use of the free () function, proved to be used in the right way. Problem not resolved

It was later speculated that the pointer variable was released without pointing to null, resulting in a wild pointer. However, all pointer variables are local variables and will be released automatically after the execution of the program, and will not affect the next time. Problem not resolved

Check the data on the web to find that malloc () memory allocation failed due to the lack of memory, but also may have been before the memory pointer out of bounds.

The reason the malloc () function allocates memory failure:
1. Not enough memory.
2. There was a cross-border access to memory in the previous program, causing some of the information involved in the malloc () allocation function to be corrupted. The next time you use the malloc () function to request memory will fail, and return null pointer nulls (0).

Under the guidance of this thought, look forward to a function that might have a pointer out of bounds

unsigned char * strarray_valarray (char *pstrarray)
{
    int valarraysize = strlen (Pstrarray)/2;       
    unsigned char *pvalarray;
    Pvalarray = (unsigned char *) malloc (sizeof (unsigned char) * valarraysize);
    for (int i = 0; i < valarraysize i++)
    {
        sscanf (Pstrarray + i * 2, "%2x", (unsigned int *) (Pvalarray + i));    Force type conversion causing pointer to cross-border
    }
}

found that the declared Pvalarray is a pointer to the unsigned char * type, because the third parameter of the SSCANF () prompts for a variable of the unsigned int * Type, thus forcing a type conversion to Pvalarray, from unsigned char * into the unsigned int *. Although for Pvalarray, the memory space does not change (all pointers, 32-bit Systems allocate 4 bytes), for the compiler, the meaning of the data is extended from the unsigned char to the unsigned int, followed by the pointer traversal access, Making changes to an array causes the pointer to go out of bounds and change to an unassigned region. So the next time you use the malloc () function to request the memory will fail, return null pointer nulls (0).

Therefore, Pvalarray is simply defined directly as a variable of type unsigned int *, and space is allocated according to the unsigned int type, without coercion type conversion. This prevents the pointer from crossing over. The test found that the problem no longer occurred. Summary of Problem Solving

If the next time you encounter such a problem, starting with the malloc () function that cannot be assigned, and looking back at the nearest malloc () function, the offending code should be in this section, probably because the pointer is out of bounds, manipulating the unknown memory, causing the malloc () Unable to continue allocating memory.

Therefore, the operation of the pointer in the future must be extremely careful to prevent the problem of the pointer out of bounds.


1. malloc function request memory failure, return NULL null (0): http://blog.csdn.net/gadflycq/article/details/40752373
2. The reason and resolution of malloc function allocation memory failure in sufficient memory: http://blog.sina.com.cn/s/blog_71d3b5480100lxcx.html

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.