C Language Memory usage

Source: Internet
Author: User

Problem: Memory usage

Someone wrote a function that converts an integer to a string:

char *itoa (int n)
{
    char retbuf[20];
    sprintf(retbuf, "%d", n);
    return retbuf;
}

If I call this function: char *STR5 = itoa (5), what happens to STR5?

Answer analysis:

The answer is uncertain, and it is certain that it is definitely not the "5" we would like to have.

RETBUF is defined in the function body as a local variable whose memory space is located somewhere in the stack (stack) and its scope is limited to the itoa () function. When the itoa () function exits, the contents of the Retbuf on the call stack are retracted, at which point the memory address may be stored in other content. Therefore, it is not up to the intended purpose to return RETBUF this local variable to the caller.

So how to solve this problem, do not worry, not only the method, but also more than one, the following to explain the three ways to solve this problem:

1), in the itoa () function to define a static char retbuf[20], according to the characteristics of static variables, we know that this can ensure that the function returned after the RETBUF space will not be retracted, because the static variables within the function is not placed in the stack, but in the program is called. BSS section, the content of this place is not withdrawn because the function exits.

This approach does solve the problem, but this approach also causes the itoa () function to become a function that cannot be reentrant (that is, there is no guarantee that the same input must have the same output), and that the content in Retbuf [] will be replaced by the next invocation result, which is not recommended.

2), within the itoa () function, use malloc () as the RETBUF to request the memory, deposit the result, and then return the RETBUF to the caller. Because Retbuf is in the heap (heap) at this time, it will not be released as the function returns, so we can achieve our goal.

But there is a situation that needs to be noted: The caller of the itoa () function must release the RETBUF when it is not needed, or it will cause a memory leak, if this function and the calling function are written by the same person, the problem is not very small, but if not, it is easier to neglect this free memory operation.

3), the function is defined as char *itoa (int n, char *retbuf), and retbuf space is requested and released by the caller, and itoa () only stores the conversion results to RETBUF.

This approach is obviously better than the first to second method, which avoids the effect of method 1 on functions, and avoids the effect of Method 2 on the release of memory allocations, which is currently a more common practice.

Extended Analysis:

In fact, in terms of the problem itself, I think everyone can immediately think of the answer, the key is to the memory of the sensitive resources of the correct and reasonable use, the following memory do a simple analysis:

1), the program has different memory segments, including:

. Data-initialization of global/static variables, valid throughout software execution;

. BSS-Uninitialized global/static variables, valid throughout the software execution process;

. stack-function call stack, where the content is valid during function execution and is assigned and retracted by the compiler;

. heap-heap, explicitly allocated and retracted by the program, if not retracted, is a memory leak.

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.