Water drop stone wearing Co-language

xiaoxiao2021-03-06  50

Question: Memory uses some functions that convert integers to strings:

Char * ITOA (INT N) {Char Retbuf [20]; Sprintf (RetBuf, "% D", N); return retbuf;} If I call this function: char * str5 = itoa (5), what is STR5? ? Answer analysis: The answer is uncertain, it can be determined is not what we want. " RetBuf is defined in the function body, is a local variable, and its memory space is located in a position in the stack (STACK), and its scope is also limited to this function in the ITOA (). When the ITOA () function exits, RetBuf will be retracted in the contents of the call stack. At this time, this memory address may store the content. Therefore, returning the partial variable of RetBuf to the caller is not the expected purpose. So how to solve this problem, don't worry, the method is not only, but more than one, let's explain the three ways to solve this problem: 1), define a Static Char Retbuf [20] in the ITOA () function. Depending on the characteristics of static variables, we know that this can ensure that the function returns RetBUF space will not be recovered because the static variables in the function are not placed in the stack, but in the program is called ".bss" segment Where the content of this place is not recovered because the function is exited. This approach can solve the problem, but this approach also leads to the ITOA () function to become an unremissible function (ie, the same input must have the same output), in addition, RetBUF [] The content will be replaced by the next call result, which is not worth recommending. 2) In the ITOA () function, use malloc () to request a memory, and store the result, and then return RetBUF to the caller. Since RetBUF is located in the heap (HEAP), it will not be released as the function is returned, so we can achieve our goal. However, there is such a situation to note: the caller of the ITOA () function must release it when it does not need RetBuf, otherwise the memory is leaking, if this function and the call function are written, the problem is not But if not, it is easier to hurt the operation of this release memory. 3) Define the function as Char * ITOA (INT N, CHAR * RETBUF), and RetBUF's space is applied and released by the caller, and it is only stored to RetBUF. This approach is obvious than the first, two methods, avoiding the impact of the method 1 on the function, and avoiding the impact of Method 2 on memory allocation, is a current approach. Practice. Extended analysis: In fact, on this problem itself, I think everyone can immediately think of the answer, the key is the correct and reasonable use of memory such sensitive resources, the following is a simple analysis of memory: 1), program There are different memory segments, including: .data - has initialized the overall / static variable, valid throughout the software execution; .bss - not initialized the overall / static variable, valid throughout the software execution; .stack - function call stack The content is valid during the execution of the function and is responsible for assigning and recovering by the compiler; .HEAP - heap, by the program explicitly assigned and reclaimed, if not recovered is a memory leak. 2) The memory you use is best to apply and release yourself.

This can be said to be a principle of memory allocation and release, such as the second type above solution, the memory allocated by ITOA (), and finally released by the caller, it is not a good way, it is better to use the third kind Apply and release by the caller yourself. In addition, this principle also means: if you want to use a pointer, it is best to confident that it has pointed to the legitimate memory area. If you don't have your own assignment, or you have illegal pointer access. Many procedural fatal errors are accessing a pointer that does not point to the legitimate memory area, which also includes empty pointers. Problem: Memory Allocation & SizeOf I use SizeOf to calculate a pointer variable, I want to get the size of the memory block allocated by this pointer variable, can I? Char * p = null; int nmemsize = 0; ... p = malloc (1024); nmemsize = sizeof (p); answer and analysis: The answer does not meet your requirements, Sizeof can only tell you the memory size of the pointer itself . The memory pointed to by the pointer, if it is malloc assignment, SizeOf is no way to know. In other words, Malloc allocated memory is no way to query the memory management module, of course, you can write code to maintain it. Question: What is the problem with the stack memory uses the following program?

Char * getString (void) {char p [] = "Hello World"; return p; // The compiler will make a warning} void test4 (void) {char * str = null; str = getString (); // STR The content is junk cout << str << endl;} answer and analysis: return to the stack memory, the memory may be destroyed, or it may not be destroyed, but after the scope has been marked, it can be used by the system, so messy Unknown, of course, the content of the pointer should be constant, it should be useful, for example, can be used to detect system memory allocation law, etc. Question: Memory usage related programming specification I want to avoid the problem of memory usage as much as possible, is there any shortcut? Answer and analysis: unless you have done something you have done, it is a shortcut, that is, standing on the shoulders of the former, now all big companies have their own coding specifications, these regulations condense A lot of experience and lessons, have a high use value, given a lot of these regulations, there is no longer listed here, I am interested, recommend reference to Lin Rui's "High Quality C / C Program Guide".

转载请注明原文地址:https://www.9cbs.com/read-82922.html

New Post(0)